4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Google map APIで複数地点に線を引きたいとき

Last updated at Posted at 2015-05-09

mapsの Polyline(polyLineOptions) を利用します。

手順としては

  1. 線を引きたい座標を用意する
    LatLng(35.701139, 139.797949)

  2. 線を引くための各種情報をセットする
    var polyLineOptions = {}

  3. 線を引く
    var poly = new google.maps.Polyline(polyLineOptions);
    poly.setMap(map);

以上3ステップ。

ちなみに今回は複数地点の座標を既に設定していたため、
線を引く際に設定が必要なpathのために再度配列化しています。
(代入するのは配列でないといけないみたいです)

// 複数地点の座標
var map_data = new Array();
map_data.push({position: new google.maps.LatLng(35.701139, 139.797949), place:'東京' });
map_data.push({position: new google.maps.LatLng(10.384458,123.798679), place:'フィリピン' });
map_data.push({position: new google.maps.LatLng(60.173468, 24.941046), place:'フィンランド' });
map_data.push({position: new google.maps.LatLng(-10.688480, -75.968200), place:'ペルー' });
map_data.push({position: new google.maps.LatLng(39.625702, -100.213340), place:'アメリカ' });

//  ラインの道筋(path)を指定するために配列を作成 
//  以前のmap_dataに入れたオブジェクトを利用
var points = new Array();
for (i =0; i < map_data.length; i++) {
    points.push(map_data[i].position);
}

function initialize() {

    var mapOptions = {
      zoom: 2,
      center: map_data[0].position,
    };

    map = new google.maps.Map(document.getElementById('gt_map'),mapOptions);

  for (i =0; i < map_data.length; i++) {

    var marker = new google.maps.Marker({
        position: map_data[i].position,
        map: map,
        icon: {
                  url: "icon.png",
                  // scalesizeでアイコンのサイズを設定
                  scaledSize: new google.maps.Size(10,10)
                }
    });

  }
  //  /for

   // ライン情報を設定
    var polyLineOptions = {
        path: points,
        strokeWeight: 1,
        strokeColor: "#0000ff",
        strokeOpacity: "0.5"
    };

   // ラインを出力
    var poly = new google.maps.Polyline(polyLineOptions);
    poly.setMap(map);

}
// /initialize

 google.maps.event.addDomListener(window, 'load', initialize);
4
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?