12
10

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 Mapsで2地点間の経路を描画するサンプル

Last updated at Posted at 2016-11-22

こんな感じ
image

APIキーだけは書き換えて下さい:

sample.html
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Google Mapsで2地点間の経路を描画するサンプル</title>

<script src="https://code.jquery.com/jquery-2.2.1.min.js"></script>
<script type="text/javascript" src="https://maps.googleapis.com/maps/api/js?key=●●●●●●●●●●"></script>

<script type="text/javascript">
  $(function() {
    var latlng1 = new google.maps.LatLng(35.705947, 139.651252);  // VAL研究所
    var latlng2 = new google.maps.LatLng(35.705469, 139.649610);  // 高円寺駅
    var map;
    var directionsService = new google.maps.DirectionsService();
    var directionsRenderer = new google.maps.DirectionsRenderer();
    
    // 地図初期化のオプション
    var mapOptions = {
        zoom: 17,
        center: latlng1,
        mapTypeId: google.maps.MapTypeId.ROADMAP,
        scaleControl: true,
    };
    // 地図を表示
    map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);

    // ルートを取得
    var request = {
      origin: latlng1,        // 出発地点の緯度、経度
      destination: latlng2,   // 到着地点の緯度、経度
      travelMode: google.maps.DirectionsTravelMode.WALKING // ルートの種類
    };
    directionsService.route(request, function(result, status) {
      directionsRenderer.setDirections(result); // 取得したルートをセット
      directionsRenderer.setMap(map); // ルートを地図に表示
    });
  });

</script>
</head>
<body>

  <div id="map_canvas" style="width: 100%; height: 600px;"></div>

</body>
</html>

WALKINGの部分は他にDRIVING(車), BICYCLING(自転車), TRANSIT(公共交通機関)が指定できる。
Directions Service  |  Google Maps JavaScript API  |  Google Developers

12
10
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
12
10

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?