使用 kml 文件时,Google 地图缩放会被覆盖 [英] Google Maps zoom gets overridden, when using a kml file

查看:30
本文介绍了使用 kml 文件时,Google 地图缩放会被覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 kml 文件中指定 google 地图的缩放级别,或者为什么在加载此文件时我的缩放级别被覆盖.我的问题实际上是如何控制以下链接的地图缩放:

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

推荐答案

默认情况下,地图居中并缩放到 kml 图层内容的边界框.

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

您可以使用 google.maps.KmlLayerOptions 对象的 preserveViewport 属性更改默认行为.如果您将其设置为 true,则地图不会居中和缩放.

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.

在示例中,使用:

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

如果以后要居中并缩放到kml图层的内容,请使用:

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

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

如果您希望在加载 kml 图层时地图始终居中(但不缩放),请使用 google.maps.KmlLayer 对象的 defaultviewport_changed 事件.您必须将地图中心设置为 kml 图层默认视口的中心.加载 kml 图层的内容并计算其默认视口时触发该事件.

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());
});

这篇关于使用 kml 文件时,Google 地图缩放会被覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆