2013年9月14日 星期六

[Google]淺談Google Directions API

Google Directions API是最近使用到的google另一項服務
簡單來說,這項服務功用就是
當我們在規劃路線(從A地到B地),所規劃出來的路線
Google Directions API會將路線中的"折線",拆成一段一段的直線回報經緯度給我們

<舉例>
出發地點 : 清華大學 24.79608,120.98709 (NTHU)
目的地 : 新竹火車站 24.80237,120.97206 (Hsinchu train station)

那麼規劃出來的路線就會下圖所示



這項Google API 所提供的查詢網址如下 : 

"http://maps.googleapis.com/maps/api/directions/xml?origin="+lat+","+lng+"&destination="+lat+","+lng+"&sensor=false&units="+units+"&mode="+mode

有三項是必填的
1. origin:start GPS location (起始位置)
2. destination:end GPS location (結束位置)
3. sensor:指出導航要求的來源裝置是否附有位置感應器,這個值只有true跟false兩種

而後面有兩項是其他種可以填的屬性,簡單介紹
1. unit: 這項屬性是制訂單位使用的,有metric和imperial兩種,主要就是公制單位跟英制單位的差別
2.mode : 代表我們規劃路線所使用的交通工具,有四種
(1).driving(預設)(2).walking (3).bicycling (4).transit

: 想看其他更詳細的介紹可以點進去官網連結-->Google Directions API官網

所以套用到我們一開始舉的例子
那我們的範例網址就如下 :

"http://maps.googleapis.com/maps/api/directions/xml?origin=24.79608,120.98709&destination=24.80237,120.97206&sensor=false&units=metric&mode=driving"

連結到網頁的結果有兩種輸入格式可以選擇
一個昰json檔,一個昰xml檔



有紅色跟綠色畫線起來的地方
就是拆成每段直線後的小起點和小終點
這項服務的優點是,不用自己申請一個key就能使用了,挺方便的

延伸閱讀:
[Google]淺談Google Place API



Related Articles

0 意見:

張貼留言

技術提供:Blogger.