[英]Problems with async geocoder.geocode
我在获取地址位置时遇到问题。 但是,此问题仅在我第一次运行时出现,即,每次我查询时,都必须单击两次,仅获得第二个值。
我相信问题是由于异步方法。 但是我无法解决问题。 他的一些朋友可以帮助我。
$('#btnTracar').click(function(){
if (geocoder){
geocoder.geocode({ 'address': document.getElementById('txtStart').value }, function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
mapStart = results[0].geometry.location;
} else { alert("Não foi possível carregar a localização. \nDescrição do Erro: " + status); }
});
geocoder.geocode({ 'address': document.getElementById('txtEnd').value }, function(results, status){
if (status == google.maps.GeocoderStatus.OK) {
mapEnd = results[0].geometry.location;
} else { alert("Não foi possível carregar a localização. \nDescrição do Erro: " + status); }
});
calcularRota();
}
});
解:
$('#btnTracar').click(function(){
if ($.trim($("#txtStart").val()) == ""){
alert("Favor preencher o campo de Origem Corretamente.");
return;
}
if ($.trim($("#txtEnd").val()) == ""){
alert("Favor preencher o campo de Origem Corretamente.");
return;
}
if (geocoder){
geocoder.geocode({ 'address': document.getElementById('txtStart').value }, function(results, status){
if (status == google.maps.GeocoderStatus.OK){
mapStart = results[0].geometry.location;
geocoder.geocode({ 'address': document.getElementById('txtEnd').value }, function(results, status){
if (status == google.maps.GeocoderStatus.OK){
mapEnd = results[0].geometry.location;
calcularRota();
}
});
}
});
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.