2012-05-17 47 views
27

我剛剛複製了此代碼,並應用了我的緯度和經度。然而,經度和緯度將是動態的,並且地圖的center將根據位置的緯度和經度而改變。在谷歌地圖中查找多個位置的中心

以下是其他問題

<!DOCTYPE html> 
<html> 

    <head> 
     <meta http-equiv="content-type" content="text/html; charset=UTF-8" /> 
     <title>Google Maps Multiple Markers</title> 
     <script src="http://maps.google.com/maps/api/js?sensor=false" type="text/javascript"></script> 
    </head> 

    <body> 
     <div id="map" style="width: 500px; height: 400px;"></div> 
     <script type="text/javascript"> 
      var locations = [ 
       ['Bondi Beach', -33.890542, 151.274856, 4], 
       ['Coogee Beach', -33.923036, 151.259052, 5], 
       ['Cronulla Beach', -34.028249, 151.157507, 3], 
       ['Manly Beach', -33.80010128657071, 151.28747820854187, 2], 
       ['Maroubra Beach', -33.950198, 151.259302, 1] 
      ]; 

      var map = new google.maps.Map(document.getElementById('map'), { 
       zoom: 10, 
       center: new google.maps.LatLng(-33.92, 151.25), 
       mapTypeId: google.maps.MapTypeId.ROADMAP 
      }); 

      var infowindow = new google.maps.InfoWindow(); 

      var marker, i; 

      for (i = 0; i < locations.length; i++) { 
       marker = new google.maps.Marker({ 
        position: new google.maps.LatLng(locations[i][1], locations[i][2]), 
        map: map 
       }); 

       google.maps.event.addListener(marker, 'click', (function(marker, i) { 
        return function() { 
         infowindow.setContent(locations[i][0]); 
         infowindow.open(map, marker); 
        } 
       })(marker, i)); 
      } 
     </script> 
    </body> 

</html> 

所以我的問題是,其動態位置時,我怎麼會知道地圖中心的代碼。我試過把center留空,但地圖沒加載。

回答

82

首先,您可以通過包含所有動態生成的位置來創建一個LatLngBounds對象。使用擴展方法來包含點。然後,您可以使用getCenter方法獲取邊界的中心。

UPDATE:

代碼:

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

for (i = 0; i < locations.length; i++) { 
    bound.extend(new google.maps.LatLng(locations[i][2], locations[i][3])); 

    // OTHER CODE 
} 

console.log(bound.getCenter()); 

插圖:

enter image description here

+2

添加map.fitBounds(結合)到代碼,以適應範圍內的地圖縮放和中心UI。 –

0

我通過平均緯度和平均經度和使用那些這樣做平均數作爲我的中心。

例子:

self.adjustPosition = function() { 
    var lat = 0, lng = 0; 

    if (self.nearbyPlaces().length == 0) { 
     return false; 
    } 

    for (var i = 0; i < self.nearbyPlaces().length; i++) { 
     lat += self.nearbyPlaces()[i].latitude; 
     lng += self.nearbyPlaces()[i].longitude; 
    } 

    lat = lat/self.nearbyPlaces().length; 
    lng = lng/self.nearbyPlaces().length; 

    self.map.setCenter(new window.google.maps.LatLng(lat, lng)); 
}; 
0

這種方法是行不通的,因爲這將平均的數字,並沒有得到該號碼的「中間」(經/緯)。想想這樣,你在美國有6個點,在加利福尼亞州有一個,東部沿海有5個點在北部和南部。東海岸的5點在地圖上向右(東)加權平均值,你的中心點在格魯吉亞周圍(東到西)。如果你想避免使用getCenter,你會希望找到最高和最低的東方(緯度+/-和長+/-),並找到每個方向的中間,一旦你有極端。

0
private LatLng computeCentroid(List<LatLng> points) { 
double latitude = 0; 
double longitude = 0; 
int n = points.size(); 

for (LatLng point : points) { 
    latitude += point.latitude; 
    longitude += point.longitude; 
} 

return new LatLng(latitude/n, longitude/n); 

}