generic function to convert address to a latlng

38 views
Skip to first unread message

Gary Tan

unread,
Jun 10, 2010, 12:17:47 PM6/10/10
to Google Maps JavaScript API v3
Hi,

I am trying to create a generic function as below. no error reported,
but no mag shows up either. can somebody help?

Thanks a lot

function codeAddress(address) {
geocoder = new google.maps.Geocoder();
if (geocoder) {
geocoder.geocode({ 'address': address }, function(results,
status) {
if (status == google.maps.GeocoderStatus.OK) {
return results[0].geometry.location
}
else {
alert("Geocode was not successful for the
following reason: " + status);
};
})
}
};

var latlng = codeAddress('150 Springfield Blvd,Richmond,95037');

// if i change to below, everything look fine.
var latlng = new google.maps.LatLng(-34.397, 150.644);

Rossko

unread,
Jun 10, 2010, 12:25:38 PM6/10/10
to Google Maps JavaScript API v3
> I am trying to create a generic function as below. no error reported,
> but no mag shows up either. can somebody help?

Geocoding is asynchronous. You can't 'return' any value from its
callback function. You need to do whatever it is you want to do with
the results, inside the callback function.

http://code.google.com/apis/maps/documentation/javascript/services.html#Geocoding
Reply all
Reply to author
Forward
0 new messages