LoginSignup
5
5

More than 5 years have passed since last update.

Google Apps ScriptのDirectionFinderを使う

Last updated at Posted at 2012-12-14

Google Apps ScriptでDirectionFinderを使う。

実は、書籍を執筆していて、現在校正作業中のステータスです。(多分、発売は新年あけてからになると思います。)
書籍の中でMapsサービスについても記述しています。

で、時間的リソースの関係でDirectionFinderについての記述ができないと思っていたのですが、
少しだけ書けてしまったので、書籍に書かれていない内容を紹介しようと思います。

DirectionFinder?

DirectionFinderとは、Google Directions APIをラップしたもので
始点と終点を指定して、その間のルート情報を取得するものです。
Directions APIのドキュメント:https://developers.google.com/maps/documentation/directions/

とりあえず、書籍のコードと同等のコード。

コード.gs(最初の例)
function myFunction() {
  var finder = Maps.newDirectionFinder();

  // 岡山駅からGoogleJapanオフィスまで
  finder.setOrigin("岡山駅");
  finder.setDestination("六本木ヒルズ");

  // 結果の取得
  var obj = finder.getDirections();

  printResult(obj);
}

function printResult(obj) {
  Logger.log("status = %s", obj.status);

  Logger.log("summary = %s", obj.routes[0].summary);
  Logger.log("bounds = %s", obj.routes[0].bounds);
  Logger.log("copyrights = %s", obj.routes[0].copyrights);
  Logger.log("waypoint_order = %s", obj.routes[0].waypoint_order);
  Logger.log("legs = %s", obj.routes[0].legs);
  Logger.log("warnings = %s", obj.routes[0].warnings);
  Logger.log("overview_polyline = %s", obj.routes[0].overview_polyline);
}

これは、岡山駅から六本木ヒルズ(Google Japan オフィス)まで行くぜ!と
思った時にどんな道のりになるのかを取得しています。

実行結果

で、取得されるログ(ログウインドウ)ですが、
status = OK
summary = ���������������������
bounds = {southwest={lng=133.88904, lat=34.66627}, northeast={lng=139.72843, lat=35.66176}}
copyrights = Map data ��2012 Google, ZENRIN
waypoint_order = []
legs = [{distance={text=660 km, value=660156}, duration={text=7 hours 56 mins, value=28536}, end_location={lng=139.72843, lat=35.66070000000001}, start_address=Okayama Station, ��� Ekimotomachi, Kita Ward, Okayama, Okayama Prefecture, Japan, end_address=Roppongi Hills, ���������-������ Roppongi, Minato, Tokyo 106-6108, Japan
(以下略)

文字化けしてしまって、ログウインドウでは何が書いてあるのかわかりません…

(書籍に書けなかった事。その1)移動モードを変えてみよう!

移動モードが、Directions APIの初期値のままなので、このルート情報はDriving(車移動)でのルートが出力されています。やっぱり公共交通機関で移動する事が多いでしょうからtransit(公共交通機関)での情報を出力…。したいのですが、なぜか、
Maps.DirectionFinder.Modeには、DRIVING(自動車)、BICYCLING(自転車)、WALKING(歩き)の3つしかありません。補完機能にも出ず、ドキュメントもなく、Stringっぽいので"transit"なんて指定すると、
「無効な引数」…だそうです。

コード

コード.gs(移動モード追加)
function myFunction() {
  var finder = Maps.newDirectionFinder();

  // 岡山駅からGoogleJapanオフィスまで
  finder.setOrigin("岡山駅");
  finder.setDestination("六本木ヒルズ");

  // 移動モードの設定
  finder.setMode(Maps.DirectionFinder.Mode.WALKING);

  // 結果の取得
  var obj = finder.getDirections();

  printResult(obj);
}

出力結果

(省略)

(書籍に書けなかった事。その2)そうだ!言語設定を変えよう

言語設定のデフォルトは(多分"en")なので、日本語にしてみようと思います。

コード.gs(言語設定追加)
function myFunction() {
  var finder = Maps.newDirectionFinder();

  // 岡山駅からGoogleJapanオフィスまで
  finder.setOrigin("岡山駅");
  finder.setDestination("六本木ヒルズ");

  // 移動モードの設定
  finder.setMode(Maps.DirectionFinder.Mode.WALKING);

  // 言語設定
  finder.setLanguage("ja");

  var obj = finder.getDirections();

  printResult(obj);
}

実行結果

もっと文字化けがひどくなったので省略(恐らく、日本語情報が増えた)

まとめ

書籍にも書いてあるのですが、ログ出力だと文字化けして日本語の文字が見えない。
他のものに出力したら話は別です。

Maps.DirectionFinder.Mode.TRANSITを増やしてください。なぜ無効な引数なんでしょうか。

と、いったようにいろいろなAPIが実行できるようになっているので
Google Apps Scriptは非常に便利です。

5
5
2

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
5
5