我想在iOS中集成mapbox导航,我可以轻松地在两个坐标之间获得方向/路由,也可以从mapbox中获取导航路径,我们可以使用以下代码
let options = NavigationOptions(styles: nil)
let viewController = NavigationViewController(for: self.directionsRoute!)
viewController.delegate=self
self.present(viewController, animated: true, completion: nil)
,但问题是我想在我的MapView中显示导航,这是另一个视图控制器的一部分,我可以通过获得方向/路线和指令来做到这一点,但是我找不到任何方法第二,以便在用户更改路径时,我可以更新路由指令和路由。
让我知道我是否缺少任何需要或任何更改。
- 预先感谢
这是我的方法:
-
首先,我确实从Mapbox API中获得了指令,利用了免费的API调用配额,并利用其良好的性能和内存管理来绘制GMSMAPVIEW或MAPKIT上的说明。
-
podfile
pod 'MapboxDirections.swift'
import MapboxDirections
这是通过以下代码完成的
- 拥有Mapbox方向的属性
@IBOutlet weak var googleMapView: GMSMapView!
let locationManager = CLLocationManager()
let mapBoxirections = Directions(accessToken: osmToken)
var path: GMSMutablePath?
- 然后进行实际的API调用
private func drawRouteBetween(source: StopModel, destination: StopModel) {
guard let name = source.name, let lat = source.latitude, let lng = source.longitude else { return }
guard let nameDest = destination.name, let latDest = destination.latitude, let lngDest = destination.longitude else { return }
let waypoints = [
Waypoint(coordinate: CLLocationCoordinate2D(latitude: lat, longitude: lng), name: name),
Waypoint(coordinate: CLLocationCoordinate2D(latitude: latDest, longitude: lngDest), name: nameDest),
]
let options = RouteOptions(waypoints: waypoints, profileIdentifier: .automobile)
options.includesSteps = true
options.distanceMeasurementSystem = .metric
mapBoxirections.calculate(options) { (waypoints, routes, error) in
guard error == nil else {
print("Error calculating directions: (error!)")
return
}
if let route = routes?.first, let leg = route.legs.first {
for step in leg.steps {
if let coordinates = step.coordinates {
for (index, point) in coordinates.enumerated() {
let source = point
if index <= coordinates.count - 2 {
let destination = coordinates[index + 1]
self.drawPolyLine(source: source, destination: destination)
}
}
}
}
}
}
}
请注意,StopModel是我的自定义群集,因此只要它具有纬度和经度,请随时用自己的自定义替换
创建在下面的cllocationmanagerdelegate上绘制polyline的方法,如下
private func drawPolyLine(source: CLLocationCoordinate2D, destination: CLLocationCoordinate2D){
path?.add(source)
path?.add(destination)
let polyLine = GMSPolyline(path: path)
polyLine.strokeWidth = 4 // width of your choice
polyLine.strokeColor = .red // color of your choice
polyLine.map = googleMapView
}
- 然后查看MapBoxDirections.Route模型并探索它的属性,您将在IT中找到非常有用的信息
,然后利用GMS委托的回调函数,该委员会通知您位置更新,而是拥有一个计时器并在每一秒
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
/* do your business here */
}
- 不要忘记将位置经理的代表与您选择的班级或您选择的班级
也许这有点有帮助:您可以轻松添加观察者以进行路线进度更改:
NotificationCenter.default.addObserver(self,
selector: #selector(progressDidChange(notification:)),
name: .routeControllerProgressDidChange,
object: navigationService.router)
您需要通过创建
来通过路线进行导航服务let navigationService = MapboxNavigationService(route: route)
功能progressDidChange
可以做类似:
@objc func progressDidChange(notification: NSNotification) {
guard let routeProgress = notification.userInfo?[RouteControllerNotificationUserInfoKey.routeProgressKey] as? RouteProgress,
let location = notification.userInfo?[RouteControllerNotificationUserInfoKey.locationKey] as? CLLocation else {
return
}
// you have all information you probably need in routeProgress, f.E.
let secondsRemaining = routeProgress.currentLegProgress.currentStepProgress.durationRemaining
...
}