leaflet latlng to name of location

2019-08-17 05:12发布

问题:

I'm creating a form where the location is being passed to an input element by default. (Basically, click on the map and the coords are default on the form) While I need to work with the coords, and I have no problem returning the coordinates, I want to show the name of the city/area of those coords in the form instead of the lat/lng.

<input type="text" id="coords_lat" name="loc_lat" value={{clickedLat}} disabled>
<input type="text" id="coords_lng" name="loc_lng" value={{clickedLng}} disabled>

Right now I'm showing like this, but I would like to show something simple like "Madrid, Spain" or even get the actual address of those coords. Is this possible to do with Leaflet?

(note i'm using meteorjs in this project)

回答1:

The functionality you describe is called reverse geocoding.

You need an external service to perform such operation (like you need an external Tile Server to get raster tiles).

You can use Leaflet Control Geocoder plugin, which abstracts some of the calls to external (reverse) geocoding services:

A simple geocoder for Leaflet that by default uses OSM/Nominatim.