PXGoogleDirections

适用于 iOS 的 Google Directions API SDK,完全使用 Swift 编写。

Cocoapods Cocoapods Swift

Cocoapods compatible Cocoapods CocoaPods CocoaPods CocoaPods CocoaPods

Carthage compatible Swift Package Manager incompabible

GitHub stars GitHub forks GitHub watchers Twitter Follow

🗺 功能

🆕 V1.6 中的新功能

🆕 V1.5.1 中的新功能

🆕 V1.4 中的新功能

🆕 V1.3 中的新功能

⚠️要求

💻 安装

从 Carthage

要在项目中使用 PXGoogleDirections,请将以下行添加到 Cartfile

github "Poulpix/PXGoogleDirections"

或者,如果您希望以特定版本的库为目标,只需将其附加到 Carttfile 中行的末尾,例如:github "Poulpix/PXGoogleDirections" ~> 1.5


然后从终端运行以下命令

carthage update

最后,返回 Xcode,将生成的框架拖放到目标“General”选项卡的“Embedded Binaries”部分。 该框架应位于 Xcode 项目的 Carthage/Build/iOS 子文件夹中。

Dropping a Carthage-generated framework in Xcode


重要提示: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 框架提供的自动化和版本更新。

要从源代码构建,请按照以下简单步骤操作

Adding the PXGoogleDirections framework as an embedded binary in Xcode

⌨️ 用法

在两行 Swift 代码中快速入门

  1. 像这样引用该库
import PXGoogleDirections
  1. 创建一个 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"))
  1. 运行 Directions 请求
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 的主要功能。

Sample app screenshot 1 Sample app screenshot 2

😱 与 Google pods 兼容性

自 V1.3 以来,PXGoogleDirections 使用 Google 的最新 Google Maps iOS SDK 分支,该分支现已拆分为更小、更模块化的框架。 PXGoogleDirections 依赖于其中的三个

不需要 Google Places iOS SDK。

如果您的应用还需要 Google Maps iOS SDK(例如,用于在地图上绘图),由于与 pod 中捆绑的 Google Maps iOS SDK 冲突,您将会遇到麻烦。 这是因为 Google 将其 pod 作为静态框架而不是动态框架发布的方式所致。

这是迄今为止已知的唯一解决方法

  1. 从您的 Podfile 中删除 PXGoogleDirections 并执行 pod update
  2. 将所有 Google 依赖项添加到您的 Podfile(例如:pod GoogleMapspod GooglePlaces)并执行 pod update
  3. 在您的文件夹的根文件夹中打开一个终端,并将 PXGoogleDirections 引用为 git 子模块,如下所示
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 上的所有可用分支


  1. 更新您的 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
    
  2. 现在您需要在两个位置执行 pod install

  1. 使用您的 project.xcworkspace 打开 Xcode 并构建 PXGoogleDirections 目标,然后构建您的应用程序目标。 一切都应该正确构建。

💣 已知问题

根据您的设置,您可能会看到以下一个或多个已知问题

🙏🏻 鸣谢

📜 许可

PXGoogleDirections SDK 在 New BSD 许可下获得许可。(更多信息请参见 LICENSE。)

📮 联系方式

请随时在 Github、Twitter、Stack Overflow 上或通过电子邮件给我留言