Google map computeDistanceBetween() 返回 NaN 没有错字

Google map computeDistanceBetween() returning NaN no typo

下面有这段代码,我正在尝试执行 computeDistanceBetween 函数,但它返回的是 NaN。没有拼写错误。我真的不知道发生了什么。请帮忙!

<html>
<head></head>
<body>
<div id ="VP"><button onClick="getAddressFromCoord('Port-Louis')">1</button>
</div>
<div id ="VP"><button onClick="findNearestEmpl()">2</button></div>
<script>
 var emplcoordinates= [];
 var empDistance=[];
       function getAddressFromCoord(Address){
    var coordinate;
 
    geocoder = new google.maps.Geocoder();
    var address = 'port louis';
       geocoder.geocode({
       'address': address
    }, function (results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
           coordinate = results[0].geometry.location.lat()+','+results[0].geometry.location.lng();
     emplcoordinates.push(new google.maps.LatLng(coordinate));
     console.log(emplcoordinates);
        } else {
            coordinate = 0;
   emplcoordinates.push(new google.maps.LatLng(coordinate));
   console.log(emplcoordinates);
        }
    });
 
}

function findNearestEmpl(){
 var checkpoint = new google.maps.LatLng(-20.244299955429746,57.459360252793886);
 for(i=0;i<emplcoordinates.length;i++)
 {
  empDistance.push( google.maps.geometry.spherical.computeDistanceBetween(checkpoint,emplcoordinates[i]));
  console.log('distance1 ='+empDistance[i])
 }
 
}
</script>

</body>
</html>

运行 按钮 1 然后按钮 2

像下面这样把经纬度转成字符串好像不行:

coordinate = results[0].geometry.location.lat()+','+results[0].geometry.location.lng();
emplcoordinates.push(new google.maps.LatLng(coordinate));

这对我有用,但是:

emplcoordinates.push(new google.maps.LatLng(results[0].geometry.location.lat(), results[0].geometry.location.lng()));

更简单,如果没有特殊原因需要创建一个单独的新 LatLng 对象:

emplcoordinates.push(results[0].geometry.location);

我得到的输出是:

distance1 =10263.42066184296