2012-07-11 108 views
0

地圖顯示2個標記,一個標記爲地理位置座標,另一個標記爲隨機座標。我無法擴展邊界以適應地圖中的兩個標記。怎麼了?先謝謝你。Google Maps v3 API擴展界限map.fitBounds無法擴展界限

<!DOCTYPE html> 
<html> 
    <head> 
    <title>Google Maps JavaScript API v3 Example: Map Geolocation</title> 
    <meta name="viewport" content="initial-scale=1.0, user-scalable=yes"> 
    <meta charset="UTF-8"> 
    <link href="https://google-developers.appspot.com/maps/documentation/javascript/examples/default.css" 
     rel="stylesheet" type="text/css"> 

    <script type="text/javascript" 
     src="https://maps.googleapis.com/maps/api/js?sensor=true"></script> 

    <script type="text/javascript"> 
     var map; 

     function initialize() { 
     var myOptions = { 
      zoom: 18, 
      mapTypeId: google.maps.MapTypeId.ROADMAP 
     }; 
     map = new google.maps.Map(document.getElementById('map_canvas'), 
      myOptions); 

     // Try HTML5 geolocation 
     if(navigator.geolocation) { 
      navigator.geolocation.getCurrentPosition(function(position) { 
      var pos = new google.maps.LatLng(position.coords.latitude, position.coords.longitude); 
      var geolocation = new google.maps.Marker({position: pos, map: map, title: 'Your geolocation', }); 

      var latitude=47.03249; 
      var longitude=28.833747; 
      var pos2=new google.maps.LatLng(latitude,longitude); 
      var geolocation2 = new google.maps.Marker({position: pos2,map: map,title: 'Your geolocation',}); 

      var pos3=new google.maps.LatLng((position.coords.latitude+latitude)/2,(position.coords.longitude+longitude)/2); 
      map.setCenter(pos3); 

      bounds.extend(geolocation.getPosition());    
      bounds.extend(geolocation2.getPosition()); 

    map.fitBounds(bounds); 

      }, 
      function() {handleNoGeolocation(true);}); 
     } 
     else {handleNoGeolocation(false); 
     } 

     } 

     google.maps.event.addDomListener(window, 'load', initialize); 
    </script> 
    </head> 
    <body> 
    <div id="map_canvas" style='width:400px; height:400px;'></div> 
    </body> 
</html> 

回答

1

您的界限需要是google.maps.LatLngBounds對象。
你永遠不會構建它。

添加(你打電話延長它在什麼地方):

var bounds = new google.maps.LatLngBounds(); 

順便說一句 - 你應該得到的JavaScript錯誤,使這個真正清楚。

+0

太棒了,我不再需要map.setCenter – Adrian 2012-07-11 05:35:39