2010-08-04 6 views
7

googlemaps fitBounds 기능에 문제가 있습니다.Google지도 fitBounds가 제대로 작동하지 않습니다.

for (var i = 0; i < countries.length; i++) { 
var country = countries[i]; 
var latlng = new google.maps.LatLng(parseFloat(country.lat), parseFloat(country.lng)); 
mapBounds.extend(latlng); 
} 

map.fitBounds(mapBounds); 

일부 아이콘은 뷰포트/표시 영역 외부에 표시됩니다.

아이디어가 있습니까?

미리 감사드립니다.

답변

0

환자에게 연결되는 링크를 보여주십시오. 국가 배열에 얼마나 많은 나라가 있습니까? 전 세계? 경계가 반 자오선을 넘고 있습니까?

country.lat 및 country.lng은 국가마다 하나의 지점이며 나라의 경계 상자를 정의하기에 충분하지 않습니다. 그것은 일종의 "국가 중심"입니까?

만약 그렇다면, 가장 동쪽 국가의 중심에서 동쪽으로 또는 서쪽 중심에서 중심에서 서쪽으로 마커가있는 경우 해당 마커는 물론 당신이 정의.

map.fitBounds() 방법이 정상적으로 작동합니다. :-)

Marcelo.

9

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을 나타내는 아래의 스크린 샷에 표시됩니다 주제에 오버 플로우 게시물 :