PXGoogleDirections
iOS的Google Directions API SDK,完全用Swift编写。
🗺 功能
- 支持截至2018年1月的Google Directions API的所有功能(完整的列表请参见此处:https://developers.google.com/maps/documentation/directions)
- 支持在Google Maps应用中“打开”,适用于特定位置和方向请求
- 还支持回调功能,当用户在Google Maps中完成操作时,将用户返回您的应用
- 如果Google Maps应用未安装,也支持回滚到内置的Apple Maps应用
- 可用现代Swift风格的完成块或在Objective-C风格的代理模式
- 查询通过HTTPS进行
- 在幕后使用JSON来帮助减小响应的大小
- 通过CocoaPods和Carthage提供
🆕 版本1.6中的新功能
- 与Google Places IDs的兼容性(用法:
PXLocation.googlePlaceId("gplaceid")
或PXLocation.googlePlaceId(gmsPlace.placeID)
如果您已经在使用Google的Places SDK for iOS) - 与Swift 4.2的兼容性
- 更新到Google Maps iOS SDK 2.7
- 通过Swift包管理器提供
🆕 版本1.5.1中的新功能
- 更新到Google Maps iOS SDK 2.5
- PXGoogleDirections Pod现在作为静态库发布(需要Cocoapods 1.4.0)
- 其他错误修复
🆕 版本1.4中的新功能
- 与Swift 4的兼容性
- 通过Carthage提供
- 对混合使用此Pod与Google Maps和/或Google Places Pods的项目进行的小幅改进(但Google Maps iOS SDK与其他Pods混合仍很糟糕...)
🆕 版本1.3中的新功能
- 完全支持Swift 3
- 完全支持Google Maps iOS SDK 2.0+
- 在
PXGoogleDirections
类中添加了一个trafficModel
属性以匹配API中的Google格式(最近添加);它仅在驾驶路线以及指定了出发日期时才有效 - 修复了绘制路线时只会绘制从路线对象中获取的基本、粗略表示的bug;现在在
PXGoogleDirectionsRoute
类的drawOnMap
方法中有一个绘制详细路线的选项 - 其他小错误修复
⚠️ 需求
- 适用于 iOS 9.3 及更高版本。
- 与 Swift 4 / Xcode 9 及更高版本兼容。
- 如果使用 Swift 3 和/或 Xcode 8,请使用 v1.3。
- 如果需要与更早版本的 Swift 兼容,请使用 v1.2.3。
- 该 SDK 依赖于官方的 Google Maps iOS SDK(更多信息请参阅:[Google Maps iOS SDK](https://developers.google.com/maps/documentation/ios/))
💻 安装
从 Carthage
将以下行添加到您的 Cartfile
中以在项目中使用 PXGoogleDirections
github "Poulpix/PXGoogleDirections"
或者,如果您希望针对特定版本的库,只需将其追加到
Cartfile
的行尾,例如:github "Poulpix/PXGoogleDirections" ~> 1.5
。
然后从终端运行以下命令
carthage update
最后,回到 Xcode,将生成的框架拖到目标“一般”标签页的“嵌入式二进制文件”部分。框架应位于 Xcode 项目的 Carthage/Build/iOS
子文件夹中。
重要:Carthage 仅从本库的 1.4 版开始支持。此库的先前版本将无法工作。
从 Cocoapods
要将 PXGoogleDirections 添加到项目中,请将以下 Podfile
添加到项目中
source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '9.3'
use_frameworks!
pod 'PXGoogleDirections'
然后从终端运行以下命令
pod install
重要:如果您的项目需要 PXGoogleDirections 和 Google Maps 以及/或 Google Places iOS SDK,您将会遇到问题。请参阅下面的“与 Google Pods 的兼容性”部分,并在需要时联系我并描述您的问题,以便获得帮助!
从源代码
从原始源代码构建是首选方法,如果您想要避免 Google Maps iOS SDK 与库冲突的已知问题。然而,您将缺乏 Cocoapods 和 Carthage 框架提供的自动化和版本更新。
要从源代码构建,请按照以下简单步骤操作
- 克隆存储库
- 将整个
PXGoogleDirections
项目添加到您的自己的 Xcode 项目中 - 在两个项目之间添加依赖关系并构建
- 请务必将
PXGoogleDirections
项目的输出(PXGoogleDirections.framework
)添加到 Xcode 项目的“主目标”下的“嵌入式二进制文件”部分
⌨️ 使用方法
Swift 代码两行快速入门
- 像这样引用库
import PXGoogleDirections
- 创建 API 对象
let directionsAPI = PXGoogleDirections(apiKey: "<insert your Google API key here>",
from: PXLocation.coordinateLocation(CLLocationCoordinate2DMake(37.331690, -122.030762)),
to: PXLocation.specificLocation("Googleplex", "Mountain View", "United States"))
- 运行方向请求
directionsAPI.calculateDirections({ response in
switch response {
case let .error(_, error):
// Oops, something bad happened, see the error object for more information
break
case let .success(request, routes):
// Do your work with the routes object array here
break
}
})
重要:通常您不需要亲自调用
GMSServices.provideAPIKey()
:PXGoogleDirections 在初始化 SDK 时会自动调用它。
有关可用属性和响应数据的信息,请参阅下面的 "文档"。
📚 文档
SDK 在 Xcode 中提供了一个集成文档,并且支持完整的自动完成。
为了帮助您开始,此仓库的 "示例" 子文件夹中还有一个示例项目。
它设计用于演示 API 和 SDK 的主要功能。
😱 与 Google pods 兼容性
自 V1.3 版本起,PXGoogleDirections 使用 Google 为 iOS SDK 提供的最新分支,该分支现在已分解为更小、更模块化的框架。PXGoogleDirections 与其中三个框架有关
GoogleMapsCore
GoogleMapsBase
GoogleMaps
不需要 Google 地点 iOS SDK。
如果您的应用程序也需要 Google Maps iOS SDK(例如,用于在地图上绘制),您会因为与存档在 pod 中包含的 Google Maps iOS SDK 发生冲突而遇到麻烦。这是由于 Google 以静态框架而非动态框架的方式发布它的 pod。
以下是迄今为止已知的唯一解决方案
- 从您的 Podfile 中删除 PXGoogleDirections 并执行
pod update
。 - 将所有 Google 依赖项添加到您的 Podfile 中(例如:
pod GoogleMaps
,pod GooglePlaces
)并执行pod update
。 - 在文件夹根目录下打开一个 Terminal,并将 PXGoogleDirections 作为 git submodule 引用,如下所示
git submodule add https://github.com/poulpix/PXGoogleDirections.git Frameworks/External/PXGoogleDirections
这将下载所有 PXGoogleDirections 项目到您自己的项目的子文件夹中(例如 Frameworks/External/PXGoogleDirections
)。当然,您可以更改此路径。
重要:您还可以通过将
-b <branch>
开关添加到git submodule
命令来请求框架的特定版本,如下所示
git submodule add -b <branch> https://github.com/poulpix/PXGoogleDirections.git Frameworks/External/PXGoogleDirections
要找到合适的分支名称,请检查 Github 上所有可用的分支
-
更新您的 Podfile,以给出如何构建您的项目和 PXGoogleDirections 子模块的指示
source 'https://github.com/CocoaPods/Specs.git' workspace 'test.xcworkspace' # Your project's workspace project 'test.xcodeproj' # Your project's Xcode project project 'Frameworks/External/PXGoogleDirections/PXGoogleDirections.xcodeproj' # Update folder structure if needed target 'test' do project 'test.xcodeproj' platform :ios, '10.0' # Update for your needs use_frameworks! # Update these lines depending on which Google pods you need pod 'GoogleMaps' pod 'GooglePlaces' # Other pods... end # This tells Cocoapods how to build the subproject target 'PXGoogleDirections' do project 'Frameworks/External/PXGoogleDirections/PXGoogleDirections.xcodeproj' platform :ios, '9.3' pod 'GoogleMaps' end
-
现在您需要在两个位置执行
pod install
- 您的项目根目录,
- PXGoogleDirections 子模块的根目录(例如
Frameworks/External/PXGoogleDirections
)。
- 使用您的项目.xcworkspace 打开 Xcode,然后构建 PXGoogleDirections 目标,然后构建您的应用程序目标。一切应该都会正常构建。
💣 已知问题
根据您的配置,您可能会看到以下一个或多个已知问题
- 运行时出现大量此类消息(通常在应用程序启动时):
两个位置都实现了 GMSxxx_whatever 类(您的应用程序名称)和(PXGoogleDirections 框架的引用)。两个中的哪一个将被使用是不确定的。
这是因为在 Carthage 或 Cocoapods 的情况下,通常您有两个版本的 Google Maps iOS SDK:一个与 PXGoogleDirections 库链接,另一个您将被迫在自己的应用程序中链接,以便显式使用它。据我所知,只要两个版本都是等效的,这些警告就不会造成真正的影响。它们只会污染运行时的输出控制台。 - 运行时出现类似的消息(通常在显示 Google Maps 视图时):
主线程检查器:在后台线程中调用 UI API:-[UIApplication setNetworkActivityIndicatorVisible:]
这种行为是 Xcode 9 新增的,似乎是 Google Maps iOS SDK 本身的问题,而不是附带的示例应用程序。这些消息不仅没有危害,而且也不合理。如果您找到解决方案,请私信我!
🙏🏻 信用
- 部分代码灵感来自 Google Maps 团队的 OpenInGoogleMaps-iOS
- 感谢 @embasssem 提供了使用 Google SDKs 的解决方案(#28)。
📜 许可证
PXGoogleDirections SDK 采用新 BSD 许可证授权。(更多信息请参阅 LICENSE 文件。)
📮 联系方式
欢迎您在 Github、Twitter、Stack Overflow 或通过电子邮件与我联系
- https://github.com/poulpix
- https://twitter.com/_RomainL
- http://stackoverflow.com/users/145997/romain
- dev (点) romain (在) me.com