javascript 无法让谷歌地图computeDistanceBetween()返回一个值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6811022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't get google maps computeDistanceBetween() to return a value
提问by slapee
The computeDistanceBetween()
function in google maps geometry library will not return a value for me. Using the alert
function it says the distance is "[object, Object]". Can anyone see where I'm going wrong? here are the important parts of the code in question:
computeDistanceBetween()
谷歌地图几何库中的函数不会为我返回值。使用该alert
函数,它表示距离是“[object, Object]”。谁能看到我哪里出错了?以下是相关代码的重要部分:
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?sensor=false&libraries=geometry"></script>
<script type="text/javascript" >
var myArray1= [['location1', lat1, lng1], ['location2', lat2, lng2], ...];
var myArray2= [['locationA', latA, lngA], ['locationB', latB, lngB], ...];
var arrays = [myArray1, myArray2];
function codeStart() {
var orig;
var startAddress = document.getElementById("start").value;
geocoder.geocode( { 'address': startAddress}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var i = 0;
var input = results[0].geometry.location;
while (i < arrays.length) {
orig = closest(input, arrays[i]);
}
}
});
}
function closest(latlng, array){
var distance;
var c = 0;
while (c < array.length){
var location = array[c];
var locationlatlng = new google.maps.LatLng(location[1],location[2]);
distance = new google.maps.geometry.spherical.computeDistanceBetween(latlng, locationlatlng);
alert(distance); // popup box says "[object, Object]"
c++;
}
}
</script>
回答by Trott
computeDistanceBetween
is a static method. So this line:
computeDistanceBetween
是一个静态方法。所以这一行:
distance = new google.maps.geometry.spherical.computeDistanceBetween(latlng, locationlatlng);
should be this instead:
应该是这样的:
distance = google.maps.geometry.spherical.computeDistanceBetween(latlng, locationlatlng)
By the way, when alert()
tells you that something is an object, it's a good time to switch to console.dir()
instead of alert()
so you can (at least in some browsers) look at the contents of the object in the console/dev tools. If you don't know much about your JavaScript console, check it out. It will save you tons of time.
顺便alert()
说一句,当告诉您某物是一个对象时,现在是切换到console.dir()
而不是alert()
这样的好时机,这样您就可以(至少在某些浏览器中)在控制台/开发工具中查看对象的内容。如果您不太了解 JavaScript 控制台,请查看它。它将为您节省大量时间。
回答by Lightness Races in Orbit
distance = new google.maps.geometry.spherical.computeDistanceBetween(latlng, locationlatlng);
For some reason you've used the syntax to create a new object. That is why, when you alert(distance)
, you're seeing that distance
is an object.
出于某种原因,您使用语法创建了一个新对象。这就是为什么,当你alert(distance)
,你看到那distance
是一个对象。
computeDistanceBetween
is just a function:
distance = google.maps.geometry.spherical.computeDistanceBetween(latlng, locationlatlng);