【问题标题】:Object has no method 'getPosition' when clustering Google map markers (api v3)聚类 Google 地图标记时,对象没有方法“getPosition”(api v3)
【发布时间】:2012-08-11 16:57:48
【问题描述】:

我正在使用Google Maps Utility Library v3 在 Google 地图上尝试聚类标记的基本实现。

但是,当我运行它时,我在 Chrome 开发者工具控制台上收到一个错误:

Uncaught TypeError: Object #<Object> has no method 'getPosition'

这与实用程序库脚本中的第 649 行有关:http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/src/markerclusterer.js。以下是哪个函数:

/**
 * Determins if a marker is contained in a bounds.
 *
 * @param {google.maps.Marker} marker The marker to check.
 * @param {google.maps.LatLngBounds} bounds The bounds to check against.
 * @return {boolean} True if the marker is in the bounds.
 * @private
 */
MarkerClusterer.prototype.isMarkerInBounds_ = function(marker, bounds) {
  return bounds.contains(marker.getPosition());
};

我使用的代码是相当标准的谷歌地图,主要功能是:

function initialize(items,loop,zoom) {
  geocoder = new google.maps.Geocoder();
  if (items.length > 0) {
    var latlng = new google.maps.LatLng(items[0].Lat, items[0].Lng);
    var myOptions = {
      zoom: zoom,
      center: latlng,
      //mapTypeControl: false,
      streetViewControl: false,
      mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map(document.getElementById("map"), myOptions);
    map.setOptions({styles: stylez});

    for (var i = 0; i < items.length; i++) {
      var marker = new google.maps.Marker({
        position: new google.maps.LatLng(items[i].Lat, items[i].Lng),
        title: items[i].Title,
        icon: _iconCenter,
        shadow: shadow,
        infocontent: items[i].Description
      });
      marker.setMap(map);
      markersArray.push(marker);
    }
    var markerCluster = new MarkerClusterer(map, items);
    google.maps.event.addListener(map, "tilesloaded", function () {
      if(loop == true){
        SetLoop();
      }
    });
  }
}

据我所知,我已经追溯了错误函数,它应该接收地图边缘的坐标以便能够确定边界,这应该只是标准行为,但显然有些地方不对。

我想知道是否有人可以对此有所了解?

感谢大家的指点...

【问题讨论】:

    标签: google-maps google-maps-api-3 google-maps-markers markerclusterer


    【解决方案1】:

    事实证明,我在 google maps 脚本之前声明了 markerclusterer 脚本。调用地图脚本首先解决了它......现在很明显!

    【讨论】:

    • 您的帖子中没有足够的信息来帮助您。
    • 事后看来你是对的,但我没有意识到这是问题所在 - 我认为问题出在我的函数或集群库上。
    • 我怀疑源顺序是问题,我在 api 脚本之前包含了我的 markerCluster.js 文件,就像它在the docs 中所说的那样,一切正常。可能有不同的问题。
    【解决方案2】:

    MarkerClusterer 需要一组标记。您创建一个,但将 items 数组传递给它的构造函数。变化:

    var markerCluster = new MarkerClusterer(map, items);
    

    到:

    var markerCluster = new MarkerClusterer(map, markersArray);
    

    【讨论】:

    • 谢谢 - 这很可能引起了问题,所以感谢您指出。更改它并不会影响原始错误,即使在函数中使用 'markersArray' 仍然会发生。我实际上已经找到了原始问题的解决方案,并在原始帖子中添加了答案。再次感谢您的帮助 - 这可能是我的下一个问题;)
    猜你喜欢
    • 2012-12-29
    • 1970-01-01
    • 2012-05-30
    • 2011-07-05
    • 2011-03-06
    • 2011-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多