-1

I am using Google's maps API to geocode two addresses. I defer the returned results and use a $.when().then() method to execute my logic once I get the coordinates for the string addresses. The problem is the API always returns the result as resolved, even if there is an error. For example if there is no internet connection instead of request timing out I get the status as ERROR and result as null or if I enter an invalid address I get the status ZERO_RESULTS and result an empty array []. As I am only interested in getting the proper coordinate results I want to handle all other responses as a geocoding error, which I don't know how to do. You can also see that I have to check if input fields are empty before geocoding because of the same problem.

I am just getting acquainted to asynchronous flow and need some guidance. I am using jquery 1.9.2 and google maps APIv3. (I can hard code all the conditions but I want to improve my coding skills and have something more generic. Is that possible.)

I will give my code here as well.

function geocode(addString) {
 var deferred = $.Deferred();
 var geocoder = new google.maps.Geocoder();
 var request = {
  address: addString
 };
 geocoder.geocode(request, function(results, status) {
  if (status === google.maps.GeocoderStatus.OK) {
    deferred.resolve(results[0].geometry.location);
  }
  else {
    // no idea what to do here
  }
});
 return deferred;
}

function Options() {
 var origin = $("#origin-field").val();
 var destination = $("#destination-field").val();
 if (origin != "" && destination != ""){
  var originCoords = geocode(origin);
  var destinationCoords = geocode(destination);
  $.when(originCoords, destinationCoords)
  .then(function(originCoordinates, destinationCoordinates) {
    console.log(originCoordinates.lat() + ',' + originCoordinates.lng());
    console.log(destinationCoordinates.lat() + ',' + destinationCoordinates.lng());
 }, function() {
    toastMessage("Geo-coding error");
  });
 }
 else {
  toastMessage("Origin and/or Destination missing");
 }
}
geocodezip
  • 158,664
  • 13
  • 220
  • 245
Arsalan Ahmed
  • 383
  • 2
  • 3
  • 14
  • 1
    You can check the list of possible status codes in documentation: https://developers.google.com/maps/documentation/javascript/reference#GeocoderStatus – xomena Oct 31 '16 at 20:19

1 Answers1

0

I solved my problem thanks to this example. As I said I just started looking at asynchronous flow so didn't know how to solve this simple problem. What I did is just catch all non-OK statuses in an else block and passed it to the deferred.reject() method. So my code became like this.

function geocode(addString) {
 var deferred = $.Deferred();
 var geocoder = new google.maps.Geocoder();
  var request = {
   address: addString
  };
  geocoder.geocode(request, function(results, status) {
   if (status === google.maps.GeocoderStatus.OK) {
     deferred.resolve(results[0].geometry.location);
   }
   else {
    deferred.reject(status);
   }
 });
 return deferred;
 }

function Options() {
 var origin = $("#origin-field").val();
 var destination = $("#destination-field").val();
 var originCoords = geocode(origin);
 var destinationCoords = geocode(destination);
 $.when(originCoords, destinationCoords)
 .then(function(origin, destination) {
    //some logic in case of success
    }, function(status) {
  toastMessage("Geo-coding error:" + status);
 });
}
Arsalan Ahmed
  • 383
  • 2
  • 3
  • 14