谷歌地图v3 - 放大/缩小能够重复(和重新定位)?

时间:2012-12-05 21:57:56

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

Javascript新手......

我有一个包含多个POLYGON区域的谷歌地图。我已经弄清楚如何只需单击多边形就可以进行ZOOM IN,并使用'dblclick'监听器进行ZOOM OUT并删除它们的监听器......

单击放大后,双击缩小,不允许我再次重复该功能。有没有办法多次放大和缩小?

这是我的代码:

     //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区域,我是否还必须更改它们的变量名称?

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

感谢您的帮助!

1 个答案:

答案 0 :(得分:0)

当前的问题是你在dblclick上添加的点击监听器中没有做同样的事情:

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

vs the original:

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