在google maps api中显示超过1 kml的负载

时间:2015-07-10 00:41:02

标签: javascript google-maps-api-3

我试图使用Google Maps API在加载时显示超过1 kml。最好的方法是什么?现在它只显示第一个kml。

这是我的脚本。 谢谢你的期待。

    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&signed_in=true"></script>
    <script>
function initialize() {
var myLatlng = new google.maps.LatLng(-105.467, 39.973);
    var mapOptions = {
      zoom: 10,
      center: myLatlng,
      mapTypeId: google.maps.MapTypeId.SATELLITE
    };

  var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);

  var sitesLayer = new google.maps.KmlLayer({
    url: 'https://example.com/kml-kmz-files/basins.kml'
  });
  sitesLayer.setMap(map);

  var basinLayer = new google.maps.KmlLayer({
    url: 'https://example.com/kml-kmz-files/boundries.kml'
  });
  basinLayer.setMap(map);
}


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

    </script>

1 个答案:

答案 0 :(得分:0)

通常会绘制所有图层(只要您没有超出任何限制),但API可能只设置视口以包含单个图层的要素(它取决于加载顺序哪个图层它会是。)

要设置视口以使其包含每个图层的要素,您必须将图层的 What is the temperature in Fahrenheit?: - 属性设置为preserveViewport,并自行处理地图视口的设置。< / p>

示例的实现:

&#13;
&#13;
true
&#13;
function initialize() {
var myLatlng = new google.maps.LatLng(0,0);
    var mapOptions = {
      zoom: 18,
      center: myLatlng
    };

  var map = new google.maps.Map(document.getElementById('map'), mapOptions);

  loadKml=function(opts,map){
     var layer=new google.maps.KmlLayer();
     opts.preserveViewport=true;
     if(map){opts.map=map;}
     
     google.maps.event.addListener(layer,'defaultviewport_changed',function(){
         var map=this.getMap(),
             bounds=map.get('kmlBounds')||this.getDefaultViewport();
         
         bounds.union(this.getDefaultViewport());
         map.set('kmlBounds',bounds);
         map.fitBounds(bounds); 
     });
     layer.setOptions(opts);
     return layer; 
  }
  //blue pins
  var sitesLayer = loadKml({
    url: 'https://www.google.com/maps/d/kml?mid=z_0vvigsiMo4.kV0iI47miw1A',
    map:map
  });
  //red star
  var basinLayer = loadKml({
    url: 'https://www.google.com/maps/d/kml?mid=z4jtZiEBVczE.kp_M0KHEIung'
  },map);
  
}


google.maps.event.addDomListener(window, 'load', initialize);
&#13;
html,body,#map{height:100%;margin:0;padding:0}
&#13;
&#13;
&#13;