2010-12-11 30 views
0

Google Maps v3 APIを使用して、特定の都市のロケーショングループを生成しています。地図をすべての場所の平均位置に合わせてロードします。これを行うには、私はlocations、次のコードを使用して呼び出さ配列に地図上のマークにしたいすべての場所の緯度経度オブジェクトをロードしています:Google Maps APIバウンドエラー

if(locations.length > 1) 
{ 
    var bounds = new google.maps.LatLngBounds(locations[0], locations[1]); 
    for(var x = 2; x < locations.length; x++) bounds.extend(locations[x]); 
    var center = bounds.getCenter(); 
} 
else var center = locations[0]; 

しかし、テストでは、私が定義するには、このコードを使用しました場所の配列:

var locations = []; 
locations.push(new google.maps.LatLng(50.11658, 8.68552)); 
locations.push(new google.maps.LatLng(50.10026, 8.66941)); 
locations.push(new google.maps.LatLng(50.10989, 8.68822)); 
locations.push(new google.maps.LatLng(50.11074, 8.68269)); 
locations.push(new google.maps.LatLng(50.1040552, 8.6936269)); 
locations.push(new google.maps.LatLng(50.110206, 8.6818686)); 
locations.push(new google.maps.LatLng(50.10957, 8.69077)); 

centerのために狂った結果を得ました。最初の場所(50.11658、8.68552の場所)を削除すると、コードが機能します。最初の場所が配列にプッシュされないように移動すると、コードが機能します。私はなぜ、どのようにその場所がこのエラーを作り出すか、あるいは作り出すことができないのか分かりません!

答えて

0

LatLngBoundsのコンストラクタは、最初の位置が南西の境界点に、2番目の位置が北東の境界点になると考えています。あなたのポイントが間違っているようです。

引数はオプションであるので、以下は動作するはずです:

if(locations.length > 1) { 
    var bounds = new google.maps.LatLngBounds(); 
    for(var x = 0; x < locations.length; x++) 
     bounds.extend(locations[x]); 
    var center = bounds.getCenter(); 
}