North East USA에서 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
개체와 뷰포트 사이에 작은 마진을 남긴다 가치가 아무것도 아니다.
fitBounds Padding http://img204.imageshack.us/img204/9149/fitit.png
fitBounds Padding http://img441.imageshack.us/img441/9615/fitus.png
당신은 또한 다음과 같은 스택을 체크 아웃에 관심이있을 수 :이 명확하게 빨간색 경계 상자가 fitBounds()
방법에 전달되는 LatLngBounds
을 나타내는 아래의 스크린 샷에 표시됩니다 주제에 오버 플로우 게시물 :