【发布时间】:2010-02-08 06:43:51
【问题描述】:
这可能吗,假设我在谷歌地图中搜索任何随机城市,谷歌地图自动设置缩放级别?我的意思是如何自动设置谷歌地图的缩放级别?
我正在使用谷歌地图 API。
请帮帮我。
【问题讨论】:
标签: google-maps
这可能吗,假设我在谷歌地图中搜索任何随机城市,谷歌地图自动设置缩放级别?我的意思是如何自动设置谷歌地图的缩放级别?
我正在使用谷歌地图 API。
请帮帮我。
【问题讨论】:
标签: google-maps
见:How to get Google Maps API to set the correct zoom level for a country?
总之,您使用 Geocoder 对象通过查询字符串 (", ") 获取位置。然后使用返回的 PlaceMarker 对象来定义 LatLngBounds 对象。使用 LatLngBounds 对象作为 getBoundsZoomLevel 的参数来设置缩放级别。
【讨论】:
您可以针对某个地理位置要求尽可能高的缩放: https://developers.google.com/maps/documentation/javascript/maxzoom
我在创建地图后集成了调用。因此,在加载地图时,它也会请求最大缩放级别,并且仅当地图的缩放级别低于地图的缩放级别时才会重置地图上的缩放级别:
this.map = this.createMap(this.el, this.initPosition);
var maxZoomService = new google.maps.MaxZoomService();
maxZoomService.getMaxZoomAtLatLng(this.initPosition, _.bind(function(response) {
if (response.status != google.maps.MaxZoomStatus.OK) {
return;
} else {
console.log("GoogleMapView maxzoomlevel is " + response.zoom);
if (response.zoom < this.map.getZoom()) {
this.map.setZoom(response.zoom);
}
}
}, this));
【讨论】:
<!DOCTYPE html "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Google Maps JavaScript API Example</title>
<script src="http://maps.google.com/maps?file=api&v=2&key=abcdefg&sensor=true_or_false"
type="text/javascript"></script>
<script type="text/javascript">
function initialize() {
if (GBrowserIsCompatible()) {
var map = new GMap2(document.getElementById("map_canvas"));
map.setCenter(new GLatLng(37.4419, -122.1419), 13);
map.setUIToDefault();
}
}
</script>
</head>
<body onload="initialize()" onunload="GUnload()">
<div id="map_canvas" style="width: 500px; height: 300px"></div>
</body>
</html>
map.setCenter() 函数可用于设置缩放级别,即 map.setCenter(GLatLng 坐标,缩放级别)
【讨论】: