gpt4 book ai didi

ios - 在我的 mapView Controller 中使用 iOS 中的 Mapbox 导航

转载 作者:搜寻专家 更新时间:2023-10-30 22:16:49 25 4
gpt4 key购买 nike

我想在 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 中显示导航,它是另一个 View Controller 的一部分,我可以通过获取方向/路线和指令来实现,但我找不到任何每次调用的方法其次,如果用户更改路径,我可以更新路线指令和路线。

如果我遗漏了任何内容或需要进行任何更改,请告诉我。

-提前致谢

最佳答案

这是我的方法:

  • 首先,我确实只从 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,因此只要它具有纬度和经度,请随时将其替换为您自己的 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 Delegate 的回调函数通知您位置更新,而不是使用计时器并每秒调用一次,这是更有效的方法

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
/* do your business here */
}
  • 不要忘记将位置管理器委托(delegate)给自己或您选择的类

关于ios - 在我的 mapView Controller 中使用 iOS 中的 Mapbox 导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57309240/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com