swift - 我在我的项目中使用 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)
}
}
解决方案
推荐阅读
- angular - 如何从常规打字稿类(非组件)调用角度 2+ 服务
- reactjs - 如何摆脱“必须使用重组道具”作业
- javascript - 为什么 JS 增量速记不起作用
- ruby-on-rails - PaperTrail 更新模型时不更新元字段
- git - 如果在拉/推时我没有看到任何冲突,Git 如何覆盖过去提交的更改?
- excel - 尝试通过 WinHttp.WinHttpRequest.5.1 下载不受信任的证书文件时使用站点变量
- python - 条件互信息
- ionic-framework - Angular http客户端无法在ios设备Ionic3上运行
- python - 谁能帮我阅读这个 XML 文件并告诉我我在看什么
- javascript - 修改正则表达式模式以将嵌套标签捕获到对象数组中