Hiding/showing a kml layer on customized google ma

2019-03-04 16:25发布

问题:

Is there any way to show a kml Layer only on Google Earth view layer? I am using Google API V3

This is the link of what I have: http://www.virtualbc.ca/knoxmountain/index2.php

Being on Google Earth View, if I select the Satellite View, I want to remove or hide the kml layer. I am actually using a set of tiles over all the Google views except the Earth view.

回答1:

You can use the maptypeid_changed event on the Google.Map object. When it changes, you can either set the kml layer to show or hide by using setMap(). Example:

function initialize() {

  var map = new google.maps.Map(document.getElementById('map-div'), {
         center: new google.maps.LatLng(40.3,-111.65),
         zoom: 13   
  });

  var kmlLayer = new google.maps.KmlLayer('http://gmaps-samples.googlecode.com/svn/trunk/ggeoxml/cta.kml');
  kmlLayer.setMap(map);
  google.maps.event.addListener(map, 'maptypeid_changed', function() {
        if(map.mapTypeId == 'hybrid') {
            kmlLayer.setMap(null);  
        } 
  });

}

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

Just make sure that when you set the map type back to a different one you re-set the kml layer to the map. When you want to hide something, you just call setMap(null).