首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MapKit:如何在移动其中一个注记时更新地图上的折线

MapKit:如何在移动其中一个注记时更新地图上的折线
EN

Stack Overflow用户
提问于 2021-02-12 04:24:06
回答 1查看 47关注 0票数 0

我正在开发一个像Uber这样的应用程序。我成功地在地图上绘制了用户和驾驶员之间的线,但我还希望跟踪驾驶员的移动,如何根据驾驶员的移动更新折线。

注意:我有一个API,可以获取驱动程序的当前位置。

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2021-02-12 06:23:41

你已经有了最重要的部分,获取司机的位置,并绘制一条通往它的路线。

现在,您只需运行一个timer并定期执行以下步骤:

  • get driver location
  • 检查新位置是否与先前的location
  • calculate不同(超出某个阈值)具有更新的location
  • remove旧路径折线叠加
  • 添加新路径折线叠加
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/66162260

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档