Google Maps zoom gets overridden, when using a kml file
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());
});
Comments
-
Mike almost 2 years
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
-
Mike over 12 yearsThanks, but when I set a higher zoom level say 18, the markers dont become visible, either they are not appearing at all or I need to center them to make them visible, how do I always make sure that markers are appearing in the map near the center
-
Tomik over 12 yearsI updated the answer. Now, you can get the map immediately centered to the position of the markers in the kml layer.
-
Mike over 12 yearsActually I have certain markers on the map depending on the location, On page load, I should be able to see all the markers and if I click somewhere near a particular market, I want it to zoom around. Also I updated the code, but it looks like it is taking the wrong js file: jsfiddle.net/WPXWX/1
-
Tomik over 12 yearsI guess you should post it as a new question, it's getting too far from your original question.
-
Mike over 12 yearsokay, But I appreciate your help with this. And with my first question in place, is my fiddle correct
-
Tomik over 12 yearsThen you should consider accepting the answer. And fiddle: there's one problem. You can't call nyLayer.getDefaultViewport() right after creation of the kml layer, the contents of the layer are undefined until you the defaultviewport_changed event is called.
-
Mike over 12 yearsI was sure to do that, but I was wondering if I can get this done first. Can you also send the link for this kml documentation. I couldn't locate anything in google code. and is it possible for you to update fiddle
-
Mike over 12 yearsI am getting something g is undefined (126 out of range 43), either markers wont come or zoom doesn't work. what's happening