2

I am using OpenStreeMap with Google as layer, and for geocoding , nominatim service. Below, is my code.

$.ajax({
url: "http://nominatim.openstreetmap.org/search?q=" + address + &format=json&polygon=0&addressdetails=1",
type: 'POST',
//data: { 'htmlViewType': htmlViewType },
complete: function () { },
success: function (result) {
    var obj = JSON.parse(result);
    if (obj.length > 0) {
        document.getElementById('LblError').innerHTML = obj[0].display_name;
        var markerslonLat = new OpenLayers.LonLat(obj[0].lon, obj[0].lat).transform(WGS84, map.getProjectionObject(), 0);
        map.setCenter(markerslonLat, 10);
        //var icon = new OpenLayers.Icon('http://www.openlayers.org/dev/img/marker.png', size, offset);
        var icon = new OpenLayers.Icon(' http://maps.google.com/mapfiles/ms/micons/blue.png', size, offset);
        markers.addMarker(new OpenLayers.Marker(markerslonLat, icon));
        map.addLayer(markers);
    }
    else {
        document.getElementById('LblError').innerHTML = 'no such address.';
    }
},
error: function (error) {
    document.getElementById('LblError').innerHTML = error;
}
});

Now, when i pass address = "Bharuch" in above URL , it returns 2 result in array, and when address = "Surat", it returns 5.

so, my question is : which result value should i take from that array result as original Result ?

Right , now i am taking result from 0 index, but in few case it don't show my actual address. Thanks.

1 Answer 1

1

You get multiple results because nominatim matches multiple results for your query. You can filter from the results based on the data fields that are returned in the JSON data set.

E.g. for your "Bharuch" address query you get 3 results - a county, a city and a town (the code is in python but the concept remains the same in javascript) :

import json
t=json.loads('[{"place_id":"1486447","licence":"Data \u00a9 OpenStreetMap contributors, .... 
for el in t:
...     print 'Type:' + el['type'] + ' display_name ' + el['display_name']
... 
Type:county display_name Bharūch, Surat, India
Type:city display_name Bharuch, Bharūch, Surat, India
Type:town display_name Bharuch, Bharūch, Surat, 392001, India

For the query above, if you're looking for the city, you need to pick the entry that has 'type'='city'. Or, as an alternative, you change your search query to "Bharuch city" and you will only get 1 result from nominatim:

import urllib
import json
params = urllib.urlencode({'q':'Bharuch city', 'format':'json','polygon':0,'addressdetails':1})
f = urllib.urlopen("http://nominatim.openstreetmap.org/search?%s" % params )
t = json.loads(f.read())
for el in t:
...    print 'Type:' + el['type'] + ' display_name ' + el['display_name']
...
Type:city display_name Bharuch, Bharūch, Surat, India
1
  • Hi diciu, Thanks for a quick reply. I will check as per your code. Thanks a lot.
    – Naresh
    Mar 11, 2013 at 7:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.