Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps zoom gets overridden, when using a kml file

Tags:

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

like image 730
Mike Avatar asked Nov 18 '11 19:11

Mike


People also ask

How do I stop Google Maps from zooming in?

To fix Google maps zooming problems, for Google maps default zoom you want to know how to change the zoom level on Google Maps. You can change the zoom level by going to the Edit map page and then selecting 'default zoom level' in the map information section and then clicking save map.

How do I change the default zoom on Google Maps?

You can change the zoom level of the map using simple steps. Step 1 Go to Add or Edit Map page . Step 2 Select 'Default zoom level' in the 'Map Information section'. Step 3 click save map and see the changes.

How do I control zoom in Google Earth?

You can zoom in and out to see more or less of a map area. Use the scroll wheel on your mouse or mouse touchpad to zoom in and out.

How do I change the max zoom level in Google Maps?

Setting up the minimum or maximum Zoom value for Google Maps can be done by adding the shortcode attribute with Store Locator Shortcode. Furthermore, the maximum value of Google Maps Zoom level is 22. Therefore, the defined maximum value must be below or equal to 22, which is the default.


1 Answers

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());
});
like image 154
Tomik Avatar answered Sep 19 '22 14:09

Tomik