次の場所の JSON を使用していると仮定します。
var beaches = [
['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]
];
これらをGoogleマップにプロットしていますが、おおよそ真ん中のものを中心にしたいと思います.
これを行うためのGoogleマップAPIに関数はありますか? それとも、自分で平均を計算する必要がありますか?
JSONとズームレベルを渡すことができる関数を作成しました:
function initialize_map(places, zoom) {
// use first coordinates in the json to initiate the map
var place_lat_lng = new google.maps.LatLng(places[0][1],places[0][2]);
var mapOptions = {
zoom: zoom,
center: place_lat_lng,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById('map_canvas'),
mapOptions);
var marker, i;
// loop through all the places to get markers
for (var i=0;i<places.length;i++)
{
var place = places[i];
marker = new google.maps.Marker({
position: new google.maps.LatLng(place[1], place[2]),
map: map,
animation: google.maps.Animation.DROP
});
// add the marker
marker.setMap(map);
}
}
この機能は機能しますが、中央に配置されず、マーカーが見えなくなります。
ビューを中央に配置し、すべてのマーカーが表示されるようにするにはどうすればよいでしょうか?