Javascript 基于标记的谷歌地图中心
我想根据动态加载的标记将谷歌地图居中。现在我正在地图上显示两个标记(请参见script.js文件): 如何根据任何加载的标记将我的谷歌地图居中?我基本上想实现这样的目标: 这是我的密码: 如您链接的中所述,您需要在添加坐标时将坐标添加到Javascript 基于标记的谷歌地图中心,javascript,google-maps,google-maps-api-3,google-maps-markers,centering,Javascript,Google Maps,Google Maps Api 3,Google Maps Markers,Centering,我想根据动态加载的标记将谷歌地图居中。现在我正在地图上显示两个标记(请参见script.js文件): 如何根据任何加载的标记将我的谷歌地图居中?我基本上想实现这样的目标: 这是我的密码: 如您链接的中所述,您需要在添加坐标时将坐标添加到边界对象,然后调用fitBounds(bounds)。看 尝试以下控制流: let location1 = new google.maps.LatLng(lat, lng); let location2 = new google.maps.LatLng(lat,
边界
对象,然后调用fitBounds(bounds)
。看
尝试以下控制流:
let location1 = new google.maps.LatLng(lat, lng);
let location2 = new google.maps.LatLng(lat, lng);
let location3 = new google.maps.LatLng(lat, lng);
let marker = new google.maps.Marker({
position: location1,
map: map
});
marker = new google.maps.Marker({
position: location2,
map: map
});
marker = new google.maps.Marker({
position: location3,
map: map
});
let bounds = new google.maps.LatLngBounds();
bounds.extend(location1);
bounds.extend(location2);
bounds.extend(location3);
map.fitBounds(bounds);
如果您有一个很长的列表,那么您可能需要对数组或其他对象进行迭代
mapster.MAP_OPTIONS = {
center: {
lat: 37.791350,
lng: -122.435883
},
zoom: 10,
disableDefaultUI: false,
scrollwheel: true,
draggable: true,
mapTypeId: google.maps.MapTypeId.ROADMAP,
zoomControlOptions: {
position: google.maps.ControlPosition.LEFT_BOTTOM,
style: google.maps.ZoomControlStyle.DEFAULT
},
panControlOptions: {
position: google.maps.ControlPosition.LEFT_BOTTOM
},
cluster: {
options: {
styles: [{
url: 'https://raw.githubusercontent.com/googlemaps/v3-utility-library/master/markerclusterer/images/m2.png',
height: 56,
width: 55
//textColor: '#000'
//textSize: 12
}, {
url: 'https://raw.githubusercontent.com/googlemaps/v3-utility-library/master/markerclusterer/images/m1.png',
height: 56,
width: 55
//textColor: '#000'
//textSize: 12
}]
}
},
geocoder: true
};
let location1 = new google.maps.LatLng(lat, lng);
let location2 = new google.maps.LatLng(lat, lng);
let location3 = new google.maps.LatLng(lat, lng);
let marker = new google.maps.Marker({
position: location1,
map: map
});
marker = new google.maps.Marker({
position: location2,
map: map
});
marker = new google.maps.Marker({
position: location3,
map: map
});
let bounds = new google.maps.LatLngBounds();
bounds.extend(location1);
bounds.extend(location2);
bounds.extend(location3);
map.fitBounds(bounds);