Help us understand the problem. What is going on with this article?

【Google Maps JavaScript API v3】すべてのマーカーを地図の中に収める【LatLngBounds】

More than 3 years have passed since last update.

デモ:http://mo49.tokyo/qiita/20160517/bounds.html

var locations = [
   ['東京タワー', 35.658581, 139.745433],
   ['スカイツリー', 35.789966, 139.821961],
   ['東京ドーム', 35.705471, 139.751801],
   ['ランドマークタワー', 35.454948, 139.631429]
];

var map = new google.maps.Map(document.getElementById('map'));
var bounds = new google.maps.LatLngBounds();
var infoWindow = new google.maps.InfoWindow();
var marker;

for (var i = 0; i < locations.length; i++) {
  marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
    map: map
  });
  // 地図表示領域をマーカー位置に合わせて拡大
  bounds.extend (marker.position);

  google.maps.event.addListener(marker, 'click', (function(marker, i) {
    return function() {
      infoWindow.setContent('観光スポット名:' + '<br>' + locations[i][0]);
      infoWindow.open(map, marker);
    }
  })(marker, i));
}

// 引数に指定した矩形領域を地図に収める
map.fitBounds (bounds);

参考
LatLngBoundsクラスの解説
fitBounds() - Mapクラスのメソッド
GoogleMap v3 マーカー位置にあわせて表示領域を自動調整する

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした