Search code examples
mapquest

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


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?


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.