perliedman / leaflet-control-geocoder

A simple geocoder form to locate places. Easily extended to multiple data providers.
http://www.liedman.net/leaflet-control-geocoder/
BSD 2-Clause "Simplified" License
563 stars 221 forks source link

how to fill the result marker/popup with other json variables (strings) from OSM/Nominatim result ? #348

Open BlackBrix opened 1 week ago

BlackBrix commented 1 week ago

how to fill the result marker/popup with other JSON variables (strings) from OSM/Nominatim result ?

from the OSM/Nominatim JSON results (examples: https://nominatim.openstreetmap.org/search?format=json&q=%27Wechold%20162%27 https://nominatim.openstreetmap.org/search?format=json&q=%27Lange%20Strasse%204,%2027318%20Hoya%27 ) I would like to extract the 'display_name' or better: individual elements (comma separated) of the 'display_name' to display them in the result marker/popup

I see that I first have to disable the default behaviour with 'defaultMarkGeocode: false' , right ?

var geocoder = L.Control.geocoder({ defaultMarkGeocode: false }) .on('markgeocode', function(e) { ---- some code --- }).addTo(map);

but due to my very limited scripting knowledge I don't know how to make a new popup with my own strings/variables (extracted from the OSM/Nominatim JSON results)


or can I go beyond the JSON results and can directly use the "adress tags" as listed here ? https://nominatim.openstreetmap.org/ui/details.html?osmtype=W&osmid=369929468&class=building https://nominatim.openstreetmap.org/ui/details.html?osmtype=W&osmid=225021671&class=building

Address Tags: (city) (place) (postcode) (housenumber)

simon04 commented 1 week ago

Use the htmlTemplate option of Nominatim:

L.Control.geocoder({
  geocoder: L.Control.Geocoder.nominatim({
    htmlTemplate: result => {
      return `${result.display_name} ${result.osm_type} ${result.osm_id}`;
    }
  }),
})
BlackBrix commented 1 week ago

thanks that works, (e.g. you can use result.display_name.split(", ") to access individual elements then)