iOS MapKit自定义用户位置标题错误



我想创建一个自定义的"用户位置";引脚,根据用户位置旋转标题。

我使用这个答案:https://stackoverflow.com/a/58363556/894671作为基础,并设法建立和运行一个自定义引脚,它根据标题旋转。

问题:

在设备上测试时,似乎使用提供的标题进行转换是不正确的。只有在0/360度,它显示正确,但如果我旋转,我看到默认MKMapKit显示标题正确旋转,而我的自定义图标管理旋转两次在同一时间。

请见附件视频:https://i.imgur.com/3PEm2MS.mp4

Demo上传到这里:https://github.com/GuntisTreulands/Demo123

但是对于所有的意图和目的,这里是AnnotationView:


class AnnotationView : MKAnnotationView, HeadingDelegate {
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
}
override init(annotation: MKAnnotation?, reuseIdentifier: String?) {
super.init(annotation: annotation, reuseIdentifier: reuseIdentifier)
}
func headingChanged(_ heading: CLLocationDirection) {
// For simplicity the affine transform is done on the view itself
UIView.animate(withDuration: 0.1, animations: { [unowned self] in
self.transform = CGAffineTransform(rotationAngle: CGFloat(heading * .pi / 180.0 ))
})
}
}

和标题从这里转发:

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
{
if let lastLocation = locations.last {
userLocationAnnotation.coordinate = lastLocation.coordinate
}
}

我不明白,为什么我的位置标题表现得这么奇怪。

好的,我设法解决了这个问题。

下面是新的结果:https://i.stack.imgur.com/NHSvY.jpg

我所做的是:

func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
if let heading = mapView.userLocation.heading {
userLocationAnnotation.heading = -mapView.camera.heading + (heading.trueHeading > 0 ? heading.trueHeading : heading.magneticHeading)
} else {
userLocationAnnotation.heading = -mapView.camera.heading + (newHeading.trueHeading > 0 ? newHeading.trueHeading : newHeading.magneticHeading)
}
}

所以-如果我使用userlocation与trackingmode == . followwithheading,这是伟大的工作。我的图标在原来图标的上面。

要隐藏原始用户图标并显示您的图标-为userlocation返回一个自定义注释视图(没有任何渲染):

func mapView(_ mapView: MKMapView, viewFor annotation: MKAnnotation) -> MKAnnotationView? {
guard !(annotation is MKUserLocation) else {
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: "AnnotationView")
if annotationView == nil {
annotationView = MKAnnotationView(annotation: annotation, reuseIdentifier: "AnnotationView")
}
return annotationView
}
if let annotation = annotation as? Annotation {
var annotationView = mapView.dequeueReusableAnnotationView(withIdentifier: NSStringFromClass(Annotation.self))
if (annotationView == nil) {
annotationView = AnnotationView(annotation: annotation as MKAnnotation, reuseIdentifier: NSStringFromClass(Annotation.self))
} else {
annotationView!.annotation = annotation as MKAnnotation
}
annotation.headingDelegate = annotationView as? HeadingDelegate
annotationView!.image = UIImage.init(named: "user_pin")
return annotationView
}

return nil
}

因此结果是:

1。)获得原始的苹果地图用户位置+ followWithHeading,但与自定义pin和正确的位置。

2)我可以调整我的自定义位置pin位置到一些不同的坐标,但请记住,followWithHeading将围绕真正的userLocation坐标旋转。

3。)我还注意到,没有userlocation,原始计算:

userLocationAnnotation.heading = -mapView.camera.heading + (newHeading.trueHeading > 0 ? newHeading.trueHeading : newHeading.magneticHeading)

Is also…还行,如果我移动和旋转。它是有缺陷的,如果我原地旋转。还是解决不了那个

我还设法用这个函数创建了一个自定义的followWithHeading:

func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
mapView.centerCoordinate = userLocationAnnotation.coordinate

mapView.camera.heading = (newHeading.trueHeading > 0 ? newHeading.trueHeading : newHeading.magneticHeading)
userLocationAnnotation.heading = -mapView.camera.heading + (newHeading.trueHeading > 0 ? newHeading.trueHeading : newHeading.magneticHeading)
}

但结果不像原来的苹果地图旋转那样流畅。如果旋转得快,就没问题。但是如果我慢慢地转动我的设备,那么旋转就明显不流畅了。

查看结果:https://i.stack.imgur.com/RtkeG.jpg

最新更新