Mapbox路由更多2条注释

时间:2019-07-04 12:45:14

标签: ios routes mapbox direction

从API中,我得到四个annotations。在代码中,我输入带有位置的静态array

我需要多个annotations之间的路由,如下图所示:

as it should be

我阅读了下一篇教程:

Annotation models

Add multiple shapes from a single shape source

Animate a line

我的代码:

    class ViewController: UIViewController {

    var mapView: MGLMapView!

    let directions = Directions(accessToken: "my token")

    var coordinates: [CLLocationCoordinate2D] = [
                                                CLLocationCoordinate2D(latitude: 42.97876170515205, longitude: 47.5146782332093),
                                                CLLocationCoordinate2D(latitude: 42.787338526873825, longitude: 47.55432128244579),
                                                CLLocationCoordinate2D(latitude: 42.97164297215167, longitude: 47.50778495604164),
                                                CLLocationCoordinate2D(latitude: 42.14711445441536, longitude: 47.87841796208566)]

    var route: Route?

    override func viewDidLoad() {
        super.viewDidLoad()

        mapView = MGLMapView(frame: view.bounds, styleURL: MGLStyle.lightStyleURL)
        mapView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
        mapView.tintColor = .darkGray

        setMapView()
    }

    fileprivate func setMapView() {
        mapView.setUserTrackingMode(.follow, animated: true)
        mapView.styleURL = MGLStyle.lightStyleURL

        mapView.setCenter(CLLocationCoordinate2D(latitude: 42.983060, longitude: 47.504682), zoomLevel: 12, animated: false)
        view.addSubview(mapView)
        mapView.delegate = self

        for (index, coordinate) in coordinates.enumerated() {
            let marker = MGLPointAnnotation()
            marker.coordinate = coordinate
            mapView.addAnnotation(marker)

            //draw line
            if (index + 1) < coordinates.count {
                calculateRoute(from: coordinate, to: coordinates[index + 1]) { (route, error) in
                    if error != nil {
                        print("error getting route")
                    }
                }
            }
        }
    }
}

//画线

extension ViewController {
    func calculateRoute(from originCoor: CLLocationCoordinate2D, to destinationCoor: CLLocationCoordinate2D, complition: @escaping (Route?, Error?) -> Void) {

        let origin = Waypoint(coordinate: originCoor, name: "Mapbox")
        let destination = Waypoint(coordinate: destinationCoor, name: "White House")
        let options = NavigationRouteOptions(waypoints: [origin, destination], profileIdentifier: .automobileAvoidingTraffic)

        _ =  directions.calculate(options) { (waypoints, routes, error) in
            guard let route = routes?.first else {print(error.debugDescription); return}
            self.route = route
            self.drawRoute(route: self.route!)

            let coordinateBounds = MGLCoordinateBounds(sw: destinationCoor, ne: originCoor)
            let insets = UIEdgeInsets(top: 50, left: 50, bottom: 50, right: 50)
            let routeCam = self.mapView.cameraThatFitsCoordinateBounds(coordinateBounds, edgePadding: insets)
            self.mapView.setCamera(routeCam, animated: true)
        }
    }

    func drawRoute(route: Route) {
        guard route.coordinateCount > 0 else {return}
        let routeCoordinates = route.coordinates!
        let polyline = MGLPolylineFeature(coordinates: routeCoordinates, count: route.coordinateCount)

        if let source = mapView.style?.source(withIdentifier: "route-source") as? MGLShapeSource {
            source.shape = polyline
        } else {
            let source = MGLShapeSource(identifier: "route-source", features: [polyline], options: nil)

            let lineStyle = MGLLineStyleLayer(identifier: "route-style", source: source)

            mapView.style?.addSource(source)
            mapView.style?.addLayer(lineStyle)
        }
    }
}

我的结果:

bad result

我看了Mapbox的所有教程,仅此而已。

我将不胜感激。谢谢!

0 个答案:

没有答案