【问题标题】:Google Maps v3 - zoom in/zoom out able to repeat (and recenter)?Google Maps v3 - 放大/缩小能够重复(和重新定位)?
【发布时间】:2012-12-05 21:57:55
【问题描述】:

我有一个包含多个 POLYGON 区域的 Google 地图。我已经想出了如何通过单击多边形来放大,以及如何使用“dblclick”侦听器来缩小并删除它们的侦听器...

一旦你点击放大,然后双击缩小,它不允许我再次重复该功能。有没有办法多次放大和缩小?

这是我的代码:

     //POLYGON ZONE 1 ================================ 
  var zone1;

  var triangleCoords = [
    new google.maps.LatLng(29.738692, -95.399331),
    new google.maps.LatLng(29.738688, -95.394781),
    new google.maps.LatLng(29.737048, -95.394781),
    new google.maps.LatLng(29.737048, -95.399331)
  ];

  // Construct the polygon
  // Note that we don't specify an array or arrays, but instead just
  // a simple array of LatLngs in the paths property
  zone1 = new google.maps.Polygon({
    paths: triangleCoords,
    strokeColor: "#00AEEF",
    strokeOpacity: 0.8,
    strokeWeight: 2,
    fillColor: "#00AEEF",
    fillOpacity: 0.05,
  });

  zone1.setMap(map);
  zone1._myBounds = new google.maps.LatLngBounds();
  for (var i=0; i<triangleCoords.length; i++)
  {
     zone1._myBounds.extend(triangleCoords[i]);
  }

  var listener = google.maps.event.addListener(zone1, 'click', function () {
      map.fitBounds(zone1._myBounds);
      google.maps.event.removeListener(listener); 
  });

    var listener2 = google.maps.event.addListener(zone1, 'dblclick', function() {
    if (map.getZoom() > 16) map.setZoom(13); 
    //map.setZoom(map.getZoom() -1);
    //map.setCenter(map.getCenter());
    google.maps.event.removeListener(listener2); 
    google.maps.event.addListener(zone1, 'click', function (){
        map.fitBounds(zone1._myBounds);});
  });

对于我的其他 POLYGON 区域,我是否还必须更改变量名称才能使它们工作?

此外,返回中心似乎无法正常工作。如果我需要在单独的问题中提出这个问题。

【问题讨论】:

  • 如果你想再做一次,为什么要移除监听器?

标签: javascript google-maps-api-3 dom-events


【解决方案1】:

直接的问题是您在 dblclick 上添加的点击侦听器中没有做同样的事情:

google.maps.event.addListener(zone1, 'click', function (){
    map.fitBounds(zone1._myBounds);});

对比原版:

var listener = google.maps.event.addListener(zone1, 'click', function () {
  map.fitBounds(zone1._myBounds);
  google.maps.event.removeListener(listener); 
});

【讨论】:

  • 所以通过对多边形1使用此代码,它不会影响多边形2吗? :google.maps.event.addListener(zone1, 'click', function (){ map.fitBounds(zone1._myBounds);}); google.maps.event.addListener(zone1, 'dblclick', function (){ if (map.getZoom() &gt; 16) map.setZoom(13);});
猜你喜欢
  • 1970-01-01
  • 2020-05-10
  • 1970-01-01
  • 2012-06-10
  • 2015-08-23
  • 2016-01-31
  • 2015-08-27
  • 1970-01-01
  • 2016-12-28
相关资源
最近更新 更多