ios - MapKit:如何在注释之一移动时更新地图上的折线
问题描述
我正在开发像 Uber 这样的应用程序。我成功地在地图上绘制了用户和司机之间的线,但我还想跟踪司机的动作,如何根据司机的动作更新折线。
注意:我有一个 API,可以获取驱动程序的当前位置。
let sourcePlaceMark = MKPlacemark(coordinate: sourceLocation, addressDictionary: nil)
let destinationPlaceMark = MKPlacemark(coordinate: destinationLocation, addressDictionary: nil)
let sourceMapItem = MKMapItem(placemark: sourcePlaceMark)
let destinationItem = MKMapItem(placemark: destinationPlaceMark)
let sourceAnotation = MKPointAnnotation()
sourceAnotation.title = sourceAddress
if let location = sourcePlaceMark.location {
sourceAnotation.coordinate = location.coordinate
}
let destinationAnotation = MKPointAnnotation()
destinationAnotation.title = destinationAddress
if let location = destinationPlaceMark.location {
destinationAnotation.coordinate = location.coordinate
}
mapView.showAnnotations([sourceAnotation, destinationAnotation], animated: true)
let directionRequest = MKDirections.Request()
directionRequest.source = sourceMapItem
directionRequest.destination = destinationItem
directionRequest.transportType = .automobile
let direction = MKDirections(request: directionRequest)
direction.calculate { (response, error) in
guard let response = response else {
if let error = error {
print("ERROR FOUND : \(error.localizedDescription)")
}
return
}
let route = response.routes[0]
mapView.addOverlay(route.polyline, level: MKOverlayLevel.aboveRoads)
解决方案
您已经完成了最重要的部分工作,获取驾驶员的位置并绘制路线。
现在,您可以简单地运行一个计时器并定期执行以下步骤:
- 获取司机位置
- 检查新位置是否与以前的位置不同(超过某个阈值)
- 使用更新的位置计算新路线
- 删除旧路线折线覆盖
- 添加新的路线折线叠加
推荐阅读
- javascript - 如何通过单击按钮从本地存储中删除元素?
- css - Angular Datatable - 覆盖按钮 CSS
- ios - Apache2 服务器视频流与移动设备和 PC 的兼容性
- javascript - 在传单上覆盖 div 并停止鼠标动作传播
- javascript - JavaScript。forEach 返回一个未定义的
- autodesk-forge - Autodesk forge:主页和信息图标消失了
- java - 如何从 Java 调用 Perl 6?
- css - 不遵守 Outlook 2013 表格边框
- javascript - axios获取请求中的意外令牌
- r - 将列转换为行重复常见值