Google Maps zoom gets overridden, when using a kml file

两盒软妹~` 提交于 2019-11-26 20:24:02

问题


How do I specify zoom level for google maps in a kml file or why is it that my zoom level gets over ridden when I load this file. My question is actually how do I control zoom of a map for the following link:

http://code.google.com/apis/maps/documentation/javascript/examples/layer-kml-features.html


回答1:


By default, the map is centered and zoomed to the bounding box of the contents of the kml layer.

You can change the default behaviour with preserveViewport property of google.maps.KmlLayerOptions object. If you set it to true the map isn't centered and zoomed.

In the example, use:

var nyLayer = new google.maps.KmlLayer(
                  'http://www.searcharoo.net/SearchKml/newyork.kml',
                  {
                      suppressInfoWindows: true,
                      map: map,
                      preserveViewport: true
                  });

If you want to center and zoom to the contents of the kml layer later, use:

var bounds = nyLayer.getDefaultViewport();
map.fitBounds(bounds);

EDIT:

If you want the map to be always centered (but not zoomed) when the kml layer is loaded, utilize defaultviewport_changed event of the google.maps.KmlLayer object. You have to set the map center to the center of the kml layer default viewport. The event is triggered when the contents of the kml layer are loaded and its default viewport is computed.

google.maps.event.addListener(nyLayer, 'defaultviewport_changed', function() {
   var bounds = nyLayer.getDefaultViewport();
   map.setCenter(bounds.getCenter());
});


来源:https://stackoverflow.com/questions/8187837/google-maps-zoom-gets-overridden-when-using-a-kml-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!