How to determine if address cannot be geocoded in JavaScript using MapQuest Map API

StackOverflow https://stackoverflow.com/questions/14326613

  •  15-01-2022
  •  | 
  •  

Question

I need to geocode bunch of addresses and pin point them on the map using MapQuest. I am using

geocodeAndAddLocations

method to do that. The only problem I have is I can't figure out if geocoding for a certain address failed or not. Does anyone know how to determine that?

Was it helpful?

Solution

I have not tested this code, but according to MapQuest API, it would be something like this:

map.geocodeAndAddLocations([
      {street:"555 17th St", postalCode:"80202"}
], function (response) {
    if (response.results.length == 0) { // FAILED!!!
        alert("can't geocode location");
    } else { // SUCCESS
        var location;
        for (j=0; j<response.results[i].locations.length; j++) {
            location = response.results[i].locations[j];
            alert('lat: ' + location.latLng.lat + ', lng: ' + location.latLng.lng);
        }
    }
});

but again, having more of your code would give a better idea what exactly you are doing and not doing.

OTHER TIPS

very simplest way... use the following url in this jquery ajax to geocode. Even you are use mapquest This ajax returns the json object.

$.getJSON("http://nominatim.openstreetmap.org/reverse?format=json&lat="+lat+"&lon="+lon+"", function(data) {

             var  loc=data.display_name;    

                });

append your lat and lng in the url.if unable to get the location details it throws the "unable to geocode " so you easily fix your problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top