Pathfinder iOS 客户端库允许开发者轻松地将 Pathfinder 路由服务集成到他们的 iOS 应用程序中。
Pathfinder 提供作为一项服务,消除开发者自己实现路由物流的需求。此 SDK 允许 iOS 应用程序作为需要运输的商品或运输提供商。此外,还支持查看商品集和运输提供商的路线。
要注册使用 Pathfinder 的应用程序,请访问我们的网站:https://thepathfinder.xyz。
要了解如何在 iOS 应用程序中集成 Pathfinder,请查看 API 文档:http://cocoadocs.org/docsets/thepathfinder 或教程:https://pathfinder.readme.io。
应用程序与 Pathfinder 服务交互有三种主要方式;作为观察者、运输提供商或商品运输请求者。在每种情况下,您都需要提供一个应用程序标识符和一组用户凭据。可以从注册应用程序时的 Pathfinder 网页上获得应用程序标识符。用户凭据标识您应用程序的最终用户。
所有 Pathfinder 路线都在“集群”内计算。集群只是一个逻辑上区分您的数据容器。例如,您可能希望从您的应用程序中提供两个独立路由的服务。您的用户凭证必须有权访问您尝试连接的集群。
let myAppId: String = 'application id from pathfinder web port'
let userCreds = getGoogleIdToken()
let pathfinderRef = Pathfinder(applicationIdentifier: myAppId, userCredentials: userCreds)
如果您的应用程序想要显示有关集群中所有(或某些子集)的商品、运输提供商及其路线的数据,您需要做以下操作
从 pathfinder 对象中获取集群引用。路径指定了在分层子集群树中的集群。如果没有传入路径,将创建对应用程序顶级默认集群的引用。
let cluster = pathfinderRef.cluster(path: "/USA/East/Boston")
通过 ClusterDelegate 协议将 ViewController 设置为集群的代理。
cluster.delegate = self
将您的集群连接到 Pathfinder。
cluster.connect()
实现 ClusterDelegate
协议。
extension ViewController: ClusterDelegate {
func connected(cluster: Cluster) {
cluster.transports().foreach { (t: Transport) -> Void in draw(t) }
cluster.commodities().foreach { (c: Commodity) -> Void in draw(c) }
}
func clusterWasRouted(routes: [Route]) {
clearOldRoutes()
routes.foreach { (r: Route) -> Void in draw(r) }
}
...
}
当出现新的商品或运输时,当运输移动时,当商品被选中、放下或取消时,以及当生成路线时,您的 ClusterDelegate 都会收到通知。
如果您的应用程序在逻辑上表示一个运输提供商,您需要做以下几步:
确定您的运输工具的参数。实体车辆可能会受到许多因素的影响:座位数、儿童座椅、自行车架、可同时运输的货币限额、某些没有山羊或狼骑行的卷心菜。无论限制因素是什么,创建一个表示这些限制的字符串到整数的字典。
parameters = [String:Int]()
parameters['people'] = 4
parameters['goats'] = 1
parameters['pizzas'] = 25
为您的应用程序获取一个运输工具引用。
let transport = pathfinder.cluster().createTransport(parameters)
将ViewController设置为运输工具的代理,通过TransportDelegate协议。
transport.delegate = self
将您的运输工具连接到Pathfinder。
transport.connect()
实现TransportDelegate
协议。
extension ViewController: TransportDelegate {
func wasRouted(transport: Transport, route: Route) {
drawRouteOnMyMap(route.coordinates())
drawCommoditiesOnMyMap(route.commodities())
}
}
每当运输工具被分配到新的路线、位置更新或离线时,您的TransportDelegate都会被通知。
如果您的应用程序请求商品,您需要做以下几步:
确定您的商品所占据的物理参数。这些参数应与同一集群中运输工具的限制相匹配。任何未设置的参数将被假定为零。
parameters = [String:Int]()
parameters['people'] = 2
发起请求,并从顶级Pathfinder对象获取商品引用。
let commodity = pathfinder.cluster().createCommodity(
start: startCoordinates, destination: endCoordinates, parameters: parameters)
通过CommodityDelegate协议将ViewController设置为商品的代理。
commodity.delegate = self
实现CommodityDelegate
协议。
extension ViewController: CommodityDelegate {
func connected(commodity: Commodity) {
draw(commodity)
}
func wasRouted(commodity: Commodity, byTransport: Transport, onRoute: Route) {
byTransport.delegate = self
byTransport.connect()
draw(onRoute)
}
...
}
每当商品被分配到路线或其取货/交货/取消状态更改时,您的CommodityDelegate都会被通知。
Pathfinder是一个开源项目,欢迎社区贡献。
以以下方式克隆存储库:
git clone https://github.com/csse497/pathfinder-ios
可以通过Xcode 7或以下命令构建框架:
xcodebuild -workspace framework/Pathfinder.xcworkspace -scheme Pathfinder -sdk iphonesimulator -configuration RELEASE
MIT.