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