我想基于动态加载的标记将Google Map集中。现在,我在地图上显示两个标记(请参阅脚本。JS文件):
$mapster.mapster('addMarker', {
location: 'Golden Gate Bridge, San Francisco, CA',
content: 'Example text 2'
});
$mapster.mapster('addMarker', {
lat: 37.751350,
lng: -122.485833,
content: '<div style="color: #f00;">Example text 1</div>'
});
一切正常,因为我定义了用于中心地图的坐标(请参阅Map-options.js文件):
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
};
如何基于任何加载标记的Google Map中心?我基本上想取得这样的成就:基于标记的Google地图
中心这是我的代码:https://plnkr.co/edit/sduzkbxiiijhnkcipsor?p=preview
如您链接的帖子中所述,您需要在添加它们时将坐标添加到bounds
对象,然后在此上调用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);
如果您有很长的列表,则可能需要在数组或其他内容上进行一些迭代。