Google maps 删除标记时设置缩放
从一组标记中删除标记时,我希望将缩放级别设置为包含尽可能靠近的所有剩余标记: 对于API第2版,已回答了这一问题: 但是我不能把它翻译成v3Google maps 删除标记时设置缩放,google-maps,google-maps-api-3,google-maps-markers,Google Maps,Google Maps Api 3,Google Maps Markers,从一组标记中删除标记时,我希望将缩放级别设置为包含尽可能靠近的所有剩余标记: 对于API第2版,已回答了这一问题: 但是我不能把它翻译成v3 <html> <head> <meta name="viewport" content="initial-scale=1.0, user-scalable=no" /> <style type="text/css"> html { height: 100% } body { height: 100
<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>
html{高度:100%}
正文{高度:100%;边距:0;填充:0}
#地图画布{高度:100%}
::删除标记加载示例::
var界;
变量点={};
/*初始化映射*/
函数初始化(){
var-map=null;
var映射选项={
中心:新google.maps.LatLng(54.749991,-4.18213),
缩放:3,
mapTypeId:google.maps.mapTypeId.ROADMAP
}
map=new google.maps.map(document.getElementById(“map_canvas”),
地图选项);
设置标记(地图、邮政编码);
}
var邮政编码=[
[EH1',55.952,-3.188],
[EH2',55.954,-3.195],
[EH3',55.954,-3.2],
[EH5',55.975,-3.216]
];
var-markersArray=[];
功能设置标记(地图、位置){
bounds=新的google.maps.LatLngBounds();
var infowindow=new google.maps.infowindow({content:“holding…”);
对于(变量i=0;i
您在将其翻译为v3时遇到了什么问题(可能会发布您的代码以尝试这样做)?看起来很简单(使用v3 google.maps.Map.fitBounds例程),希望这会有所帮助。。必须先从代码中删除一些不相关的东西..修复了!抱歉,简单到使映射变为全局。。我以为我已经这么做了,但显然错过了一些其他的东西(这里没有包括),这打破了它..而且-正如geocodezip所说-map.setZoom(map.getBoundsZoomLevel(bounds)-1);应该是map.fitbunds(bounds);