3
1

More than 5 years have passed since last update.

JavaScriptでGoogleMapAPIを使って、経路検索の結果を地図上に表示してみる

Last updated at Posted at 2018-12-09

はじめに

リアルタイムGTFSを使ったアプリケーションの作成の中で、使ったコードを
まとめています。

また今回の経路検索の表示が「GTFS」を何が関係があるかというと

各バス運行事業さんが「GTFS」のデータをGoogleさんに提供していることで
実現しています。
(GoogleMapでバスを使った経路検索ができることが前提ということです)

やること

2点間のルート検索をGoogleさんにしてもらい、それをGoogle map上に
ただ表示するだけです。

経路を案内するや、ユーザー指定で検索をするは次の課題としています。

さっそく

GTFSアプリでは、宇野バスさんのオープンデータを使っているので
2点間の経路検索は「岡山駅」→「岡山市役所」です。

GoogleMapの描画などはできている前提で書いています。

sample.js
var directionsService = new google.maps.DirectionsService;
var directionsRenderer = new google.maps.DirectionsRenderer;

directionsService.route({
    origin: new google.maps.LatLng(34.666608, 133.918170), //岡山駅
    destination: new google.maps.LatLng(34.655539, 133.919745), //岡山市役所  緯度経度でなくても名称でも検索可
    travelMode: google.maps.TravelMode.TRANSIT
}, function(response, status) {
    console.log(response);
    if (status === google.maps.DirectionsStatus.OK) {
        // ルート検索の結果を地図上に描画
        directionsRenderer.setMap(map);
        directionsRenderer.setDirections(response);
    }
  });

コメントでも書いてありますが、スタート(origin)とゴール(destination)は
「岡山駅」「岡山市役所」でも検索できます。
Googleさん素晴らしい。

また、
travelMode: google.maps.TravelMode.TRANSITの「TRANSIT」は
DRIVING,TRANSIT,WALKING,BICYCLINGに変更して検索もできます。

最後に

私自身の検索力や理解力によって、
力技の部分も多分にあり、さらにコードが汚いなどがあると思います。

訂正をかけていきますので、アドバイスもお願いします。

3
1
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
3
1