【发布时间】:2015-12-10 15:11:23
【问题描述】:
我有两个隐藏字段,其中包含纬度值和经度值,id-#latitude_val and #longitude_val。我将使用初始纬度和经度设置变量chicago。如果以上两个字段为空,我将传递地名(此处为静态检查)并通过以下代码定位标记。
function initialize() {
chicago = new google.maps.LatLng(51.508742,-0.120850);//default value
if(($("#latitude_val").val().length >3) || ($("#longitude_val").val().length>3))
{
chicago = new google.maps.LatLng($("#latitude_val").val(), $("#longitude_val").val());
}
else
{
geocoder = new google.maps.Geocoder();
geocoder.geocode( { 'address': 'Abudhabi'}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK)
{
console.log(results[0].geometry.location.lat()+' '+results[0].geometry.location.lng());//if null this should print first
chicago = new google.maps.LatLng(results[0].geometry.location.lat(),results[0].geometry.location.lng());
console.log('__'+chicago);//this should print second
}
else
{
console.log("Geocode was not successful for the following reason: " + status);
}
});
}
console.log('****'+chicago);//this should print third
var mapOptions = { zoom:4, mapTypeId: google.maps.MapTypeId.ROADMAP, center: chicago }
map = new google.maps.Map(document.getElementById("googlemap"), mapOptions);
var marker=new google.maps.Marker({
position:chicago,
map:map,
draggable:true,
animation: google.maps.Animation.DROP
});
marker.setMap(map);
});
}
我已经检查了chicago的值。这里默认值由地图中的标记标记。当我检查控制台时,第三个是第一个打印,第一个在第二个,第二个在第三个.我不明白为什么会这样。我需要根据顺序运行它。
【问题讨论】:
-
请提供一个Minimal, Complete, Tested and Readable example 来证明这个问题。
-
地理编码器是异步的,您的 cmets 指示您希望何时打印各种值不正确。
-
@geocodezip 如何制作同步地理编码器??一切都很好,我只想让变量按代码顺序获取值。
-
@geocodezip 我不明白你为什么说打印的值不正确
-
因为你说的东西会先打印第三次,所以会先打印。
标签: google-maps google-maps-api-3