Рассмотрим следующий пример, который сгенерирует 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>
При многократном обновлении этого примера ни один маркер никогда не выходит за пределы области просмотра,Самое большее, иногда маркер слегка обрезается сверху, когда он скрыт за элементами управления:
Google Maps LatLngBounds.extend () Demo http://img825.imageshack.us/img825/7424/fitboundsfit.png
Также ничего не стоит, что fitBounds()
всегда оставляет небольшое поле между LatLngBounds
объектом и окном просмотра.Это ясно показано на скриншотах ниже, где красная ограничивающая рамка представляет LatLngBounds
, который передается методу fitBounds()
:
fitBounds Padding http://img204.imageshack.us/img204/9149/fitit.png
fitBoundsPadding http://img441.imageshack.us/img441/9615/fitus.png
Вам также может быть интересно ознакомиться со следующими сообщениями переполнения стека по теме: