admin管理员组文章数量:1397102
I am newbie in Maps API, i am trying get the distance (in Km) from 2 adress. What is wrong with this code?
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.sphericalputeDistanceBetween(locationOrigem, locationDestino));
By a unknown reason, var locationOrigem and locationDestino are "undefined" outside of anonymous functions ??? Why???
I am newbie in Maps API, i am trying get the distance (in Km) from 2 adress. What is wrong with this code?
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.puteDistanceBetween(locationOrigem, locationDestino));
By a unknown reason, var locationOrigem and locationDestino are "undefined" outside of anonymous functions ??? Why???
Share Improve this question edited Jul 19, 2011 at 22:12 celsowm asked Jul 19, 2011 at 21:00 celsowmcelsowm 46410 gold badges41 silver badges64 bronze badges 1- See my updated post. Does it work for you now? – Ruslan Polutsygan Commented Jul 20, 2011 at 12:55
1 Answer
Reset to default 4Have you included this script?
<script type="text/javascript" src="http://maps.google./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 puteDistanceBetween
. Try to dump all variables.
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
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.puteDistanceBetween(locationOrigem, locationDestino));
});
});
Also you need to check status of response. I haven't include because it is not the subject.
本文标签: javascriptHow to use computeDistanceBetween() correctlyStack Overflow
版权声明:本文标题:javascript - How to use computeDistanceBetween() correctly? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744149422a2592991.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论