Presentation is loading. Please wait.

Presentation is loading. Please wait.

Location Based Services - LBS Google Maps 路徑規劃與導航 1.

Similar presentations


Presentation on theme: "Location Based Services - LBS Google Maps 路徑規劃與導航 1."— Presentation transcript:

1 Location Based Services - LBS Google Maps 路徑規劃與導航 1

2 1.Google Maps Drawing 2.Google Maps 路徑規劃 3.Google Maps 導航 2

3 Google Maps Drawing Chapter1. 3

4 1. Google Maps Drawing APIs 簡介 Google Maps APIs 提供在地圖上 的繪圖功能 Polyline ( 折線 ) Polygon ( 多邊形 ) Circle ( 圈 ) 4

5 2. Google Maps Drawing APIs 5 此物件是 Google Maps API 的主要類別物件,提供控制地圖上 許多元件的方法 GoogleMap(Object) MoreMore.. final PolylineaddPolyline (PolylineOptions options) Adds a polyline to this map.

6 2. Google Maps Drawing APIs final CircleaddCircle (CircleOptions options) Add a circle to this map. 6 final PolygonaddPolygon (PolygonOptions options) Adds a polygon to this map. GoogleMap(Object)

7 2. Google Maps Drawing APIs 7 定義 Polyline 相關屬性 PolylineOptions (Object) MoreMore.. PolylineOptionsadd (LatLng point) Adds a vertex to the end of the polyline being built. PolylineOptionsaddAll (Iterable points) Adds vertices to the end of the polyline being built.

8 2. Google Maps Drawing APIs 8 PolylineOptionswidth (float width) Sets the width of the polyline in screen pixels. PolylineOptionscolor (int color) Sets the color of the polyline as a 32-bit ARGB color.

9 2. Google Maps Drawing APIs 9 定義 Polyline 相關屬性 PolygonOptions (Object) MoreMore.. PolygonOptionsadd (LatLng point) Adds a vertex to the outline of the polygon being built. PolygonOptionsaddAll (Iterable points) Adds vertices to the outline of the polygon being built.

10 2. Google Maps Drawing APIs 10 PolygonOptionsstrokeColor (int color) Specifies the polygon's stroke color, as 32-bit ARGB. PolygonOptionsstrokeWidth (float width) Specifies the polygon's stroke width, in display pixels.

11 2. Google Maps Drawing APIs 11 定義 Polyline 相關屬性 CircleOptions (Object) More.. CircleOptionscenter (LatLng center) Sets the center using a LatLng. CircleOptionsradius (double radius) Sets the radius in meters.

12 2. Google Maps Drawing APIs 12 CircleOptionsstrokeColor (int color) Sets the stroke color. CircleOptionsstrokeWidth (float width) Sets the stroke width. CircleOptions (Object)

13 3. Google Maps Lab2-1 專案建立 13 Google Maps Lab2-1 設定目標 於地圖繪製折線、多邊形、圓圈 搭配 ArrayList 實現多點陣列繪圖

14 3. Google Maps Lab2-1 專案建立 14 Google Maps Lab2-1 小叮嚀 ※ 可直接使用 lab1 專案修改 Google Maps APIs Key 申請 (Ch2_2) 相關 Metadata 設定 (Ch2_3-1) Import Google Maps Library (Ch2_3-2) 實作 Lab2-1 前,需先完成以下事項

15 3. Google Maps Lab2-1 專案建立 共啟用四組權限 INTERNET 網路存取權限 WRITE_EXTERNAL_STORAGE 允許 API 在裝置的外部儲存區 (sdcard) ACCESS_NETWORK_STATE 存取網路狀態的權限 READ_GSERVICES 允許 API 存取以網頁為基礎的服務 ( 此權限不在清單上, 需要手動建立, 請詳下一頁 ) 15

16 在其他 user-permission 底下貼上 READ_GSERVICES 權限 1 2 16

17 1 2 import com.google.android.gms.maps.CameraUpdateFactory; 3 import com.google.android.gms.maps.GoogleMap; 4 import com.google.android.gms.maps.MapFragment; 5 import com.google.android.gms.maps.model.CircleOptions; 6 import com.google.android.gms.maps.model.LatLng; 7 import com.google.android.gms.maps.model.MarkerOptions; 8 import com.google.android.gms.maps.model.PolygonOptions; 9 import com.google.android.gms.maps.model.PolylineOptions; 10 //Google Maps APIs 11 import android.os.Bundle; 3. Google Maps Lab2-1 專案建立 Android:MainActivity.java 17 打開 activity_main.xml

18 12 import android.app.Activity; 13 import android.graphics.Color; 14 import android.view.Menu; 15 public class MainActivity extends Activity { … 16 private GoogleMap map; 17 // 設定全域參數 18 19 protected void onCreate(Bundle savedInstanceState){ 20 super.onCreate(savedInstanceState); 21 setContentView(R.layout.activity_main); 22 creatMap(); 23 // 呼叫執行 creatMap Function 24 } 18

19 3. Google Maps Lab2-1 專案建立 1 private void creatMap() 2 { 3 MapFragment mf=(MapFragment)getFragmentManager().findFragmentById(R.id.map); 4 map = mf.getMap(); 5 // 將 MapFragment 設定給 GoogleMap 物件 6 LatLng stuLocation = new LatLng(22.763284, 120.376330); 7 LatLng Location1 = new LatLng(22.765026, 120.375257); 8 LatLng Location2 = new LatLng(22.762936, 120.373882); 9 LatLng Location3 = new LatLng(22.766217, 120.377391); 10 LatLng Location4 = new LatLng(22.759563, 120.377444); 11 // 設定五組經緯度位置並存入物件 19

20 12 13 map.addMarker(new MarkerOptions() 14.position(stuLocation) 15.title(" 樹德科技大學 ") 16.snippet("824 高雄市燕巢區橫山路 59 號 ")); 17 // 定義 stuLocation 座標相關屬性 18 map.addMarker(new MarkerOptions() 19.position(Location1) 20.rotation(180f) 21.title(" 測試地標 ") 22.snippet(" 測試地標 ")); 23 // 定義 Location1 座標相關屬性 24 20

21 25 26 map.addCircle(new CircleOptions(). 27 center(stuLocation). 28 radius(20). 29 strokeWidth(5). 30 strokeColor(Color.RED)); 31 // 在 stuLocation 中心座標繪製圓圈 32 map.addCircle(new CircleOptions(). 33 center(Location1). 34 radius(20). 35 strokeWidth(5). 36 strokeColor(Color.RED)); 37 // 在 Location1 中心座標繪製圓圈 21

22 38 39 map.addPolyline(new PolylineOptions(). 40 add(stuLocation,Location1). 41 width(5). 42 color(Color.RED)); 43 // 在 stuLocation, Location1 兩座標間繪製一條線 44 45 map.addPolygon(new PolygonOptions(). 46 add(Location2,Location3,Location4). 47 strokeWidth(5). 48 strokeColor(Color.GREEN)); 49 // 在 Location2, Location3, Location4 三座標間繪製一多邊形 50 22

23 51 52 ArrayList points = new ArrayList (); 53 points.add(new LatLng(22.764976, 120.364380)); 54 points.add(new LatLng(22.765521, 120.366515)); 55 points.add(new LatLng(22.760500, 120.367777)); 56 points.add(new LatLng(22.764691, 120.374834)); 57 // 透過 ArrayList 的方式來儲存座標物件 58 59 map.addPolyline(new PolylineOptions(). 60 addAll(points). 61 width(5). 62 color(Color.BLUE)); 63 // 在 addPolyline 可以使用 addAll 繪製所有座標的連線 23

24 64 for(int i=0;i<points.size();i++) 65 { 66 map.addCircle(new CircleOptions(). 67 center(points.get(i)). 68 radius(20). 69 strokeWidth(5). 70 strokeColor(Color.RED)); 71 } 72 // 使用迴圈將所有 ArrayList 儲存的座標物件繪製圓圈 73 map.animateCamera( 74 CameraUpdateFactory.newLatLngZoom(stuLocation,15.5f), 2000, null); 75 // 執行 Camera 的移動效果 76} 24

25 25 程式執行結果 3. Google Maps Lab2-1 專案建立

26 Google Maps 路徑規劃 Chapter2. 26

27 1. Google Maps 路徑規劃簡介 為起點與終點座標作路徑的規劃 27

28 1. Google Maps 路徑規劃簡介 路徑 : 給定起點與終點 路徑 : 給定起點、終點與中繼點 28 起點 終點終點 中繼點

29 2. Google Directions API Google Directions API 服務使用 Google 所開放的 Web Service 可計算起點與終點之間的路徑 29

30 2. Google Directions API Android Web Service Require Response 30 (JSON or XML) (Start Location,End Location) HTTP GET

31 2. Google Directions API 使用規範 31 規範內容 \ 版本 GeneralBusiness HTTP Require 2,500 次 100,000 次 中繼路點 8 個 23 個 開放性質免費需付費

32 2. Google Directions API 32 http://maps.googleapis.com/maps/api/directions/output?parameters Directions API Service URI https://maps.googleapis.com/maps/api/directions/output?parameters or

33 2-1. Google Directions API 參數介紹 必要參數 (GET) 33 參數名稱 Input origin 輸入要計算導航的 ( 起點 ) 地址或經 / 緯度文字值 destination 輸入要計算導航的 ( 終點 ) 地址或經 / 緯度文字值 sensor 指出導航要求的來源裝置是否附有位置感應器 (true or false)

34 2-1. Google Directions API 參數介紹 選用參數 (GET) 34 參數名稱 Input mode 指定計算路線時所要使用的交通模式,預設 driving ( 模式選擇請參考 2-2) waypoints 指定路點陣列。路點會透過讓路線行經指定位置來修改路線 alternatives 設定為 true 時,表示「導航」服務可在回應中提供一個以上的替代路線 More..

35 2-2. Google Directions API Mode 35 Mode 說明 driving 表示使用道路網的標準行車路線 walking 要求使用人行道與騎樓 ( 如可使用 ) 的步行路線 bicycling 要求使用單車道及專用道路 ( 如可使用 ) 的單車路線。 transit 要求使用大眾運輸路線 ( 如可使用 ) 的導航。

36 2-3. JSON 簡介 36 JSON ( JavaScript Object Notation )是一種輕量級的 資料交換語言,以文字為基礎,且易於讓人閱讀

37 2-3. JSON 簡介 37 e.g. {"subject":"Math"} {"key" : "Value"} JSON Object

38 2-3. JSON 簡介 38 ["Value", "Value", "Value“…] e.g. ["0", "4", "hello", "123"…] JSON Array

39 2-3. JSON 簡介 39 JSON Hybrid Array {"key" : ["Value", "Value", "Value“…]} e.g. {"subject": ["0", "4", "hello", "123"…]}

40 2-3. JSON 簡介 40 JSON Hybrid ["Value", {"key" : "Value"}, {"key" : "Value"}] e.g. ["0", {"subject":"Math"}, {"score":80}] Object

41 2-4. 測試 Google Directions API 41 http://maps.google.com/maps/api/directions/json ?origin= 樹德科大 &destination= 楠梓火車站 &language=zh-TW &sensor=true Click Parsing

42 2-5. Google Directions API Response 42 [] routes {} bounds copyrights [] legs {} overview_polyline summary [] warnings [] waypoint_order status 包含此路線的檢視區邊框 包含這個路線中要顯示的版權文字 指定路線中兩個位置之間路線的航段相關資訊 用來存放導航的概略路徑 包含路線的簡短文字說明 顯示這些警告訊息 指定計算的路線中任何路點的順序 More..

43 2-5. Google Directions API Response 43 [] routes {} overview_polyline points : " 編碼 " "overview_polyline": { "points": "a~l~Fjk~uOnzh@vlbBtc~@tsE`vnApw{A`dw@~w\\|tNtqf@l{Yd_Fblh@rxo@b}@xxSfytAbl k@xxaBeJxlcBb~t@zbh@jc|Bx}C`rv@rw|@rlhA~dVzeo@vrSnc}Axf]fjz@xfFbw~@dz{A~d {A|zOxbrBbdUvpo@`cFp~xBc`Hk@nurDznmFfwMbwz@bbl@lq~@loPpxq@bw_@v|{Cbt Y~jGqeMb{iF|n\\~mbDzeVh_Wr|Efc\\x`Ij{kE}mAb~uF{cNd}xBjp]fulBiwJpgg@|kHntyArpb@ bijCk_Kv~eGyqTj_|@`uV`k|DcsNdwxAott@r}q@_gc@nu`CnvHx`k@dse@j|p@zpiAp|gEic y@`omFvaErfo@igQxnlApqGze~AsyRzrjAb__@ftyB}pIlo_BflmA~yQftNboWzoAlzp@mz` @|}_@fda@jakEitAn{fB_a]lexClshBtmqAdmY_hLxiZd~XtaBndgC" }

44 2-6. JSON APIs 44 JSONObjectgetJSONObject (String name) Returns the value mapped by name if it exists and is a JSONObject. JSONArraygetJSONArray (String name) Returns the value mapped by name if it exists and is a JSONArray. JSONObject (Object) MoreMore..

45 2-6. JSON APIs 45 JSONObjectgetJSONObject (int index) Returns the value at index if it exists and is a JSONObject. JSONArraygetJSONArray (int index) Returns the value at index if it exists and is a JSONArray. JSONArray (Object) MoreMore..

46 2-7. Android Http Object 簡介 HttpGet (Object) 定義將要執行的 URI publicHttpGet (URI uri) Set URI. 46

47 2-7. Android Http Object 簡介 DefaultHttpClient (Object) 將設定好的 Request 傳送到 Server 上執行 abstract HttpResponseexecute (HttpUriRequest request) Executes a request using the default context. 47 MoreMore..

48 2-7. Android Http Object 簡介 HttpResponse (Object) 承接向 Server 發出 Request 後 Response 的結果 publicgetEntity (none) Obtains the message entity of this response, if any. abstract StatusLine getStatusLine (none) Obtains the status line of this response. 48 MoreMore..

49 3. Google Maps Lab2-2 專案建立 49 Google Maps Lab2-2 設定目標 透過 Google Directions 規劃路徑 處理回傳 JSON 物件並解析出路徑座標 將回傳路徑陣列繪製到地圖上

50 3. Google Maps Lab2-2 專案建立 50 Google Maps Lab2-2 小叮嚀 ※ 可直接使用 lab1 專案修改 Google Maps APIs Key 申請 (Ch2_2) 相關 Metadata 設定 (Ch2_3-1) Import Google Maps Library (Ch2_3-2) 實作 Lab2-2 前,需先完成以下事項

51 3. Google Maps Lab2-2 專案建立 共啟用四組權限 INTERNET 網路存取權限 WRITE_EXTERNAL_STORAGE 允許 API 在裝置的外部儲存區 (sdcard) ACCESS_NETWORK_STATE 存取網路狀態的權限 READ_GSERVICES 允許 API 存取以網頁為基礎的服務 ( 此權限不在清單上, 需要手動建立, 請詳下一頁 ) 51

52 在其他 user-permission 底下貼上 READ_GSERVICES 權限 1 2 52

53 1 2 import java.text.MessageFormat; 3 import java.util.ArrayList; 4 import org.apache.http.HttpResponse; 5 import org.apache.http.client.HttpClient; 6 import org.apache.http.client.methods.HttpGet; 7 import org.apache.http.impl.client.DefaultHttpClient; 8 import org.apache.http.util.EntityUtils; 9 import org.json.JSONArray; 10 import org.json.JSONObject; 11 //Http 相關物件與 JSON 資料處理相關物件 3. Google Maps Lab2-2 專案建立 Android:MainActivity.java 53 2 打開 activity_main.xml

54 12 import android.app.Activity; 13 import android.graphics.Color; 14 import android.os.Bundle; 15 import android.os.Handler; 16 import android.util.Log; 17 import android.view.Menu; 18 // ↑ 其他系統相關物件 ↓ Google Maps APIs 相關物件 19 import com.google.android.gms.maps.CameraUpdateFactory; 20 import com.google.android.gms.maps.GoogleMap; 21 import com.google.android.gms.maps.MapFragment; 22 import com.google.android.gms.maps.model.CircleOptions; 23 import com.google.android.gms.maps.model.LatLng; 24 import com.google.android.gms.maps.model.MarkerOptions; 25 import com.google.android.gms.maps.model.PolylineOptions; 54

55 1 public class MainActivity extends Activity { 2 public GoogleMap map; 3 public String startLocation = " 高雄火車站 "; 4 public String endLocation = "22.765002,120.364371"; 5 private Handler handler = new Handler(); 6 // 設定相關全域參數 7 protected void onCreate(Bundle savedInstanceState) { 8 super.onCreate(savedInstanceState); 9 setContentView(R.layout.activity_main); 10 setMap(); 11 startDirection(); 12 } 13 // 啟動執行 creatMap( 設定地圖 ) 與 startDirection( 開始規劃路線 ) 的兩個 function 14 55

56 15 private void setMap() 16 { 17 MapFragment mf=(MapFragment)getFragmentManager().findFragmentById(R.id.map); 18 map =mf.getMap(); 19 } 20 // 設定地圖相關物件 21 private void startDirection() 22 { 23 Thread theard = new Thread(){ 24 public void run(){GetDirection(startLocation,endLocation);} 25 }; 26 theard.start(); 27 } 28 // 啟動執行 creatMap( 設定地圖 ) 與 startDirection( 開始規劃路線 ) 的兩個 function 56

57 1 public void GetDirection(String startLocation, String endLocation) 2 { 3 String mapAPI = 4 "http://maps.google.com/maps/api/directions/json?" + 5 "origin={0}&destination={1}&language=zh-TW&sensor=true"; 6 String url = MessageFormat.format(mapAPI, startLocation, endLocation); 7 HttpGet get = new HttpGet(url); 8 // 設定 Google Directions APIs 網址與對應參數,並置入初始化 HttpGet 物件 9 try 10 { 11 HttpClient httpClient = new DefaultHttpClient(); 12 HttpResponse httpResponse = httpClient.execute(get); 13 // 對 Google Directions APIs 服務發出路徑規劃請求 14 57

58 15 if (httpResponse.getStatusLine().getStatusCode() == 200) 16 { 17 String strResult = EntityUtils.toString(httpResponse.getEntity()); 18 // 取得 Google Directions APIs 所有規劃路徑的相關資料 (JSON 格式 ) 19 JSONObject jsonObject = new JSONObject(strResult); 20 // 將整個 JSON 格式的資料置入初始化的 JSON 物件 21 JSONArray routeObject = jsonObject.getJSONArray("routes"); 22 // 取出第一層的陣列資料 (routes) 23 String polyline = routeObject.getJSONObject(0). 24 getJSONObject("overview_polyline").getString("points"); 25 // 取出 routes 陣列底下的 overview_polyline 物件內的 points ( 經編碼過後的資訊內容 ) 26 if (polyline.length() > 0) 27 { 28 drawPath(decodePoly(polyline)); 58

59 29 /* 30 先執行 decodePoly 將編碼的路徑位置轉換成為 ArrayList 並回傳給 31 drawPath function ,來給定所有路徑點並實現繪製路線的功能 32 */ 33 } 34 } 35 }catch (Exception e) 36 { 37 Log.e("map", "MapRoute:" + e.toString()); 38 39 } 40 } 41 42 59

60 1 private void drawPath(final ArrayList points) 2 { 3 Runnable r1 = new Runnable() { 4 public void run() { 5 map.addPolyline(new PolylineOptions(). 6 addAll(points). 7 width(5). 8 color(Color.BLUE)); 9 // 將所有陣列路徑點 (points) 置入折線繪圖的功能中 10 for(int i=0;i<points.size();i++) 11 { 12 map.addCircle(new CircleOptions(). 13 center(points.get(i)). 14 radius(20). 60

61 15 strokeWidth(5). 16 strokeColor(Color.RED)); 17 } 18 // 將所有陣列路徑點 (points) 標示圓圈,以利清楚觀察所有回傳的規劃路徑點 19 map.addMarker(new MarkerOptions() 20.position(points.get(0)) 21.title(" 我是起點 ") 22.snippet(" 我是起點 ")); 23 // 使用 Marker 來標示路徑的起點, points.get(0) -> 取所有陣列路徑點第一筆 ( 起點 ) 資訊 24 map.addMarker(new MarkerOptions() 25.position(points.get(points.size()-1)) 26.title(" 我是終點 ") 27.snippet(" 我是終點 ")); 28 /* 61

62 29 使用 Marker 來標示路徑的終點, 30 points.get(points.size()-1) -> 取所有陣列路徑點最後一筆 ( 終點 ) 資訊 31 */ 32 map.animateCamera(CameraUpdateFactory. 33 newLatLngZoom(points.get(0),15.5f), 2000, null); 34 // 執行 Camera 的移動效果,並以起點為中心點,縮放比例 15.5 ,播放時間長度為 2000 ms 35 } 36 }; 37 handler.post(r1); 38 } 39 40 41 42 62

63 1 private ArrayList decodePoly(String encoded) { 2 { 3 ArrayList poly = new ArrayList (); 4 int index = 0, len = encoded.length(); 5 int lat = 0, lng = 0; 6 while(index < len) { 7 int b, shift = 0, result = 0; 8 do{ 9 b = encoded.charAt(index++) - 63; 10 result |= (b & 0x1f) << shift; 11 shift += 5; 12 } while (b >= 0x20); 13 int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); 14 lat += dlat; 63

64 15 shift = 0; result = 0; 16 do{ b = encoded.charAt(index++) - 63; 17 result |= (b & 0x1f) << shift; 18 shift += 5; 19 } while (b >= 0x20); 20 int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); 21 lng += dlng; 22 LatLng p = new LatLng((((double) lat / 1E5)), 23 (((double) lng / 1E5))); 24 poly.add(p); 25 } 26 return poly; 27 } 28 } 64

65 65 程式執行結果 3. Google Maps Lab2-2 專案建立

66 Google Maps 導航 Chapter3. 66

67 1. Google Maps 導航簡介 結合 Google Maps 與 GPS 定位,引導通往目的地 所規畫路徑的走向。 67

68 2. Google Maps 導航功能考量 68 路徑規劃 當前位置 終點終點 只需要設定一個終點 ( 目的地 ) ,起點設為當前位置

69 2. Google Maps 導航功能考量 69 GPS 高精準定位點 當前位置 終點終點 隨著物體的移動, GPS 需要具備高精準度的定位效果 來判斷定位點位移的位置

70 2. Google Maps 導航功能考量 70 Google Maps 視覺角度 Camera Position Map Position

71 2. Google Maps 導航功能考量 71 Google Maps 視覺角度 俯視角 側視角

72 2. Google Maps 導航功能考量 72 Google Maps 判斷方位旋轉地圖 地圖沒旋轉 地圖有旋轉 右彎前

73 2-1. Google Maps 導航結合方向感測器 73 北 =0 東 =90 西 =270 南 =180 水平方向 水平垂直方向 Value[0] = 方位角 Value[1] = 傾斜角 Value[2] = 旋轉角 方向感測器回傳值

74 2-2. Android LBS APIs Criteria (Object) 承接向 Server 發出 Request 後 Response 的結果 voidsetAccuracy (int accuracy) Indicates the desired accuracy for latitude and longitude. voidsetCostAllowed (boolean costAllowed) Indicates whether the provider is allowed to incur monetary cost. 74 MoreMore..

75 voidsetPowerRequirement (int level) Indicates the desired maximum power level. voidsetSpeedRequired (boolean speedRequired) Indicates whether the provider must provide speed information. 75 voidsetAltitudeRequired (boolean altitudeRequired) Indicates whether the provider must provide altitude information.

76 2-3. Google Maps Camera APIs 76 此物件是 Google Maps API 的主要類別物件,提供控制地圖上 許多元件的方法 GoogleMap(Object) MoreMore.. final voidmoveCamera (CameraUpdate update) Repositions the camera according to the instructions defined in the update. final voidsetMyLocationEnabled (boolean enabled) Enables or disables the my-location layer.

77 2-3. Google Maps Camera APIs CameraUpdateFactory (Object) 此物件主要用於控制地圖 CameraUpdate 的相關動作 MoreMore.. 77 static CameraUpdatenewCameraPosition (CameraPosition cameraPosition) Returns a CameraUpdate that moves the camera to a specified CameraPosition.

78 2-3. Google Maps Camera APIs CameraPosition.Builder (Object) 建立 Camera 相關屬性,包含位置、方位、傾斜、遠近 78 CameraPosition.Builderbearing (float bearing) Sets the direction that the camera is pointing in, in degrees clockwise from north. CameraPosition.Buildertarget (LatLng location) Sets the location that the camera is pointing at.

79 2-3. Google Maps Camera APIs 79 CameraPosition.Buildertilt (float tilt) Sets the angle, in degrees, of the camera from the nadir (directly facing the Earth). CameraPosition.Builderzoom (float zoom) Sets the zoom level of the camera. CameraPositionbuild (none) Builds a CameraPosition.

80 3. Google Maps Lab2-3 專案建立 80 Google Maps Lab2-3 設定目標 透過 Google Directions 規劃導航路徑 結合 GPS 定位更新定位點 方向感測器結合 Camera 鏡頭旋轉功能 終點終點

81 3. Google Maps Lab2-3 專案建立 81 Google Maps Lab2-3 小叮嚀 ※ 可直接使用 lab2-2 專案修改 Google Maps APIs Key 申請 (Ch2_2) 相關 Metadata 設定 (Ch2_3-1) Import Google Maps Library (Ch2_3-2) 實作 Lab2-3 前,需先完成以下事項 終點終點

82 3. Google Maps Lab2-3 專案建立 共啟用七組權限 82 INTERNET 網路存取權限 WRITE_EXTERNAL_STORAGE 允許 API 在裝置的外部儲存區 (sdcard) ACCESS_NETWORK_STATE 存取網路狀態的權限 READ_GSERVICES 允許 API 存取以網頁為基礎的服務 ( 此權限不在清單上, 需要手動建立, 請詳下二頁 )

83 3. Google Maps Lab2-3 專案建立 83 ACCESS_FINE_LOCATION GPS 定位權限 ACCESS_COARSE_LOCATION 無線網路定位 ACCESS_GPS GPS 存取權限

84 1 2 import java.text.MessageFormat; 3 import java.util.ArrayList; 4 import org.apache.http.HttpResponse; 5 import org.apache.http.client.HttpClient; 6 import org.apache.http.client.methods.HttpGet; 7 import org.apache.http.impl.client.DefaultHttpClient; 8 import org.apache.http.util.EntityUtils; 9 import org.json.JSONArray; 10 import org.json.JSONObject; 11 //Http 相關物件與 JSON 資料處理相關物件 3. Google Maps Lab2-3 專案建立 Android:MainActivity.java 84 3 打開 activity_main.xml

85 12 import android.app.Activity; 13 import android.graphics.Color; 14 import android.os.Bundle; 15 import android.os.Handler; 16 import android.util.Log; 17 import android.view.Menu; 18 import android.content.Context; 19 import android.view.WindowManager; 20 import android.widget.Toast; 21 // ↑ 其他系統相關物件 22 import android.hardware.Sensor; 23 import android.hardware.SensorEvent; 24 import android.hardware.SensorEventListener; 25 import android.hardware.SensorManager; 85

86 26 // 感測器相關物件 27 import android.location.Criteria; 28 import android.location.Location; 29 import android.location.LocationListener; 30 import android.location.LocationManager; 31 // 定位相關物件 32 import com.google.android.gms.maps.CameraUpdateFactory; 33 import com.google.android.gms.maps.GoogleMap; 34 import com.google.android.gms.maps.MapFragment; 35 import com.google.android.gms.maps.model.CircleOptions; 36 import com.google.android.gms.maps.model.LatLng; 37 import com.google.android.gms.maps.model.MarkerOptions; 38 import com.google.android.gms.maps.model.PolylineOptions; 39 // Google Maps APIs 相關物件 86

87 1 public class MainActivity extends Activity { 2 public GoogleMap map; 3 public String startLocation; 4 public String endLocation = " 楠梓火車站 "; 5 private Handler handler = new Handler(); 6 private LocationManager locationManager; 7 static LatLng MyLocation; 8 private SensorManager mSensorManager=null; 9 private Sensor mSensor=null; 10 float azimuth; 11 // 設定相關全域參數 12 protected void onCreate(Bundle savedInstanceState) { 13 super.onCreate(savedInstanceState); 14 setContentView(R.layout.activity_main); 87

88 15 init(); 16 setLocationManager(); 17 startDirection(); 18 } 19 /* 啟動執行 init ( 初始化感測器、定位、 Map 等功能 ) 20 setLocationManager( 設定與讀取定位相關物件 ) 21 startDirection( 規劃導航路徑 ) 22 */ 23 private void init() { 24 getWindow().setFlags( 25 WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, 26 WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); 27 // 設定成為不休眠的狀態 ( 導航進行中應呈現不休眠狀態 ) 28 88

89 29 mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE); 30 mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ORIENTATION); 31 // 取得方向感測器物件 32 mSensorManager.registerListener( 33 mSensorEventListener, 34 mSensor, 35 SensorManager.SENSOR_DELAY_NORMAL); 36 // 監聽方向感測器物件的狀態,呼叫 mSensorEventListener 監聽事件 37 MapFragment mf = 38 (MapFragment)getFragmentManager().findFragmentById(R.id.map); 39 map = mf.getMap(); 40 // 設定 Google Maps 相關物件 41 map.setMyLocationEnabled(true); 42 // 啟用定位點 ( 當前位置 ) 的藍色指標 89

90 43 locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 44 Location mLocation = getLocation(this); 45 startLocation = mLocation.getLatitude()+","+mLocation.getLongitude(); 46 // 取得定位相關物件,並取得當前經緯度設定給起始點位置 (startLocation) 47 private Location getLocation(Context context) { 48 LocationManager locMan = (LocationManager) context. 49 getSystemService(Context.LOCATION_SERVICE); 50 Location location = locMan. 51 getLastKnownLocation(LocationManager.GPS_PROVIDER); 52 if (location == null){ 53 location = locMan. 54 getLastKnownLocation(LocationManager.NETWORK_PROVIDER); 55 return location; 56 } 90

91 57 // 回傳當前定位物件的方法 58 private void setLocationManager() 59 { 60 Criteria criteria = new Criteria(); 61 criteria.setAccuracy(Criteria.ACCURACY_FINE); 62 // 獲取較精準位置 63 criteria.setCostAllowed(true); 64 // 允許產生系統消耗 65 criteria.setPowerRequirement(Criteria.POWER_HIGH); 66 // 消耗大的話,獲取的頻率高 67 criteria.setSpeedRequired(true); 68 // 設定是否需要提供速度的資訊 69 criteria.setAltitudeRequired(true); 70 // 設定是否需要提供海拔的資訊 91

92 71 String bestProvider = locationManager.getBestProvider(criteria, true); 72 locationManager.requestLocationUpdates(bestProvider, 1000, 1, locationListener); 73 // 取得最佳條件的 Provider ,並取得目前位置給 MyLocation 74} 75 76 77 78 private final SensorEventListener mSensorEventListener =new SensorEventListener() 79 { 80 public void onSensorChanged(SensorEvent event) { 81 if(event.sensor.getType()==Sensor.TYPE_ORIENTATION){ 82 azimuth = event.values[0]; 83 // 方位角 (0 到 359) 0= 北, 90= 東, 180= 南, 270= 西 84 } 92

93 85 } 86 public void onAccuracyChanged(Sensor sensor, int accuracy) { 87 } 88 }; 89 private final LocationListener locationListener = new LocationListener(){ 90 public void onLocationChanged(Location location) { 91 if (location != null) { 92 MyLocation = new LatLng(location.getLatitude(), location.getLongitude()); 93 // 當地點改變時取得經緯度存到 MyLocation 變數中 94 map.moveCamera(CameraUpdateFactory. 95 newCameraPosition(new CameraPosition.Builder() 96.target(MyLocation) 97.zoom(19) 98.bearing(azimuth) 93

94 99.tilt(60) 100.build())); 101 /* target: 目標 ( 攝影機中心點 ) 102 bearing: 定位角 ( 接方向感測器輸出的角度值 ) 103 zoom: 放大 19 Level 104 tilt: 傾斜視角 105 */ 106 } 107 } 108 public void onProviderDisabled(String arg0){ 109 // 當 GPS 或網路定位功能關閉時 110 } 111 public void onProviderEnabled(String arg0) { 112 // 當 GPS 或網路定位功能開啟 94

95 113 } 114 public void onStatusChanged(String arg0, int arg1, Bundle arg2) { 115 // 定位狀態改變 116 } 117 } 118 // 設定地圖相關物件 119 private void startDirection() 120 { 121 Thread theard = new Thread(){ 122 public void run(){GetDirection(startLocation,endLocation);} 123 }; 124 theard.start(); 125 } 126 // 啟動執行 creatMap( 設定地圖 ) 與 startDirection( 開始規劃路線 ) 的兩個 function 95

96 1 public void GetDirection(String startLocation, String endLocation) 2 { 3 String mapAPI = 4 "http://maps.google.com/maps/api/directions/json?" + 5 "origin={0}&destination={1}&language=zh-TW&sensor=true"; 6 String url = MessageFormat.format(mapAPI, startLocation, endLocation); 7 HttpGet get = new HttpGet(url); 8 // 設定 Google Directions APIs 網址與對應參數,並置入初始化 HttpGet 物件 9 try 10 { 11 HttpClient httpClient = new DefaultHttpClient(); 12 HttpResponse httpResponse = httpClient.execute(get); 13 // 對 Google Directions APIs 服務發出路徑規劃請求 14 96

97 15 if (httpResponse.getStatusLine().getStatusCode() == 200) 16 { 17 String strResult = EntityUtils.toString(httpResponse.getEntity()); 18 // 取得 Google Directions APIs 所有規劃路徑的相關資料 (JSON 格式 ) 19 JSONObject jsonObject = new JSONObject(strResult); 20 // 將整個 JSON 格式的資料置入初始化的 JSON 物件 21 JSONArray routeObject = jsonObject.getJSONArray("routes"); 22 // 取出第一層的陣列資料 (routes) 23 String polyline = routeObject.getJSONObject(0). 24 getJSONObject("overview_polyline").getString("points"); 25 // 取出 routes 陣列底下的 overview_polyline 物件內的 points ( 經編碼過後的資訊內容 ) 26 if (polyline.length() > 0) 27 { 28 drawPath(decodePoly(polyline)); 97

98 29 /* 30 先執行 decodePoly 將編碼的路徑位置轉換成為 ArrayList 並回傳給 31 drawPath function ,來給定所有路徑點並實現繪製路線的功能 32 */ 33 } 34 } 35 }catch (Exception e) 36 { 37 Log.e("map", "MapRoute:" + e.toString()); 38 39 } 40 } 41 42 98

99 1 private void drawPath(final ArrayList points) 2 { 3 Runnable r1 = new Runnable() { 4 public void run() { 5 map.addPolyline(new PolylineOptions(). 6 addAll(points). 7 width(5). 8 color(Color.BLUE)); 9 // 將所有陣列路徑點 (points) 置入折線繪圖的功能中 10 for(int i=0;i<points.size();i++) 11 { 12 map.addCircle(new CircleOptions(). 13 center(points.get(i)). 14 radius(20). 99

100 15 strokeWidth(5). 16 strokeColor(Color.RED)); 17 } 18 // 將所有陣列路徑點 (points) 標示圓圈,以利清楚觀察所有回傳的規劃路徑點 19 map.addMarker(new MarkerOptions() 20.position(points.get(0)) 21.title(" 我是起點 ") 22.snippet(" 我是起點 ")); 23 // 使用 Marker 來標示路徑的起點, points.get(0) -> 取所有陣列路徑點第一筆 ( 起點 ) 資訊 24 map.addMarker(new MarkerOptions() 25.position(points.get(points.size()-1)) 26.title(" 我是終點 ") 27.snippet(" 我是終點 ")); 28 // 使用 Marker 來標示路徑的終點, 100

101 29 CameraPosition cameraPosition = new CameraPosition.Builder() 30.target(points.get(0)) 31.zoom(19) 32.bearing(270) 33.tilt(60) 34.build(); 35 36 map.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition)); 37 38 // 執行 Camera 的移動效果,並以起點為中心點,縮放比例 15.5 ,播放時間長度為 2000 ms 39 } 40 }; 41 handler.post(r1); 42 } 101

102 1 private ArrayList decodePoly(String encoded) { 2 { 3 ArrayList poly = new ArrayList (); 4 int index = 0, len = encoded.length(); 5 int lat = 0, lng = 0; 6 while(index < len) { 7 int b, shift = 0, result = 0; 8 do{ 9 b = encoded.charAt(index++) - 63; 10 result |= (b & 0x1f) << shift; 11 shift += 5; 12 } while (b >= 0x20); 13 int dlat = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); 14 lat += dlat; 102

103 15 shift = 0; result = 0; 16 do{ b = encoded.charAt(index++) - 63; 17 result |= (b & 0x1f) << shift; 18 shift += 5; 19 } while (b >= 0x20); 20 int dlng = ((result & 1) != 0 ? ~(result >> 1) : (result >> 1)); 21 lng += dlng; 22 LatLng p = new LatLng((((double) lat / 1E5)), 23 (((double) lng / 1E5))); 24 poly.add(p); 25 } 26 return poly; 27 } 28 } 103

104 104 程式執行結果 3. Google Maps Lab2-3 專案建立


Download ppt "Location Based Services - LBS Google Maps 路徑規劃與導航 1."

Similar presentations


Ads by Google