我有一个google地图,上面有几个kml文件。我喜欢将缩放因子设置为动态,以便显示所有kml-file contenz。是可以计算kml文件的边界,合并它们并设置缩放,以显示所有kml的东西
答案 0 :(得分:0)
尝试使用preserveVieport
var kmlLayer = new google.maps.KmlLayer('your.kml', {
map: map,
preserveViewport: true
});
答案 1 :(得分:0)
你需要做两件事:
preserveViewport
的{{1}}选项设置为true(以防止自动缩放适合)KmlLayer
var mapLayer1 = new google.maps.KmlLayer({
url: kmlUrl,
preserveViewport: true
});
代码段
var bounds = new google.maps.LatLngBounds(); // global
// determine the bounds for layer 1
google.maps.event.addListener(mapLayer1, 'defaultviewport_changed', function() {
bounds.union(mapLayer1.getDefaultViewport());
map.fitBounds(bounds);
});
// include the bounds for layer 2
google.maps.event.addListener(mapLayer2, 'defaultviewport_changed', function() {
bounds.union(mapLayer2.getDefaultViewport());
map.fitBounds(bounds);
});
// repeat for all KmlLayers you want shown
function initialize() {
var location = new google.maps.LatLng(52.0, 5.1);
var mapOptions = {
zoom: 15,
center: location,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
var map = new google.maps.Map(document.getElementById('map-canvas'), mapOptions);
var bounds = new google.maps.LatLngBounds();
//Add MapLayers
var mapLayer1 = new google.maps.KmlLayer({
url: 'http://www.geocodezip.com/geoxml3_test/utah-hunt_com_DeerSouthern_kml.xml',
preserveViewport: true
});
google.maps.event.addListener(mapLayer1, 'defaultviewport_changed', function() {
bounds.union(mapLayer1.getDefaultViewport());
map.fitBounds(bounds);
});
mapLayer1.setMap(map);
var mapLayer2 = new google.maps.KmlLayer({
url: 'http://www.geocodezip.com/geoxml3_test/utah-hunt_com_DeerSoutheastern_kml.xml',
preserveViewport: true
});
google.maps.event.addListener(mapLayer2, 'defaultviewport_changed', function() {
bounds.union(mapLayer2.getDefaultViewport());
map.fitBounds(bounds);
});
mapLayer2.setMap(map);
var mapLayer3 = new google.maps.KmlLayer({
url: 'http://www.geocodezip.com/geoxml3_test/utah-hunt_com_DeerNorthern_kml.xml',
preserveViewport: true
});
google.maps.event.addListener(mapLayer3, 'defaultviewport_changed', function() {
bounds.union(mapLayer3.getDefaultViewport());
map.fitBounds(bounds);
});
mapLayer3.setMap(map);
}
google.maps.event.addDomListener(window, "load", initialize);
html,
body,
#map-canvas {
height: 100%;
width: 100%;
margin: 0px;
padding: 0px
}