0

Google Maps v3 API を使用して、特定の都市の場所のグループを生成しています。すべての場所の平均位置を中心にマップをロードしたい。これを行うために、地図上でマークしたいすべての場所の LatLng オブジェクトを呼び出して配列にロードlocationsし、次のコードを使用しています。

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 の場所) を削除すると、コードは機能します。配列にプッシュされた最初の場所にならないように移動すると、コードは機能します。その場所でこのエラーが発生する理由や方法がわかりません。

4

1 に答える 1

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();
}
于 2010-12-11T04:17:13.413 に答える