从一组标记中删除标记时,我想设置缩放级别以包括尽可能接近的所有剩余标记:
已针对 API 的 v2 回答了此问题: 删除标记时设置缩放级别
但我不能把它翻译成 v3 ..
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
html { height: 100% }
body { height: 100%; margin: 0; padding: 0 }
#map_canvas { height: 100% }
</style>
<script type="text/javascript"
src="https://maps.googleapis.com/maps/api/js?key=&sensor=false">
</script>
</head>
<body onload="initialize()">
<div id="title">:: Removing Markers Example Loading:: </div>
<script type="text/javascript">
var bounds;
var points = {};
/* Initialize the map */
function initialize(){
var map = null;
var mapOptions={
center: new google.maps.LatLng(54.749991,-4.18213),
zoom: 3,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
setMarkers(map, postcodes);
}
var postcodes = [
['EH1',55.952,-3.188],
['EH2',55.954,-3.195],
['EH3',55.954,-3.2],
['EH5',55.975,-3.216]
];
var markersArray = [];
function setMarkers(map, locations) {
bounds = new google.maps.LatLngBounds();
var infowindow = new google.maps.InfoWindow({content: "holding..."});
for (var i = 0; i < locations.length; i++) {
var pcd = locations[i];
var myLatLng = new google.maps.LatLng(pcd[1], pcd[2]);
points[i]=myLatLng;
var marker = new google.maps.Marker({
position: myLatLng,
map: map,
html: "<a onclick=\"removeMarker('" + pcd[0] + "','"+i+"');\" href='javascript:void(0);'>Remove " + pcd[0] + "</a>",
title: pcd[0],
});
bounds.extend(myLatLng);
markersArray[i] = marker;
google.maps.event.addListener(marker, 'click', function(){
infowindow.setContent(this.html);
infowindow.open(map,this);
});
}
map.fitBounds(bounds);
}
function removeMarker(pc,markersKey){
markersArray[markersKey].setMap(null);
points[markersKey]=null;
bounds = new google.maps.LatLngBounds();
for (var point in points)
{
if (points[point]!=null)
{
bounds.extend(points[point]);
}
}
map.setCenter(bounds.getCenter());
map.setZoom(map.getBoundsZoomLevel(bounds)-1);
}
</script>
<div id="map_canvas" style="width:350px;height:450px;"></div>
</body>
</html>