【问题标题】:Google Geocoder + google maps adding marker谷歌地理编码器+谷歌地图添加标记
【发布时间】:2012-05-21 10:31:18
【问题描述】:

我想要做的是在地理编码器完成解析为 latlng 时添加一个标记...这是我的代码:

function parseLocation() {
    var user1Location = "Mechelen, Belgium";
    var geocoder = new google.maps.Geocoder();
    //convert location into longitude and latitude
    geocoder.geocode({
        address: user1Location
    }, function (locResult) {
        lat1 = locResult[0].geometry.location.lat();
        lng1 = locResult[0].geometry.location.lng();
        if (status == google.maps.GeocoderStatus.OK) {
            $('#map_canvas').bind('init', function () {
                $('#map_canvas').gmap('addMarker', {
                    'position': lat1 + ',' + lng1
                }).click(function () {
                    $('#map_canvas').gmap('openInfoWindow', {
                        'content': lat1 + ',' + lng1
                    }, this);
                });
            });
        }
    });
}

当我警告初始化函数中的变量未定义时,我知道此地理编码器是异步的,但我正在等待状态正常...知道我做错了什么吗?

【问题讨论】:

    标签: javascript jquery google-maps geocoding


    【解决方案1】:

    试试看。地理编码器是异步的,但多次调用回调函数。

    geocoder.geocode({"address":user1Location}, function(locResult, status) {
         if (status == google.maps.GeocoderStatus.OK) {
            lat1 = locResult[0].geometry.location.lat();
            lng1 = locResult[0].geometry.location.lng();
         }
    });
    

    这就是在同一代码中的链接。

    http://jsfiddle.net/2jbTX/2/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-07-30
      • 1970-01-01
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      • 1970-01-01
      • 2020-10-12
      • 1970-01-01
      相关资源
      最近更新 更多