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

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

问题描述

如何为kml文件中的Google地图指定缩放级别,或者为什么当我加载此文件时,缩放级别会被忽略。我的问题实际上是如何控制以下链接的地图缩放比例:

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

解决方案

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

您可以使用 preserveViewport 属性 google.maps.KmlLayerOptions 对象。如果您将它设置为 true ,那么地图不会居中放大。



在示例中,使用:

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

如果您想稍后居中并放大到kml图层的内容,请使用:

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

编辑:

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

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


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

解决方案

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

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

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