首页 > 解决方案 > 我在我的项目中使用 Mapbox,想在道路的任一边缘创建折线。如何获取道路边缘的坐标

问题描述

我想跟踪用户步行坐标,所以我能够实现在道路边缘绘制折线,但坐标不是基于在边缘绘制的路线。

有两个航点作为起点和终点。如何获得在边缘绘制的折线的坐标。

// 道路边缘的坐标

     let delhiCoordinate = CLLocationCoordinate2D(latitude: 28.70442616237679, longitude: 77.10282448346703)

let currentCoordinate = CLLocationCoordinate2D(latitude: 28.704500269004427, longitude: 77.10271828467046)

// 在底部添加按钮

  func addButton(){
    
    navigationBtn = UIButton(frame: CGRect(x: (self.view.frame.width/2) - 100, y: self.view.frame.height - 80, width: 200, height: 50))
    navigationBtn.backgroundColor = .white
    navigationBtn.setTitle("Navigate", for: .normal)
    navigationBtn.setTitleColor(UIColor.red, for: .normal)
    navigationBtn.addTarget(self, action: #selector(navigateButtonPressed(_:)), for: .touchUpInside)
    view.addSubview(navigationBtn)
}

@objc func navigateButtonPressed(_ sender: UIButton){
            let annotation = MGLPointAnnotation()
    // 28.704499582120988,77.10271822945401
    
            annotation.coordinate = CLLocationCoordinate2D(latitude:28.704500269004427, longitude: 77.10271828467046)
            annotation.title = "Source"

            mapView.addAnnotation(annotation)
    calculateRoute(from: currentCoordinate, to: delhiCoordinate) { (route, error) in
        if error != nil{
            print("error")
        }
        print("route...\(route)")
    }
}

// 计算路线并绘制路线

     func calculateRoute(from originCoor: CLLocationCoordinate2D, to     destinationCoor: CLLocationCoordinate2D, completion: @escaping(Route?, Error?) -> Void){
    print("origin...\(originCoor)")
    let origin = Waypoint(coordinate: originCoor, coordinateAccuracy: -1, name: "start")
    let destination = Waypoint(coordinate: destinationCoor, coordinateAccuracy: -1, name: "finish")

    
    print("origin...\(origin)....desti....\(destination)")
    
    let options = NavigationRouteOptions(waypoints: [origin,destination], profileIdentifier: .automobileAvoidingTraffic)
    print("options...\(options)")
    _ = Directions.shared.calculate(options, completionHandler: { (waypoints,routes,error) in
        print("routess...\(routes?.first)")
        print("routess...\(waypoints)")
        self.directionRoute = routes?.first
        // draw line
        //print("self.directionRoute...\(self.directionRoute)")
        self.drawRoute(route: self.directionRoute!)
        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}
    print("route....\(route.coordinates)")
    var 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)
        print("source...\(polyLine.coordinate)")
     
        // this line is used to draw line at the edge of the road
        linestyle.lineOffset =  NSExpression(forConstantValue: -10)
      
       linestyle.lineWidth = NSExpression(forConstantValue: 1.0)
        mapView.style?.addSource(source)
        mapView.style?.addLayer(linestyle)
   
        
                    
    }
}

标签: swiftmapboxmapbox-ios

解决方案


推荐阅读