gpt4 book ai didi

swift - 创建指向指定位置方向的自定义罗盘箭头

转载 作者:行者123 更新时间:2023-11-28 06:02:40 25 4
gpt4 key购买 nike

基本上我想要一个旋转的箭头图像,以便将用户引导到附近的位置。我正在使用核心位置航向数据来获取方向信息,相关代码如下。

 func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
print("MAGNETIC HEADING: \(newHeading.magneticHeading)")
print("TRUE HEADING \(newHeading.trueHeading)")

var degrees = newHeading.trueHeading
//Rotate the arrow image
if self.arrowImageView != nil {

self.arrowImageView.transform = CGAffineTransform(rotationAngle: CGFloat(degrees * (180.0 / M_PI)))
}


}

我不确定这是否是正确的方法。

最佳答案

我设法使用此处找到的 C# 解决方案解决了这个问题 How to rotate a Direction Arrow to particular location

func UpdateCompass(origin: CLLocationCoordinate2D,target:CLLocationCoordinate2D, heading: CLHeading)
{
var angle1 = GetAngleBetweenPoints(origin: origin, target: target);
var angle2 = GetAngleFromHeading(heading: heading);
var radian = .pi * (angle1 + angle2) / 180.0;
let res = radiansToDegrees(radians:radian)
self.arrowImageView.transform = CGAffineTransform(rotationAngle: CGFloat(radian))
//self.CompassImageView.transform = CGAffineTransform(rotationAngle: CGFloat(radian))
UIView.animate(withDuration: 0.5, animations: {
self.CompassImageView.transform = CGAffineTransform(rotationAngle: CGFloat(radian))
})
}

func GetAngleBetweenPoints(origin:CLLocationCoordinate2D,target: CLLocationCoordinate2D) -> Double
{
var n = 270 - (atan2(origin.latitude - target.latitude, origin.longitude - target.longitude)) * 180 / .pi;
return n.truncatingRemainder(dividingBy: 360);
}

func GetAngleFromHeading(heading:CLHeading) -> Double
{
var radians = -heading.magneticHeading / 180.0 * .pi;
return radians * (180.0 / .pi);
}

关于swift - 创建指向指定位置方向的自定义罗盘箭头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49194959/

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