Javascript 为什么谷歌地图地理编码器不调用初始化?
我试图让谷歌地图地理编码器从一个地址返回一个LatLng,然后用该LatLng在中心初始化地图 我看到了一些关于这个主题的问题,建议首先用任意中心初始化地图,然后重新居中,但这似乎是浪费 下面的代码工作正常,直到我将全局lat和lon更改为零。然后使用地址调用geocder,并返回LatLng。然后我得到的只是一个空白窗口,初始化函数中的警报永远不会触发 在我开始在0,0上初始化然后居中之前,有人能解释一下为什么这不起作用吗 谢谢Javascript 为什么谷歌地图地理编码器不调用初始化?,javascript,google-maps,google-maps-api-3,Javascript,Google Maps,Google Maps Api 3,我试图让谷歌地图地理编码器从一个地址返回一个LatLng,然后用该LatLng在中心初始化地图 我看到了一些关于这个主题的问题,建议首先用任意中心初始化地图,然后重新居中,但这似乎是浪费 下面的代码工作正常,直到我将全局lat和lon更改为零。然后使用地址调用geocder,并返回LatLng。然后我得到的只是一个空白窗口,初始化函数中的警报永远不会触发 在我开始在0,0上初始化然后居中之前,有人能解释一下为什么这不起作用吗 谢谢 var lat = 37.425593; var lon = -
var lat = 37.425593;
var lon = -122.075915;
var address = '1600 Amphitheatre Pky, Mountain View, CA';
var LatLon = new google.maps.LatLng(0, 0);
function initialize() {
alert("2. "+LatLon);
var mapOptions = {
center: LatLon,
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var marker = new google.maps.Marker({
position: LatLon,
map: map
});
}
if (lat == 0 && lon == 0) {
alert('address = '+address);
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
LatLon = results[0].geometry.location;
alert("1. "+LatLon);
google.maps.event.addDomListener(window, 'load', initialize);
} else {
alert("No results found");
}
} else {
alert("Geocoder failed: " + status);
}
});
} else {
alert('lat/lon = '+lat+' '+lon);
LatLon = new google.maps.LatLng(lat, lon);
alert("1. "+LatLon);
google.maps.event.addDomListener(window, 'load', initialize);
}
地理编码器是异步的。您尚未将代码放入上下文中,但返回坐标所需的时间必须意味着当坐标从服务器返回时,窗口加载事件已经触发,因此初始化函数永远不会运行。如果使用onload事件启动geocode操作,或者如果直接调用initialize并将代码放在页面底部,以便在页面完全呈现(并且地图有一个大小)之前不会执行代码,则该操作将有效
var地址='加利福尼亚州山景城1600圆形剧场Pky';
var LatLon=new google.maps.LatLng(0,0);
函数初始化(LatLon){
变量映射选项={
中心:拉特伦,
缩放:14,
mapTypeId:google.maps.mapTypeId.ROADMAP
};
var map=new google.maps.map(document.getElementById('map-canvas'),mapOptions);
var marker=new google.maps.marker({
职位:拉特隆,
地图:地图
});
}
var geocoder=new google.maps.geocoder();
geocoder.geocode({'address':address},函数(结果,状态){
if(status==google.maps.GeocoderStatus.OK){
如果(结果[0]){
var LatLon=results[0]。geometry.location;
初始化(LatLon);
}否则{
警报(“未发现结果”);
}
}否则{
警报(“地理编码器失败:+状态);
}
});
可能存在的副本
<script type="text/javascript">
var address = '1600 Amphitheatre Pky, Mountain View, CA';
var LatLon = new google.maps.LatLng(0, 0);
function initialize(LatLon) {
var mapOptions = {
center: LatLon,
zoom: 14,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var marker = new google.maps.Marker({
position: LatLon,
map: map
});
}
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
var LatLon = results[0].geometry.location;
initialize(LatLon);
} else {
alert("No results found");
}
} else {
alert("Geocoder failed: " + status);
}
});
</script>
</body>