【问题标题】:Why does Google Maps Geocoder not call initialize?为什么谷歌地图地理编码器不调用初始化?
【发布时间】:2013-12-07 11:25:50
【问题描述】:

我正在尝试让 Google 地图地理编码器从地址返回 LatLng,然后以该 LatLng 为中心初始化地图。

我看到一些关于这个主题的问题,建议应该先用任意中心初始化地图,然后再重新设置,但这似乎很浪费。

在我将全局 lat 和 lon 更改为零之前,以下代码可以正常工作。然后使用地址调用 geocder 并返回 LatLng。然后我得到的只是一个空白窗口,并且永远不会触发初始化函数中的警报。

在我走在 0,0 初始化然后居中的路线之前,有人能解释一下为什么这不起作用吗?

谢谢

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);
}

【问题讨论】:

标签: javascript google-maps google-maps-api-3


【解决方案1】:

地理编码器是异步的。您还没有将代码放在上下文中,但是返回坐标所需的时间一定意味着窗口加载事件已经触发,当坐标从服务器返回时,因此初始化函数永远不会运行。如果您使用 onload 事件启动地理编码操作,或者如果您直接调用初始化并将代码放在页面底部,则它将起作用,直到页面完全呈现(并且地图具有大小) )。

<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>

working example

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-12-17
    • 2012-07-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多