kml在高缩放级别隐藏多边形

时间:2013-07-15 01:59:44

标签: google-maps hide zoom kml polygon

我有一张Google地图,其中包含在KML文件中定义的点和多边形,并且只在另一个KML文件中定义了点。我希望能够加载一个或另一个,具体取决于缩放级别,并尝试了以下变体:

var url_end = "?nocache=" + (new Date()).valueOf();
var NWlayer = new google.maps.KmlLayer({
  if (map.getZoom() > 18){
    url: "http://www.example.com/Google/ShowPointsOnly.kml" + url_end
  }
  else {
    url: "http://www.example.com/Google/ShowAll.kml" + url_end
  }
});
NWlayer.setMap(map);

...但是根据我尝试的变化,无论缩放级别如何,我都会获得点和多边形,否则我根本就没有地图。

1 个答案:

答案 0 :(得分:2)

您需要为google.maps.Map zoom_changed事件添加事件侦听器

var NWlayer1 = new google.maps.KmlLayer({
    preserveViewport: true,
    url: "http://www.example.com/Google/ShowPointsOnly.kml" + url_end
  });
var NWlayer2 = new google.maps.KmlLayer({
    preserveViewport: true,
    url: "http://www.example.com/Google/ShowAll.kml" + url_end
  });
google.maps.event.addListener(map, 'zoom_changed', function() {
  if (map.getZoom() > 18){
    NWlayer1.setMap(map);
    NWlayer2.setMap(null);
  } else {
    NWlayer1.setMap(null);
    NWlayer2.setMap(map); 
  }
});