适用于 iOS 的 Google Directions API SDK,完全使用 Swift 编写。
PXLocation.googlePlaceId("gplaceid")
,或者如果已经使用了 Google 的 Places SDK for iOS,则使用 PXLocation.googlePlaceId(gmsPlace.placeID)
)PXGoogleDirections
类中添加了 trafficModel
属性,以匹配 API 中的 Google 属性(最近添加);它仅适用于驾驶路线,并且在指定出发日期时才有效PXGoogleDirectionsRoute
类的 drawOnMap
方法中绘制详细路线要在项目中使用 PXGoogleDirections,请将以下行添加到 Cartfile
中
github "Poulpix/PXGoogleDirections"
或者,如果您希望以特定版本的库为目标,只需将其附加到
Carttfile
中行的末尾,例如:github "Poulpix/PXGoogleDirections" ~> 1.5
。
然后从终端运行以下命令
carthage update
最后,返回 Xcode,将生成的框架拖放到目标“General”选项卡的“Embedded Binaries”部分。 该框架应位于 Xcode 项目的 Carthage/Build/iOS
子文件夹中。
重要提示:Carthage 仅从该库的 1.4 版本开始支持。 此库的先前版本将不起作用。
要在项目中使用 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 项目主目标的“Embedded Binaries”部分在两行 Swift 代码中快速入门
import PXGoogleDirections
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 中提供了集成的文档,并提供完整的自动完成支持。
为了帮助您入门,在此存储库的“Sample”子文件夹中还提供了一个示例项目。
它旨在演示 API 和 SDK 的主要功能。
自 V1.3 以来,PXGoogleDirections 使用 Google 的最新 Google Maps iOS SDK 分支,该分支现已拆分为更小、更模块化的框架。 PXGoogleDirections 依赖于其中的三个
GoogleMapsCore
GoogleMapsBase
GoogleMaps
不需要 Google Places iOS SDK。
如果您的应用还需要 Google Maps iOS SDK(例如,用于在地图上绘图),由于与 pod 中捆绑的 Google Maps iOS SDK 冲突,您将会遇到麻烦。 这是因为 Google 将其 pod 作为静态框架而不是动态框架发布的方式所致。
这是迄今为止已知的唯一解决方法
pod update
。pod GoogleMaps
、pod GooglePlaces
)并执行 pod update
。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
Frameworks/External/PXGoogleDirections
)。根据您的设置,您可能会看到以下一个或多个已知问题
运行时(通常是应用程序启动时)出现大量类似这样的消息:Class GMSxxx_whatever is implemented in both (name of your app) and (reference to PXGoogleDirections framework). One of the two will be used. Which one is undefined.
这是因为使用 Carthage 或 Cocoapods,您通常会拥有两个版本的 Google Maps iOS SDK:一个已与 PXGoogleDirections 库链接,另一个是如果您希望显式使用它,您将被迫在自己的应用程序中链接到的版本。 从我所看到的,只要两个版本等效,这些警告就不会产生实际影响。 它们只会污染运行时输出控制台。
运行时出现类似这样的消息(通常在显示 Google 地图视图时):Main Thread Checker: UI API called on a background thread: -[UIApplication setNetworkActivityIndicatorVisible:]
此行为是 Xcode 9 中新增的,罪魁祸首似乎是 Google Maps iOS SDK 本身,而不是库提供的示例应用程序。 这些消息并非真正有害,但也不正常。 如果您找到解决方案,请私信我!
该框架尚未为 Swift Package Manager 做好准备,因为它需要静态链接到 Google Maps iOS SDK 并包含 Google Maps 资源包; 目前,这两项任务都无法通过 Swift Package Manager 完成。
PXGoogleDirections SDK 在 New BSD 许可下获得许可。(更多信息请参见 LICENSE。)
请随时在 Github、Twitter、Stack Overflow 上或通过电子邮件给我留言