核心 URL Scheme
baidumap://map/poi/search?query=關鍵字&location=緯度,經度&radius=半徑(米)&src=你的App名稱
參數詳細表
| 參數 | 必填 | 說明 | 範例 |
| query | 是 | 搜尋關鍵字(URL Encode) | query=學校 |
| location | 否 | 中心點座標,預設為當前定位 | location=39.915,116.404 |
| radius | 否 | 搜尋半徑(米),預設 2000 | radius=5000 |
| src | 是 | 呼叫端 App 名稱(百度審核用) | src=MyApp |
| coord_type | 否 | 座標類型:gcj02(預設)/bd09ll/wgs84 | coord_type=gcj02 |
Swift 完整程式碼
func openBaiduMapSearch(keyword: String, latitude: Double, longitude: Double, radius: Int = 2000) {
let encodedKeyword = keyword.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)!
let urlString = "baidumap://map/poi/search?query=\(encodedKeyword)&location=\(latitude),\(longitude)&radius=\(radius)&src=MyApp"
guard let url = URL(string: urlString),
UIApplication.shared.canOpenURL(url) else {
// 未安裝百度地圖 → 導向 App Store 或 Web 版
let webUrl = "https://map.baidu.com/search/\(encodedKeyword)/"
UIApplication.shared.open(URL(string: webUrl)!)
return
}
UIApplication.shared.open(url, options: [:], completionHandler: nil)
}
Info.plist 必加白名單
LSApplicationQueriesSchemes
baidumap
官方文件連結
常見問題
- ❌
canOpenURL 回傳 false → 未加 LSApplicationQueriesSchemes 或未安裝百度地圖
- ❌ 搜尋無結果 → 關鍵字編碼錯誤、座標系統不符(確認用 GCJ-02)
- ✅ 建議提供「在瀏覽器開啟」備選,覆蓋未安裝用戶