Google Maps v3 API 扩展边界。Javascript 操作方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3610135/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Google Maps v3 API Extend Bounds. Javascript How-To?
提问by Thomas Clowes
function initialize(){
// Creating a map
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(53.0123601276819, -2.44519164333635),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var m = [];
function addMarker(title, lat, lng) {
m[m.length] = new google.maps.Marker({
position: new google.maps.LatLng(lat, lng),
map: map,
title: title,
clickable: true,
icon: 'http://domain.com/MVC/images/full.png'
});
}
addMarker('Home', 53.0682143712504, -2.52150736731894);
addMarker('Away', 53.0123601276819, -2.44519164333635);
addMarker('Away', 59.0123601276819, -2.44519164333635);
// Create a LatLngBounds object
var bounds = new google.maps.LatLngBounds();
for (var i = 0; i < m.length; i++) {
// Insert code to add marker to map here
// Extend the LatLngBound object
bounds.extend(m[i]);
}
alert(m[0]);
map.fitBounds(bounds);
document.write(getBounds());
}
The above is my code.
以上是我的代码。
My intentions are to develop a map which shows numerous markers and pans the zoom such that all the markers fit on my screen.
我的目的是开发一张地图,显示大量标记并平移缩放,以便所有标记都适合我的屏幕。
I am new to JS.
我是 JS 新手。
My understanding is that
我的理解是
var m = [];
creates an empty array.
创建一个空数组。
Then everytime i call addMarker()the details get added to this array m
然后每次我调用addMarker()细节都会添加到这个数组 m
At the top I set a default zoom and center point.
在顶部,我设置了默认缩放和中心点。
I then add various markers.
然后我添加各种标记。
I then loop through each key in the m array and extend the bounds using the data from this.
然后我循环遍历 m 数组中的每个键并使用来自 this 的数据扩展边界。
It is then my understanding that map.fitBounds(bounds);should redefine the zoom/center as applicable to fit all the markers.
然后我的理解是map.fitBounds(bounds);应该重新定义缩放/中心以适合所有标记。
It does not. All my markers show, but the zoom/center is not auto adjusting as such, and I have no idea why.
它不是。我的所有标记都显示,但缩放/中心不会自动调整,我不知道为什么。
Any advice would be greatly appreciated. Thanks
任何建议将不胜感激。谢谢
回答by Thomas Clowes
You need to pass a LatLngobject to the bounds.extendfunction.
您需要将一个LatLng对象传递给该bounds.extend函数。
Here is the code :
这是代码:
....
bounds.extend(new google.maps.LatLng(lat, lng));
....
回答by Erin Lehmkühl
Here is a clarification of the above answer which is correct except it's missing a parenthesis and a little brief.
这是对上述答案的澄清,它是正确的,只是缺少括号和简短的内容。
//make an empty bounds variable
var bounds = new google.maps.LatLngBounds();
//do your map stuff here
//special note: make sure your lat and lng are floats or googleMaps will error
var lat = 38.103;
var lng = -121.572;
bounds.extend(new google.maps.LatLng(lat, lng));
//adjust the viewport of the map
//special note: this only needs to be done once, don't put it in a loop
map.fitBounds(bounds);

