How can I determine which country, state, region, city or locality lies inside a leaflet shape (polygon | rectangle | circle etc) created by the user?
1 Answer
I think you are looking for Reverse gecoding. Leaflet does not provide gecoding services,but you can find similar example here.
Google Geocoding service provide results in JSON format but you may need to pay for extra quotas.
Sample link: http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452
Updated with Stack Snippet:
window.cb = function cb(json) {
document.getElementById('result').innerHTML = 'Country:' + json.address.country + '|' + 'State:' + json.address.state + '|' + 'County:' + json.address.county + '|' + 'City:' + json.address.city;
}
window.getLocation = function getLocation() {
var s = document.createElement('script');
s.src = 'http://nominatim.openstreetmap.org/reverse?json_callback=cb&format=json&lat=40.714224&lon=-73.961452&zoom=27&addressdetails=1';
document.getElementsByTagName('head')[0].appendChild(s);
};
.myText {font-weight:bold;font-size:1.0em;}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<button onclick="getLocation();" name="search" class='myText'>Get Location!</button>
<p id="result" class='myText'></p>
-
I am using nominatim reverse geocoder with leaflet but it doesnt offer any such functionality. Commented Aug 25, 2016 at 11:32
-
1It does provides, refer this documenations. Example:nominatim.openstreetmap.org/… Commented Aug 25, 2016 at 11:42