1

次の場所の 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);            
    }   
}   

この機能は機能しますが、中央に配置されず、マーカーが見えなくなります。

ビューを中央に配置し、すべてのマーカーが表示されるようにするにはどうすればよいでしょうか?

4

2 に答える 2

4

Google Maps API v3には、一連のマーカーを表示するためのメソッドが含まれています。

fitBounds(bounds:LatLngBounds)-なし-指定された境界を含むようにビューポートを設定します。

すべてのマーカーをgoogle.maps.LatLngBoundsオブジェクトに追加してから、マップオブジェクトでそのメソッドを呼び出します。このような:

function initialize_map(places, zoom) {
     var bounds = new google.maps.LatLngBounds(); // empty bounds object

    // 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);            
        bounds.extend(marker.getPosition()); // add the marker to the bounds

    }   

    map.fitBounds(bounds); // show all the markers.
}
于 2013-03-01T13:45:45.350 に答える
2

マーカーをLatLngBoundsに追加し、ボックスの中央をつかむ簡単な方法は次のとおりですこれにより、少なくとも、持っているすべてのマーカーが表示されます。長方形と「C」マーカーは、バウンディングボックスと中央がどのように見えるかを示します。

var bounds = new google.maps.LatLngBounds();
for (x in beaches) {

    var data = beaches[x];       
    var lat = beaches[x][1];
    var lng = beaches[x][2]
    var latLng = new google.maps.LatLng(lat, lng);
    var marker = new google.maps.Marker({
        map: map,
        position: latLng
    });
    markers.push(marker);
    bounds.extend(latLng);
}

map.fitBounds(bounds)

new google.maps.Rectangle({
    bounds: bounds,
    fillColor: '#000000',
    map: map
})

var centerBounds = new google.maps.Marker({
    map: map,
    position: bounds.getCenter(),
    icon: 'http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=C|FF0000|000000'
});
于 2013-03-01T13:43:33.753 に答える