Cal*_*lou 31 javascript google-maps google-maps-api-3
我认为标题已经足够了,我甚至不知道如何为V2和V1 API做这个:/ /
谢谢 :)
Dan*_*llo 74
正如其他答案所暗示的那样,该fitBounds()方法应该可以解决问题.
考虑以下示例,它将在美国东北部生成10个随机点,并应用以下fitBounds()方法:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps LatLngBounds.extend() Demo</title>
<script src="http://maps.google.com/maps/api/js?sensor=false"
type="text/javascript"></script>
</head>
<body>
<div id="map" style="width: 400px; height: 300px;"></div>
<script type="text/javascript">
var map = new google.maps.Map(document.getElementById('map'), {
mapTypeId: google.maps.MapTypeId.TERRAIN
});
var markerBounds = new google.maps.LatLngBounds();
var randomPoint, i;
for (i = 0; i < 10; i++) {
// Generate 10 random points within North East USA
randomPoint = new google.maps.LatLng( 39.00 + (Math.random() - 0.5) * 20,
-77.00 + (Math.random() - 0.5) * 20);
// Draw a marker for each random point
new google.maps.Marker({
position: randomPoint,
map: map
});
// Extend markerBounds with each random point.
markerBounds.extend(randomPoint);
}
// At the end markerBounds will be the smallest bounding box to contain
// our 10 random points
// Finally we can call the Map.fitBounds() method to set the map to fit
// our markerBounds
map.fitBounds(markerBounds);
</script>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
多次刷新此示例,没有标记出现在视口之外:

这是方式:
map.fitBounds(bounds);
map.setCenter(bounds.getCenter());
Run Code Online (Sandbox Code Playgroud)
bounds是坐标(标记)的数组.每次放置标记时都会执行以下操作:
bounds.extend(currLatLng);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
42414 次 |
| 最近记录: |