2013-08-23 50 views
0

如何動態地從以下座標集中計算地圖中心座標。這些座標是從xml文件中獲取的。基於XML座標,我想設置中心座標。 VAR triangleCoords = [ 新google.maps.LatLng(51.049042161127545,-3.1417465209960938) 新google.maps.LatLng(51.00477555656173,-3.1235504150390625) 新google.maps.LatLng(51.01428024457833,-3.0490493774414062) 新谷歌。 maps.LatLng(51.047962990786715,-3.0806350708007812) ];如何從座標集中設置地圖中心屬性

 <html> 
     <head> 

      <link href="/maps/documentation/javascript/examples/default.css" rel="stylesheet"> 
      <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script> 
      <script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script> 
      <script> 
       function initialize() { 


        var myLatLng = new google.maps.LatLng(51.017303, -3.096289); 
        var mapOptions = { 
         zoom: 12, 
         center: myLatLng, 
         mapTypeId: google.maps.MapTypeId.TERRAIN 
        }; 

        var bermudaTriangle; 

        var map = new google.maps.Map(document.getElementById('map-canvas'), 
       mapOptions); 


        var triangleCoords = [ 
       new google.maps.LatLng(51.049042161127545, -3.1417465209960938), 
       new google.maps.LatLng(51.00477555656173, -3.1235504150390625), 
       new google.maps.LatLng(51.01428024457833, -3.0490493774414062), 
       new google.maps.LatLng(51.047962990786715, -3.0806350708007812) 
      ]; 



        // Construct the polygon 
        bermudaTriangle = new google.maps.Polygon({ 
         paths: triangleCoords, 
         strokeColor: '#FF0000', 
         strokeOpacity: 0.8, 
         strokeWeight: 2, 
         fillColor: '#FF0000', 
         fillOpacity: 0.35 
        }); 


        bermudaTriangle.setMap(map); 
        /// To display Marker within the above drawn polygon coordinates 


        var pos = new google.maps.LatLng(51.01428024457833, -3.0490493774414062); 

        var marker = new google.maps.Marker({ 
         position: pos, 
         map: map, 
         title: name 
        }); 

        var info = "This is a marker for the following co-ordinates:<br />latitude: " + latitude + "<br/>longitude: " + longitude; 
        google.maps.event.addListener(marker, 'click', function() { 
         var infowindow = new google.maps.InfoWindow({ 
          content: info 
         }); 
         infowindow.open(map, marker); 
        }); 

       } 

       google.maps.event.addDomListener(window, 'load', initialize); 

      </script> 
     </head> 
     <body> 
      <div id="map-canvas" style="height: 500px; width: 500px"> 
      </div> 
     </body> 
</html> 

    Could you please help me out this. 

回答

1

你必須添加一個名爲map.setBounds 首先聲明一個新LatLngBounds,然後添加所有你必須給它的latlngs方法。

像這樣:在https://developers.google.com/maps/documentation/javascript/reference#Map

+0

好,謝謝

var bounds = new google.maps.LatLngBounds(); for (var i = 0; i < triangleCoords.length; i++) { bounds.extend(triangleCoords[i]); } map.fitBounds(bounds); 

更多信息。我們如何通過放置for循環來創建如下所示的數組。 var triangleCoords = [new google.maps.LatLng(51.049042161127545,-3.1417465209960938),new google.maps.LatLng(51.00477555656173,-3.1235504150390625),new google.maps.LatLng(51.01428024457833,-3.0490493774414062),new google.maps.LatLng( 51.047962990786715,-3.0806350708007812)]; – user2703381

+0

你爲什麼不接受? – putvande

+0

如何通過從數據庫中獲取座標動態地創建上述traingleCoords的數組?請 – user2703381

相關問題