Problem getting distanceFrom in Google Map api
I try to get the distance between two points using google maps api,
function load_points(){
var geocoder = new GClientGeocoder();
var firstZip, secondZip;
geocoder.getLatLng(document.getElementById("firstZip").value, function(point) {
if (!point) {
alert("This point could not be geocoded");
} else {
firstZip = point;
var marker = new GMarker(point);
map.addOverlay(marker);
}
});
geocoder.getLatLng(document.getElementById("secondZip").value, function(point) {
if (!point) {
alert("This point could not be geocoded");
} e开发者_Python百科lse {
secondZip = point;
var marker = new GMarker(point);
map.addOverlay(marker);
}
});
alert(new GLatLng(firstZip).distanceFrom(new GLatLng(secondZip)));
}
the problem is when I try it seems first to be executed the alert and after that the geocoding part, and of course the distanceFrom method fails because the value of firstZip and secondZip is undefined. Someone knows how to solve it?
The getLatLng
function is asynchronous, so you need to wait for both calls to it to return successfully before you attempt to use the firstZip
and secondZip
variables.
精彩评论