回调函数中的Google Map Geocoded TypeError

Kev*_*vin 5 javascript google-maps google-maps-api-3

我有以下2个函数来拉入,地理编码和在谷歌地图中放置标记.

我不断得到一个TypeError: adds[i] is undefined,这当然导致地图的其余部分炸弹.

这是我的代码:

// Place Markers on the Map
var PlaceMarkers = function (iw, adds, gc) {
    var image = {url: "http://meatmysite.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>'; // <-----   TypeError: adds[i] is undefined
                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!');
                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, f) {
    var ret = '';
    gc.geocode({'address': add}, function(res, status) {
        if (status == 'OK') {
            f(res[0].geometry.location);
            console.log('Found Here: ' + ret.toString());
        }
    });
    return -1;
};
Run Code Online (Sandbox Code Playgroud)

演示返回数据添加

[
{
    "address": "1 My Street Gilbert, AZ 85234",
    "title": "My Title 1",
    "url": "http://www.myurl.com/",
    "mainphone": null,
    "mainemail": null,
    "content": "1 My Street<br />Gilbert, AZ 85234"
},
{
    "address": "2 My Street North Richland Hills, TX 76182",
    "title": "My Title 2",
    "url": null,
    "mainphone": null,
    "mainemail": null,
    "content": "2 My Street<br />North Richland Hills, TX 76182"
}
]
Run Code Online (Sandbox Code Playgroud)

geo*_*zip 1

一种选择是将完整的“地址”对象传递到函数中GetLatLng,然后从那里传递到其回调中(这样你就可以得到函数闭包):

// Get Lat/Lng Location
var GetLatLng = function (gc, add, f) {
    gc.geocode({
        'address': add.address
    }, function (res, status) {
        if (status == 'OK') {
            f(res[0].geometry.location, add);
        }
    });
};
Run Code Online (Sandbox Code Playgroud)

然后在回调中像这样使用它(您也可以仅将索引传递到数组中):

GetLatLng(gc, adds[i], function (pos, add) {
    if (pos) {
        var ipop = '<h1>' + add.title + '</h1>'; 
        if (!isBlank(add.url)) {
            ipop += '<a href="' + add.url + '" target="_blank">' + add.url + '</a><br />';
        }
        ipop += '<div class="map_item_content" id="mi_content' + i + '">' + add.content + '</div>';
        if (!isBlank(add.mainphone)) {
            ipop += '<br /><strong>Phone:</strong> <a href="tel:' + add.mainphone + '">' + add.mainphone + '</a>';
        }
        if (!isBlank(add.mainemail)) {
            ipop += '<br /><strong>Email:</strong> <a href="mailto:' + add.mainemail + '">' + add.mainemail + '</a>';
        }
        console.log('HEY NOW: ' + pos.toString() + ' - Location Found!');
        var mark = new google.maps.Marker({
            title: add.title,
            position: pos,
            map: map,
            icon: image,
            html: ipop
        });
        google.maps.event.addListener(mark, 'click', function () {
            iw.setContent(this.html);
            iw.open(map, this);
        });
    }
});
Run Code Online (Sandbox Code Playgroud)

概念证明小提琴

代码片段:

// Get Lat/Lng Location
var GetLatLng = function (gc, add, f) {
    gc.geocode({
        'address': add.address
    }, function (res, status) {
        if (status == 'OK') {
            f(res[0].geometry.location, add);
        }
    });
};
Run Code Online (Sandbox Code Playgroud)
GetLatLng(gc, adds[i], function (pos, add) {
    if (pos) {
        var ipop = '<h1>' + add.title + '</h1>'; 
        if (!isBlank(add.url)) {
            ipop += '<a href="' + add.url + '" target="_blank">' + add.url + '</a><br />';
        }
        ipop += '<div class="map_item_content" id="mi_content' + i + '">' + add.content + '</div>';
        if (!isBlank(add.mainphone)) {
            ipop += '<br /><strong>Phone:</strong> <a href="tel:' + add.mainphone + '">' + add.mainphone + '</a>';
        }
        if (!isBlank(add.mainemail)) {
            ipop += '<br /><strong>Email:</strong> <a href="mailto:' + add.mainemail + '">' + add.mainemail + '</a>';
        }
        console.log('HEY NOW: ' + pos.toString() + ' - Location Found!');
        var mark = new google.maps.Marker({
            title: add.title,
            position: pos,
            map: map,
            icon: image,
            html: ipop
        });
        google.maps.event.addListener(mark, 'click', function () {
            iw.setContent(this.html);
            iw.open(map, this);
        });
    }
});
Run Code Online (Sandbox Code Playgroud)
var geocoder = new google.maps.Geocoder();
var map;
var infoWindow = new google.maps.InfoWindow();

function initialize() {
  map = new google.maps.Map(
    document.getElementById("map_canvas"), {
      center: new google.maps.LatLng(37.4419, -122.1419),
      zoom: 13,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
  PlaceMarkers(infoWindow, adds, geocoder);
}
google.maps.event.addDomListener(window, "load", initialize);

// Place Markers on the Map
var PlaceMarkers = function(iw, adds, gc) {
  var bounds = new google.maps.LatLngBounds();
  var image = {
    url: "http://meatmysite.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], function(pos, add) {
      if (pos) {
        var ipop = '<h1>' + add.title + '</h1>'; // <-----   TypeError: adds[i] is undefined
        if (!isBlank(add.url)) {
          ipop += '<a href="' + add.url + '" target="_blank">' + add.url + '</a><br />';
        }
        ipop += '<div class="map_item_content" id="mi_content' + i + '">' + add.content + '</div>';
        if (!isBlank(add.mainphone)) {
          ipop += '<br /><strong>Phone:</strong> <a href="tel:' + add.mainphone + '">' + add.mainphone + '</a>';
        }
        if (!isBlank(add.mainemail)) {
          ipop += '<br /><strong>Email:</strong> <a href="mailto:' + add.mainemail + '">' + add.mainemail + '</a>';
        }
        console.log('HEY NOW: ' + pos.toString() + ' - Location Found!');
        var mark = new google.maps.Marker({
          title: add.title,
          position: pos,
          map: map,
          // icon: image,
          html: ipop
        });
        bounds.extend(mark.getPosition());
        map.fitBounds(bounds);
        google.maps.event.addListener(mark, 'click', function() {
          iw.setContent(this.html);
          iw.open(map, this);
        });
      }
    });
  }
};
// Get Lat/Lng Location
var GetLatLng = function(gc, add, f) {
  gc.geocode({
    'address': add.address
  }, function(res, status) {
    if (status == 'OK') {
      f(res[0].geometry.location, add);
    }
  });
};

var adds = [{
  "address": "1 My Street Gilbert, AZ 85234",
  "title": "My Title 1",
  "url": "http://www.myurl.com/",
  "mainphone": null,
  "mainemail": null,
  "content": "1 My Street<br />Gilbert, AZ 85234"
}, {
  "address": "2 My Street North Richland Hills, TX 76182",
  "title": "My Title 2",
  "url": null,
  "mainphone": null,
  "mainemail": null,
  "content": "2 My Street<br />North Richland Hills, TX 76182"
}];

function isBlank(str) {
  return (!str || /^\s*$/.test(str));
}
Run Code Online (Sandbox Code Playgroud)