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

iOS 唤起原生地圖 App 顯示指定位置:構造 maps.google.com URL 並 openURL

     編輯:程式設計綜合問答

構造 Google Maps Web URL,包含標題、經緯度、縮放級別,用 UIApplication.shared.openURL 唤起系統地圖 App(或瀏覽器)。

代碼實現

NSString *title = @"目的地";
float latitude = 35.4634;
float longitude = 9.43425;
int zoom = 13;

NSString *urlString = [NSString stringWithFormat:
    @"https://maps.google.com/maps?q=%@@%1.6f,%1.6f&z=%d", 
    title, latitude, longitude, zoom];

NSURL *url = [NSURL URLWithString:urlString];
[[UIApplication sharedApplication] openURL:url];

參數說明

  • q=label@lat,lng:標記點標題與坐標。
  • z=zoom:縮放級別 (1-20)。
  • 經緯度格式 %1.6f 保留 6 位小數(約 0.1 米精度)。

現代替代:Apple Maps 深層鏈接

NSString *urlString = [NSString stringWithFormat:
    @"https://maps.apple.com/?q=%@&ll=%f,%f&z=%d", 
    title, latitude, longitude, zoom];
// 優先喚起 Apple Maps,無則瀏覽器打開 Google Maps

注意

  • iOS 10+ 需在 Info.plist 添加 LSApplicationQueriesSchemes 包含 maps,否則 canOpenURL 返回 false(但 openURL 通常仍可)。
  • 中文標題需 URL 編碼:[title stringByAddingPercentEncodingWithAllowedCharacters:.URLQueryAllowedCharacterSet]。

一行 URL 唤起地圖導航,無需集成複雜 SDK。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved