如何在谷歌地图中设置缩放级别
这里是我写的代码,通过提供纬度和经度向谷歌地图添加标记。问题是我得到一个非常高度放大的谷歌地图。我曾尝试将缩放级别设置为1,但这对高度放大的地图没有影响。如何在谷歌地图中设置缩放级别
<script src="http://maps.google.com/maps/api/js?v=3&sensor=false" type="text/javascript"></script>
<script type="text/javascript">
var icon = new google.maps.MarkerImage("http://maps.google.com/mapfiles/ms/micons/blue.png",new google.maps.Size(32, 32), new google.maps.Point(0, 0),new google.maps.Point(16, 32));
var center = null;
var map = null;
var currentPopup;
var bounds = new google.maps.LatLngBounds();
function addMarker(lat, lng, info) {
var pt = new google.maps.LatLng(lat, lng);
bounds.extend(pt);
var marker = new google.maps.Marker({
position: pt,
icon: icon,
map: map
});
var popup = new google.maps.InfoWindow({
content: info,
maxWidth: 300
});
google.maps.event.addListener(marker, "click", function() {
if (currentPopup != null) {
currentPopup.close();
currentPopup = null;
}
popup.open(map, marker);
currentPopup = popup;
});
google.maps.event.addListener(popup, "closeclick", function() {
map.panTo(center);
currentPopup = null;
});
}
function initMap() {
map = new google.maps.Map(document.getElementById("map"), {
center: new google.maps.LatLng(0, 0),
zoom: 1,
mapTypeId: google.maps.MapTypeId.ROADMAP,
mapTypeControl: false,
mapTypeControlOptions: {
style: google.maps.MapTypeControlStyle.HORIZONTAL_BAR
},
navigationControl: true,
navigationControlOptions: {
style: google.maps.NavigationControlStyle.SMALL
}
});
addMarker(27.703402,85.311668,'New Road');
center = bounds.getCenter();
map.fitBounds(bounds);
}
</script>
</head>
<body onload="initMap()" style="margin:0px; border:0px; padding:0px;">
<div id="map"></div>
</body>
</html>
我该如何降低这种情况下的缩放级别?
您的代码如下缩放地图,以适应指定的范围:
addMarker(27.703402,85.311668,'New Road');
center = bounds.getCenter();
map.fitBounds(bounds);
如果您只有1个标记并将其添加到边界,结果为最接近的变焦可能:
function addMarker(lat, lng, info) {
var pt = new google.maps.LatLng(lat, lng);
bounds.extend(pt);
}
如果你跟踪你已经“添加”到地图(或扩展与边界)标记的数量,你只能调用fitBounds如果这个数字大于一。我通常将标记推入数组(以备后用)并测试该数组的长度。
如果您只有一个标记,请勿使用fitBounds。请拨打setCenter
,setZoom
以标记位置和所需的缩放级别。
function addMarker(lat, lng, info) {
var pt = new google.maps.LatLng(lat, lng);
map.setCenter(pt);
map.setZoom(your desired zoom);
}
map.setZoom(zoom:number)
https://developers.google.com/maps/documentation/javascript/reference#Map
下面是我用一个函数:
var map = new google.maps.Map(document.getElementById('map'), {
center: new google.maps.LatLng(52.2, 5),
mapTypeId: google.maps.MapTypeId.ROADMAP,
zoom: 7
});
function zoomTo(level) {
google.maps.event.addListener(map, 'zoom_changed', function() {
zoomChangeBoundsListener = google.maps.event.addListener(map, 'bounds_changed', function (event) {
if (this.getZoom() > level && this.initialZoom == true) {
this.setZoom(level);
this.initialZoom = false;
}
google.maps.event.removeListener(zoomChangeBoundsListener);
});
});
}
对于缩放地图二级然后只需添加一行 map.setZoom(map.getZoom() + 2);
的这个小代码,您正在寻找什么是每个缩放级别的尺度。使用这些:
20:1128.497220
19:2256.994440
18:4513.988880
17:9027.977761
16:18055.955520
15:36111.911040
14 :72223.822090
13:144447.644200
12:288895.288400
11:577790.576700
10:1155581.153000
9:2311162.307000
8:4622324.614000
7:9244649。227000
6:18489298.450000
5:36978596.910000
4:73957193.820000
3:147914387.600000
2:295828775.300000
1:591657550.500000
以英尺为单位? – 2015-11-09 02:40:19
我只有一个标记,所以我想下降ase缩放级别,你能帮我修改上面的代码吗?感谢您的帮助 – rockstar 2012-07-13 02:29:15
修改了解决单个标记案例的答案 – geocodezip 2012-07-13 05:54:51