【问题标题】:Calling mapView(_:viewFor:) when user location changes当用户位置改变时调用 mapView(_:viewFor:)
【发布时间】:2019-06-20 20:43:31
【问题描述】:

我希望将用户位置显示为自定义图像(而不是蓝色信标)。我还希望每当用户位置更改其坐标时,此自定义图像都会相应地旋转以表示用户路线。为了实现这一点,我实现了下面的代码。当应用程序启动时,它使用注释类型 userLocation 调用,以便用户位置自定义图像显示正常。问题是,当我模拟用户位置坐标的变化时,委托 mapView(_:viewFor:) 方法永远不会使用注释类型 userLocation 再次调用,因此图像永远不会旋转。 (注意:UIImage rotateImage(by:) 是自定义方法)。

是否有一些方法可以调用 mapView(_:viewFor:) 以便在用户位置更改时更新用户位置的图像注释?感谢您的帮助。

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
    if annotation.isEqual(mapView.userLocation) {
        // Annotation image for user location
        let annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: nil)
        annotationView.image = UIImage(named: "UserLocationImage.png")?.rotateImage(by: currentCourse)
        return annotationView  
    }
}

【问题讨论】:

    标签: swift annotations mapkit core-location


    【解决方案1】:

    使用 CLLocationManagerDelegate https://developer.apple.com/documentation/corelocation/cllocationmanagerdelegate 在您的代码中实现此协议,并将您的 mapView 函数放入它的 locationManager(_:didUpdateLocations:) 方法中。

    【讨论】:

    • 我在我的代码中实现了方法 locationManager(:didUpdateLocations:)。我只是看不到如何调用 mapView(:viewFor:) 来更新用户位置注释。无论如何感谢您的回答。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多