javascript 如何正确使用computeDistanceBetween()?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6754085/
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
How to use computeDistanceBetween() correctly?
提问by celsowm
I am newbie in Maps API, i am trying get the distance (in Km) from 2 adress. What is wrong with this code?
我是 Maps API 的新手,我正在尝试从 2 个地址获取距离(以公里为单位)。这段代码有什么问题?
var mygc = new google.maps.Geocoder();
var locationOrigem;
var locationDestino;
var latOrigem = 0;
var longOrigem = 0;
var latDestino = 0;
var longDestino = 0;
mygc.geocode({'address' : 'Presidente Vargas 897, Centro RJ'}, function(results, status){
locationOrigem = results[0].geometry.location;
latOrigem = results[0].geometry.location.lat();
longOrigem = results[0].geometry.location.lng();
});
mygc.geocode({'address' : 'Abelardo Bueno 3000, Barra da Tijuca RJ'}, function(results, status){
locationDestino = results[0].geometry.location;
latDestino = results[0].geometry.location.lat();
longDestino = results[0].geometry.location.lng();
});
alert(google.maps.geometry.spherical.computeDistanceBetween(locationOrigem, locationDestino));
By a unknown reason, var locationOrigem and locationDestinoare "undefined" outside of anonymous functions ??? Why???
由于未知原因,var locationOrigem 和 locationDestino在匿名函数之外是“未定义的”???为什么???
回答by Ruslan Polutsygan
Have you included this script?
你有没有包含这个脚本?
<script type="text/javascript" src="http://maps.google.com/maps/api/js?libraries=geometry&sensor=false&language=en"></script>
What happen when you run your code? Are you sure that your addresses were geocoded correctly? Are you sure that problem is in computeDistanceBetween
. Try to dump all variables.
运行代码时会发生什么?您确定您的地址已正确进行地理编码吗?你确定问题出在computeDistanceBetween
. 尝试转储所有变量。
UPDATE:
更新:
Requests to the Geocoder are executed asynchronously. So request for distance calculation were run before you had got geocoded data. That is why your variables were undefined
. You need to use callbacks to ensure that request is finished. Try this code
对地理编码器的请求是异步执行的。因此在您获得地理编码数据之前运行距离计算请求。这就是为什么你的变量是undefined
. 您需要使用回调来确保请求完成。试试这个代码
var mygc = new google.maps.Geocoder();
var locationOrigem;
var locationDestino;
var latOrigem = 0;
var longOrigem = 0;
var latDestino = 0;
var longDestino = 0;
mygc.geocode({'address' : 'Presidente Vargas 897, Centro RJ'}, function(results, status){
locationOrigem = results[0].geometry.location;
latOrigem = results[0].geometry.location.lat();
longOrigem = results[0].geometry.location.lng();
mygc.geocode({'address' : 'Abelardo Bueno 3000, Barra da Tijuca RJ'}, function(results, status){
locationDestino = results[0].geometry.location;
latDestino = results[0].geometry.location.lat();
longDestino = results[0].geometry.location.lng();
console.log(locationOrigem);
console.log(locationDestino);
console.log(google.maps.geometry.spherical.computeDistanceBetween(locationOrigem, locationDestino));
});
});
Also you need to check status of response. I haven't include because it is not the subject.
您还需要检查响应状态。我没有包括,因为它不是主题。