7

我想在 iOS 中集成 Mapbox 导航,我可以轻松获取两个坐标之间的方向/路线,也可以从 mapbox 获取导航路径,我们可以使用下面的代码

let options = NavigationOptions(styles: nil)
let viewController = NavigationViewController(for: self.directionsRoute!)
viewController.delegate=self    
self.present(viewController, animated: true, completion: nil)

但问题是我想在我的地图视图中显示导航,这是另一个视图控制器的一部分,我可以通过获取方向/路线和指令来做到这一点,但我找不到任何每秒都会调用的方法,这样如果用户更改路径,我可以更新路线指令以及路线。

让我知道我是否遗漏了任何内容或需要进行任何更改。

-提前致谢

4

2 回答 2

1

这是我的方法:

  • 首先,我确实从 MapBox api 获得了方向指令,利用它的免费 API 调用配额,并利用它们的良好性能和内存管理在 GMSMapView 或 MapKit 上绘制指令。

  • 播客文件

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 是我定制的 CLLocation,所以只要它有纬度和经度,就可以随意替换它

  • 创建在您的 CLLocationManagerDelegate 上绘制折线的方法,如下所示

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 模型并探索它的属性,您会在其中发现非常有用的信息

然后利用 GMS 代表的回调函数通知您位置更新,而不是有一个计时器并每秒调用一次,这是更有效的方法

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        /* do your business here */
    }
  • 不要忘记让位置经理代表自己或您选择的班级
于 2019-08-07T09:41:37.953 回答
0

也许这会有所帮助:您可以轻松地为路线进度更改添加观察者:

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

    ...
}
于 2019-08-05T12:16:31.580 回答