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.
Best Answer
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 injavascript
) :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
: