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());
});
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…