删除标记时设置缩放级别

时间:2010-04-26 20:46:48

标签: google-maps google-maps-markers

使用javascript google-maps api

我目前已将其设置为删除我在我添加类似位置时设置的制造商

  function addLocation(map,location) {
      var point = new GLatLng(location.lat, location.lon);
      var marker = new GMarker(point);
      map.addOverlay(marker);
      bounds.extend(marker.getPoint());

      $('<a href="#" class="closebutton">').click(function(e){
        e.preventDefault();
        $(this).parent().remove();
        map.removeOverlay(marker);
        map.closeInfoWindow();
    }).prependTo($('<li>'+location.label+'</li>').click(function() {
            showMessage(marker, location.label,map);    
      }).appendTo("#list"));
      GEvent.addListener(marker, "click", function() {
        showMessage(marker, location.label,map);
      });
  }

然后我有一个设置缩放级别的功能

 function zoomToBounds(map) {
     map.setCenter(bounds.getCenter());
     map.setZoom(map.getBoundsZoomLevel(bounds) - 1);
 }

这是在我的addLocations函数之后调用并执行我想要的操作并设置缩放级别,以便我可以看到所有制作者。

现在如果我在

之后立即调用zoomToBounds
  map.removeOverlay(marker);

然后它不移动它只是保持在相同的缩放级别

所以我想知道的是,如果有一种方法让我在删除标记后设置缩放级别?

1 个答案:

答案 0 :(得分:4)

嘿那里 - 这绝对是您可以使用Google Maps API完成的事情。

您需要确保执行的一件重要事情是在尝试让GMap2对象重新计算其位置和缩放级别之前更新GLatLngBounds对象。

为此,我建议保留GMarkers正在使用的所有点的某种数据存储。

使用GEvent侦听器,您还可以在删除GMarker时将zoomToBounds函数绑定到事件。

以下是我所说的代码片段:

var bounds = new GLatLngBounds();
var points = {};

function createMarker(location)
{
     /*Create Our Marker*/
     var point = new GLatLng(location.lat,location.lon);
     var marker = new GMarker(point);

     /*Add an additional identifier to the Marker*/
     marker.myMarkerName = 'uniqueNameToIDMarkerPointLater';

     /*Store the point used by this Marker in the points object*/
     points[marker.myMarkerName] = point;

     /*Create an event that triggers after the marker is removed to call zoomToBounds*/
     GEvent.addListener(marker,"remove",function()
     {
          /*Passes the marker's ID to zoomToBounds*/
          zoomToBounds(this.myMarkerName);    
     };

     /*Add the new point to the existing bounds calculation*/
     bounds.extend(point);      

     /*Draws the Marker on the Map*/     
     map.addOverlay(marker);                  
}

function zoomToBounds(name)
{
     /*Remove the Point from the Point Data Store*/
     points[name]=null;

     /*Create a new Bounds object*/
     bounds = new GLatLngBounds();

     /*Iterate through all our points and build our new GLatLngBounds object*/
     for (var point in points)
     {
          if (points[point]!=null)
          {
               bounds.extend(points[point]);
          }
     }

     /*Calculate the Position and Zoom of the Map*/
     map.setCenter(bounds.getCenter());
     map.setZoom(map.getBoundsZoomLevel(bounds)-1);
}

GLatLngBounds对象不存储它用于计算最大和最小边界的所有点 - 因此需要创建一个新对象来重新定义矩形的边界。

我还创建了一个功能正常的例子 here

随意将源代码用于您需要的任何内容 - 让我知道您如何使用它,或者如果您有任何其他问题!

以下是没有任何评论的代码:

var bounds = new GLatLngBounds();
var points = {};

function createMarker(location)
{
     var point = new GLatLng(location.lat,location.lon);
     var marker = new GMarker(point);
     marker.myMarkerName = 'uniqueNameToIDMarkerPointLater';
     points[marker.myMarkerName] = point;
     GEvent.addListener(marker,"remove",function()
     {
          zoomToBounds(this.myMarkerName);    
     };
     bounds.extend(point);        
     map.addOverlay(marker);                  
}

function zoomToBounds(name)
{
     points[name]=null;
     bounds = new GLatLngBounds();
     for (var point in points)
     {
          if (points[point]!=null)
          {
               bounds.extend(points[point]);
          }
     }
     map.setCenter(bounds.getCenter());
     map.setZoom(map.getBoundsZoomLevel(bounds)-1);
}
相关问题