谷歌地图 API v3:中心 &放大显示的标记 [英] Google MAP API v3: Center & Zoom on displayed markers
本文介绍了谷歌地图 API v3:中心 &放大显示的标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用以下代码在我的地图上设置标记:
I use the following code to set markers on my map:
var latLngs = []
$.each(locations.markers, function(i, m){
var myLatLng = new google.maps.LatLng(m.latitude, m.longitude);
latLngs[i] = myLatLng
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
shadow: shadow,
icon: image,
shape: shape,
title: m.city,
zIndex: i
});
})
标记显示在我的地图上.现在我想居中 &在这些标记上缩放地图.我怎样才能做到这一点?我试过了:
The markers show up on my map. Now I would like to center & zoom the map on these markers. How can I accomplish this? I have tried:
map.fitBounds(getBoundsForLatLngs(latLngs));
latLngs 的 console.log 给出:
The console.log of latLngs gives out:
[(46.793182, 7.146903) { b=46.793182, more...}, (46.8077779, 7.1709386) { b=46.8077779, more...}]
但它似乎不起作用,而且我在控制台中也没有发现任何错误.我做错了什么?
But it doesn't seem to work, and I get no error in the console. What am I doing wrong?
推荐答案
我也发现这个修复缩放以适合所有标记
LatLngList:latLng 的实例数组,用于例子:
LatLngList: an array of instances of latLng, for example:
// "map" is an instance of GMap3
var LatLngList = [
new google.maps.LatLng (52.537,-2.061),
new google.maps.LatLng (52.564,-2.017)
],
latlngbounds = new google.maps.LatLngBounds();
LatLngList.forEach(function(latLng){
latlngbounds.extend(latLng);
});
// or with ES6:
// for( var latLng of LatLngList)
// latlngbounds.extend(latLng);
map.setCenter(latlngbounds.getCenter());
map.fitBounds(latlngbounds);
这篇关于谷歌地图 API v3:中心 &放大显示的标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文