【问题标题】:Google Maps geocoding and markers in loop谷歌地图地理编码和循环标记
【发布时间】:2010-11-06 11:24:53
【问题描述】:

我在这里完全感到困惑。我有一个对象列表,每个对象都包含一个位置。我使用 google.maps.geocoder 查找此位置,然后在地图上为该位置放置一个标记。

但由于某种原因,只出现了一个标记。我想这与我在其他线程中看到的闭包问题有关,但我似乎无法将解决方案应用于我所拥有的。

我的代码如下:

var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
  map.fitBounds(bounds);

  for (var item in list) {
    var geocoder = new google.maps.Geocoder();
    var geoOptions = {
      address: item.location,
      bounds: bounds,
      region: "NO"
    };
    geocoder.geocode(geoOptions, function(results, status) {
      if (status == google.maps.GeocoderStatus.OK) {
        addMarker(map, item, results[0].geometry.location);
      } else {
        console.log("Geocode failed " + status);
      }
    });
  }

function addMarker(map, item, location) {
  var marker = new google.maps.Marker({ map : map, position : location});
  marker.setTitle(item.title);
  var infowindow = new google.maps.InfoWindow( {
    content : item.body,
    size : new google.maps.Size(100, 300)
  });
  (function(map, marker) {
    new google.maps.event.addListener(marker, "click", function() {
      infowindow.open(map, marker);
    });
    })(map, marker);
  }

感谢任何帮助。

更新:为了避免第一个答案中建议的循环闭包,我已将代码更改为:

//This is the entry
function codeLocations(list, map) {
  for (var i = 0; i < list.length; i++) {
    console.log("Looping " + list[i].location);
    var geocoder = new google.maps.Geocoder();
    var geoOptions = {
      address: list[i].location,
      bounds: getBounds(),
      region: "NO"
    };
    geocoder.geocode(geoOptions, createGeocodeCallback(list[i], map));
  }
}

function createGeocodeCallback(item, map) {
  console.log("Generating geocode callback for " + item.location);
  return function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
      console.log("Geocoding " + item.location + " OK");
      addMarker(map, item, results[0].geometry.location);
    } else {
      console.log("Geocode failed " + status);
    }
  }
}

function addMarker(map, item, location) {
  console.log("Setting marker for " + item.location + " (location: " + location + ")");
  var marker = new google.maps.Marker({ map : map, position : location});
  marker.setTitle(item.title);
  var infowindow = new google.maps.InfoWindow( {
    content : item.body,
    size : new google.maps.Size(100, 300)
  });
  new google.maps.event.addListener(marker, "click", function() {
    infowindow.open(map, marker);
  });
}

根据日志语句,我现在在正确的位置拥有正确的对象,这意味着每次设置标记时,项目和位置对象都不同,但我的地图上仍然只有一个标记。这怎么可能?

【问题讨论】:

    标签: javascript google-maps google-maps-markers


    【解决方案1】:

    不要在循环中创建闭包。 That just won't work。这可能是问题的解决方案:

      function callback() {
        return function(results, status) {
          if (status == google.maps.GeocoderStatus.OK) {
            addMarker(map, item, results[0].geometry.location);
          } else {
            console.log("Geocode failed " + status);
          }
        };
      }
    
      for (var item in list) {
        var geocoder = new google.maps.Geocoder();
        var geoOptions = {
          address: item.location,
          bounds: bounds,
          region: "NO"
        };
        geocoder.geocode(geoOptions, callback());
      }
    

    【讨论】:

    • 谢谢。我已经更新了代码以避免这种情况,但它似乎并没有解决问题:-/
    • @fiskeben,你能给我们一个在线的例子吗,这样调试起来容易很多
    • 它在 Chrome 9.x、Firefox 3.x 和 IE9 上运行良好,当然启用了控制台 - 顺便说一句,你把地图居中了;)
    • @fiskeben,是的,这是一个截图:tinyimg.org/images/911sample.png。你用的是什么浏览器?
    • 天哪,我现在明白了。我什么时候才能学会在字里行间阅读? :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多