This is a static archive of our old OpenStreetMap Help Site. Please post any new questions and answers at community.osm.org.

How to use javascript to display marker for an address?

0

I have a map inside a Confluence wiki page, and using PocketQuery I get the addresses I want to display from my DB.

What's the most direct way to do the Nominatim request and the Leaflet marker placement?

I can generate code that looks like this:

    <script>
    #foreach($person in $result)
    $.getJSON('https://nominatim.openstreetmap.org/search/$person.strasse $person.hausnummer ,$person.plz?format=json', cbMarker);
    function cbMarker(data){
      L.marker([data.lat, data.lon], {title: '$person.name'}).addTo(map);
    }
    #end
    </script>
but something is wrong, it seems to crash the template and it falls back to just showing me $result as a table.

asked 06 Nov '15, 10:13

HeaDCase's gravatar image

HeaDCase
15113
accept rate: 0%

edited 14 Nov '15, 22:08

aseerel4c26's gravatar image

aseerel4c26 ♦
32.6k18248554

1

Is Conluence supposed to be Confluence?

(14 Nov '15, 08:51) scai ♦
(14 Nov '15, 16:32) aseerel4c26 ♦
1

Yeah, my typo there. Got it sorted in the meantime, the suggestion about using only the first element of the returned array is right on the money. Thanks!

(14 Nov '15, 20:58) HeaDCase

One Answer:

3

Your question is difficult to understand (what is Conluence and where does PocketQuery come into it - are we supposed to know?) but your JavaScript looks like it assumes that the Nominatim response will be a hash when indeed it will be an array of search results; you probably need something like data[0].lat instead of data.lat. Which will of course fail if the result set is empty.

answered 13 Nov '15, 23:00

Frederik%20Ramm's gravatar image

Frederik Ramm ♦
82.5k927201273
accept rate: 23%

Source code available on GitHub .