【问题标题】:Google Maps GeoCode Callback Parameter errorGoogle Maps GeoCode 回调参数错误
【发布时间】:2015-10-01 22:19:55
【问题描述】:

我已经阅读、阅读和阅读更多内容,结果我想出了以下内容:

// Place Markers on the Map
var PlaceMarkers = function (iw, adds, gc) {
    var image = {url: "http://mywebstte.com/Images/star2.png", size: new google.maps.Size(24, 24)};
    var aCt = adds.length;
    for(var i = 0; i < aCt; ++i) {
        GetLatLng(gc, adds[i].address, function(pos){
            if(pos){
                var ipop = '<h1>' + adds[i].title + '</h1>';
                if(!isBlank(adds[i].url)){
                    ipop += '<a href="' + adds[i].url + '" target="_blank">' + adds[i].url + '</a><br />';
                }
                ipop += '<div class="map_item_content" id="mi_content' + i + '">' + adds[i].content + '</div>';
                if(!isBlank(adds[i].mainphone)){
                    ipop += '<br /><strong>Phone:</strong> <a href="tel:'+adds[i].mainphone+'">' + adds[i].mainphone + '</a>';
                }
                if(!isBlank(adds[i].mainemail)){
                    ipop += '<br /><strong>Email:</strong> <a href="mailto:'+adds[i].mainemail+'">' + adds[i].mainemail + '</a>';
                }
                console.log('HEY NOW: ' + pos.toString() + ' - Location Found!'); // Never displays, as a result neither do the markers
                var mark = new google.maps.Marker({title: adds[i].title, position: pos, map: map, icon: image, html: ipop});            
                google.maps.event.addListener(mark, 'click', function(){
                    iw.setContent(this.html);
                    iw.open(map, this);
                });
            }
        });
    }
};
// Get Lat/Lng Location
var GetLatLng = function(gc, add, callback) {
    var ret = '';
    gc.geocode({'address': add}, function(res, status) {
        if (status == 'OK') {
            ret = res[0].geometry.location;
            console.log('Found Here: ' + ret.toString()); // Always displays
        } else {
            ret = null;
            console.log('Nothing Found For: ' + add);
        }
    });
    callback(ret);
};

现在,在 GetLatLng 中,我的控制台将显示通过地址找到的 lat/lng。我已经读到我需要回调,因为这些调用是异步的,但是,即使在使用回调之后 PlaceMarkers 仍然是空白的并且不会放置标记。

编辑到 GetLatLng

// Get Lat/Lng Location
var GetLatLng = function(gc, add, f) {
var ret = '';
gc.geocode({'address': add}, function(res, status) {
    if (status == 'OK') {
        f(res[0].geometry.location);
        console.log('Found Here: ' + ret.toString()); // Always displays
    }
});
return -1;
};

现在返回,但 TypeError: adds[i] is undefined 出现错误

【问题讨论】:

标签: javascript google-maps


【解决方案1】:

您需要在可用的地理编码器回调中使用数据:

// Get Lat/Lng Location
var GetLatLng = function(gc, add, callback) {
    var ret = '';
    gc.geocode({'address': add}, function(res, status) {
        if (status == 'OK') {
            ret = res[0].geometry.location;
            console.log('Found Here: ' + ret.toString()); // Always displays
        } else {
            ret = null;
            console.log('Nothing Found For: ' + add);
        }
        callback(ret);
    }  /* end of geocoder callback function */ );
};

【讨论】:

  • :) 谢谢,你一定在我编辑的时候发布了。我将在 7 分钟内重新编辑并接受 S.O.让我等待
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-07-22
  • 1970-01-01
相关资源
最近更新 更多