ThirdPartyMapper
使用自定义 URL 协议或辅助方法与第三方 iOS 地图客户端交互,允许用户选择他们喜欢的地图客户端。
示例
支持的地图客户端
客户端 | URL 协议 | App Store |
---|---|---|
Apple Maps | maps |
链接 |
Google Maps | comgooglemaps |
链接 |
Waze | waze |
链接 |
更多即将到来... |
遗憾的是,并非所有地图客户端都提供 URL 协议供 ThirdPartyMapper
支持。如果您知道其他候选者,请 告诉我们。
如何安装
Swift 包管理器
在 Xcode 中,点击“文件”菜单,选择“Swift 包”,然后点击“添加依赖关系…”,接着输入此仓库的 URL
https://github.com/lordzsolt/ThirdPartyMapper.git
CocoaPods
使用 CocoaPods,只需将 ThirdPartyMapper 添加到 Podfile 中
pod 'ThirdPartyMapper'
或者,您可以从源文件夹手动导入文件。
使用方法
重要
为了让 第三方映射器 能够正常工作,您需要在项目的 Info.plist
文件中声明您希望使用的应用的 URL 方案。
<key>LSApplicationQueriesSchemes</key>
<array>
<string>comgooglemaps</string>
<string>waze</string>
</array>
isExactLocation
标志
您会注意到,大多数方法都有 isExactLocation
参数。这是为了指定是否应使用 searchQuery
将用户导航到确切的位置,例如地址。
如果您想在一个地图上突出显示一个 general 区域,如加利福尼亚,将 false
传递给此参数。这项功能可能并不适用于所有应用。
获取支持客户端列表
let clients = ThirdPartyMapper.allowedClients
重新排序或更改客户端列表
有时,根据应用可供的地理区域,您可能决定以不同的顺序显示客户端列表,或显示您选择的特定列表。这就是为什么 ThirdPartyMapper.allowedClients
可以公开设置。
ThirdPartyMapper.allowedClients = [.waze, .googleMaps]
检索已安装客户端列表
let installedClients = ThirdPartyMapper.installedClients(
searchQuery: "One Apple Park Way, Cupertino, CA 95014, United States",
isExactLocation: true)
在UIKit中显示客户端选择器操作表
ThirdPartyMapper.openClientPicker(
searchQuery: "One Apple Park Way, Cupertino, CA 95014, United States",
isExactLocation: true,
on: viewController)
在SwiftUI(iOS 15.0)中显示客户端选择器确认对话框
struct ContentView: View {
private static let appleCampusAddress = "One Apple Park Way, Cupertino, CA 95014, United States"
@State var isMapPickerShown = false
var body: some View {
VStack {
Button("Apple Campus") {
isMapPickerShown = true
}
.buttonStyle(.bordered)
.frame(height: 40)
}
.mapPicker(
searchQuery: Self.appleCampusAddress,
isExactLocation: true,
isPresented: $isConfirmationDialogShown)
}
}
在SwiftUI(iOS 15.0前)中显示客户端选择器操作表
struct ContentView: View {
private static let appleCampusAddress = "One Apple Park Way, Cupertino, CA 95014, United States"
@State var isMapPickerShown = false
var body: some View {
VStack {
Button("Apple Campus") {
isMapPickerShown = true
}
.buttonStyle(.bordered)
.frame(height: 40)
}
.mapPickerActionSheet(
searchQuery: Self.appleCampusAddress,
isExactLocation: true,
isPresented: $isConfirmationDialogShown)
}
}
需求
ThirdPartyMapper使用Swift 5.0编写,需要iOS 11.0及以上,Xcode 10.2及以上。
感谢
ThirdPartyMapper由Zsolt Kovacs创建。
该项目灵感来源于ThirdPartyMailer。
许可
ThirdPartyMapper 可在 MIT 许可证下使用。更多信息请参阅 LICENSE 文件。