首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> 程式設計綜合問答

iOS 地圖導航:從當前位置到大頭針繪製路線

     編輯:程式設計綜合問答

方案一:啟動 Google Maps App(最簡單)

CLLocationCoordinate2D start = {currentLat, currentLng};
CLLocationCoordinate2D dest = {pinLat, pinLng};
NSString *urlStr = [NSString stringWithFormat:
    @"https://maps.google.com/?saddr=%f,%f&daddr=%f,%f",
    start.latitude, start.longitude, dest.latitude, dest.longitude];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:urlStr]];

優點:零開發成本、導航體驗完整;缺點:跳出 App、需安裝 Google Maps。

方案二:App 內繪製路線(使用 Google Directions API)

  1. 請求 https://maps.googleapis.com/maps/api/directions/json?origin=...&destination=...&key=YOUR_KEY
  2. 解析回傳 routes[0].overview_polyline.points(編碼後的路徑點)
  3. 解碼 Polyline 為 CLLocationCoordinate2D 陣列
  4. 建立 MKPolyline 加入 MKMapView 顯示

開源庫推薦

Demo 參考

Code4App 地圖路徑 Demo

注意

  • Google Directions API 需啟用計費帳戶,免費額度有限
  • iOS 內建 MKDirections (iOS 7+) 可離線/免費計算路徑,但僅支援駕車/步行,無大眾運輸
  1. 上一篇文章:
  2. 下一篇文章:
Struts2 ` s:textarea ` 與原生 ` textarea ` 差異解析 Struts2 `` 與原生 `