Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Waiting for google maps geocoder?

geo = function(options){
    geocoder.geocode( options, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var x = results;
            alert('pear');
            return x;
        } else {
            return -1;
            }
        });
    }

getAddr = function(addr){
    if(typeof addr != 'undefined' && addr != null) {
        var blah = geo({ address: addr, });
                    alert('apple');
                    return blah;
    }
    return -1;
}

So when I call getAddr I get undefined, also apple is alerted first and then pear. I realize that google maps geocodes asynchronously, but is there a way to make this work?

like image 409
Derek Avatar asked Feb 19 '12 00:02

Derek


People also ask

What is Geocoder map?

android.location.Geocoder. A class for handling geocoding and reverse geocoding. Geocoding is the process of transforming a street address or other description of a location into a (latitude, longitude) coordinate.

Is Google reverse geocoding free?

The Geocoding API uses a pay-as-you-go pricing model.

What is Geocoder Web service?

The Geocoding API is a service that provides geocoding and reverse geocoding of addresses. This service is also available as part of the client-side Google Maps JavaScript API, or for server-side use with the Java Client, Python Client, Go Client and Node. js Client for Google Maps Services.


1 Answers

You will not be able to do it that way. You have an asynchronous call to google's geocoder, which means you will not be able to have the getAddr return the results. Instead you should do something like this:

getAddr = function(addr, f){
    if(typeof addr != 'undefined' && addr != null) {
        geocoder.geocode( { address: addr, }, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            f(results);
          }
        });
    }
    return -1;
}

And then you use in your code like that:

getAddr(addr, function(res) {
  // blah blah, whatever you would do with 
  // what was returned from getAddr previously
  // you just use res instead
  // For example:
  alert(res);
});

EDIT: If you want to you could also add more status validation:

getAddr = function(addr, f){
    if(typeof addr != 'undefined' && addr != null) {
        geocoder.geocode( { address: addr, }, function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            f('ok', results);
          } else {
            f('error', null);
          }
        });
    } else {
      f('error', null);
    }
}

And you can use it like that:

getAddr(addr, function(status, res) {
  // blah blah, whatever you would do with 
  // what was returned from getAddr previously
  // you just use res instead
  // For example:
  if (status == 'ok') {
    alert(res);
  } else {
    alert("Error")
  }
});
like image 191
mck Avatar answered Oct 06 '22 20:10

mck