缩放级别的任何更改都会导致我的所有标记重新出现在我的Google地图上 [英] Any change in zoom level causes all my markers to re-appear on my Google map

查看:95
本文介绍了缩放级别的任何更改都会导致我的所有标记重新出现在我的Google地图上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

缩放级别的任何更改都会导致自上次加载页面以来随时出现在地图上的所有标记。无论缩放级别的变化是由于我在代码中调用setZoom()还是因为我操作了缩放滑块,都是如此。

Any change in zoom level causes all markers to appear on the map that have been on the map at any time since the last page load. This is true whether the zoom level change is due to a setZoom() call in my code or because I operate the zoom slider.

我有一个带按钮的地图控件添加对应于不同类别的标记。因此,您单击组的按钮,地图将用代表组的标记填充。然后,您单击个人按钮,组标记将被清除并删除,并且个别标记将出现在地图上。和其他类别一样。但是,缩放级别的任何更改都会返回自页面上次刷新后在地图上显示的所有标记。

I have a map control widget with buttons to add markers that correspond to different categories. So you click the button for Groups and the map populates with markers that represent Groups. Then you click on the Individuals button and the Groups markers are cleared and deleted and the Individual markers appear on the map. And so on with other categories. But any change in zoom level brings back any markers that have been on the map since the page was last refreshed.

我正在使用MarkerClustererPlus。我不知道这是否会成为MarkerClustererPlus,Google代码或我的代码中的一个错误。希望后者。我将在下面添加我的addMarkers函数。由于我在添加新标记之前清除并删除了标记,因此我不知道以前的标记返回的可能性如何,也不知道为什么缩放更改会触发它们的返回:

I'm using MarkerClustererPlus. I don't know whether this would be a bug in MarkerClustererPlus, in Google's code or in my code. Hopefully the latter. I'll include my addMarkers function below. Since I both clear and delete the markers before adding new markers, I don't know how it's possible for the previous markers to come back, nevermind why a zoom change trigger their return:

function addMarkers(map,flag) { 

  clearOverlays();
  deleteOverlays();

  if ('event' == flag) {
    data = himaps_event_data;
  } else if ('story' == flag) {
    data = himaps_story_data;
  } else if ('group' == flag) {
    data = himaps_group_data;
  } else {
    data = himaps_user_data;
  }

  for (var k in data) {
    var item = data[k];
    var latLng = new google.maps.LatLng(item.lat, item.lng);
    var marker = new google.maps.Marker({'position': latLng});
    markersArray.push(marker); 
  } 

  var markerCluster = new MarkerClusterer(map, markersArray);  
}

另外,如果我更改缩放比例,那么标记不会再清除。如果我点击按钮更改类别,它们只会继续累积在地图上。这绝对不会发生,如果我不改变缩放级别。

Also, if I change zoom, then the markers don't clear anymore. They just continue to accumulate on the map if I click the buttons to change category. That definitely doesn't happen if I don't change the zoom level.

根据要求,这里是更多的代码:

By request, here's more of the code:

function clearOverlays() {
  if (markersArray) { 
    for (var i = 0; i < markersArray.length; i++ ) {
      markersArray[i].setMap(null);
    }
  }
}

function deleteOverlays() {
  if (markersArray) {
    for (i in markersArray) {
      markersArray[i].setMap(null);
    }
    markersArray.length = 0;
  }
}

/* map controls */
(function($) {
  Drupal.behaviors.himaps = {
  attach: function(context, settings) {
    $('.map-controls button').click(function() {
      flag = this.id.replace('map-',''); 
      addMarkers(map,flag);
    });
  }
  }
})(jQuery);


/* homepage map */
function newMap(clat,clng) {
  if (!clat) {
    clat = 37.65;  
  }
  if (!clng) {
    clng = -97.43;  
  }
  var myOptions = {
    scrollwheel: false,
    mapTypeControl: false,
    mapTypeControlOptions: {
        style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR,
        position: google.maps.ControlPosition.LEFT_CENTER
    },
    panControl: false,
    panControlOptions: {
        position: google.maps.ControlPosition.LEFT_CENTER
    },
    zoomControlOptions: {
        style: google.maps.ZoomControlStyle.LARGE,
        position: google.maps.ControlPosition.LEFT_CENTER
    },
    draggable: true,
    center: new google.maps.LatLng(clat, clng),
    zoom: 4,
    mapTypeId: google.maps.MapTypeId.ROADMAP
  };

  return new google.maps.Map(document.getElementById("map-canvas"), myOptions);

}

var map;
var markersArray = [];
var geocoder; 
var marker;
var listener;

google.maps.event.addDomListener(window, 'load', initmapping);

function initmapping() { //aka initialize()
  map = newMap();
  addMarkers(map,'group');
  geocoder = new google.maps.Geocoder();
}

最后但并非最不重要的是,这里是MarkerClustererPlus库: http:// google-maps-utility-library-v3 .googlecode.com / svn / tags / markerclustererplus / 2.0.9 / src /

Last but not least, here is the MarkerClustererPlus library: http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerclustererplus/2.0.9/src/

推荐答案

对MarkerClusterer了解甚多,但每当您调用addMarkers函数时,您正在创建一个新的MarkerClusterer,但旧的永远不会被销毁/删除,因此缩放更改了您创建的所有实例重新绘制了它们的标记。尝试使用MarkerClusterer的clearMarkers或removerMarkers方法,并且只创建MarkerClusterer的一个实例并重用它,而不是在每次调用addMarkers时创建一个新实例。

I don't know much about MarkerClusterer but what appears to be happening is every time you call your addMarkers function you are creating a new MarkerClusterer but the old one is never destroyed/removed, hence on zoom change all instances you have created redraw the markers they have. Try the clearMarkers or removerMarkers method of MarkerClusterer and also only create one instance of MarkerClusterer and reuse it instead of creating a new one each time you cal addMarkers.

这篇关于缩放级别的任何更改都会导致我的所有标记重新出现在我的Google地图上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆