xcode iPhone 指南针显示特定位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7298193/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 21:52:22  来源:igfitidea点击:

iPhone compass showing to a specific location

iphoneobjective-cxcodecompass-geolocationdirection

提问by moonix82

I am new to objective-c so I want to apologize if my question is too rubbish.

我是objective-c的新手,所以如果我的问题太垃圾,我想道歉。

I am programming a compass and yet, it is working (thanks to a tutorial). Actual position (Longitude and Lattitude) is given. Is it possible that my arrow within the compass can show me the direction to a specific location (longitude2 and lattitude2)? I have read that I have to consider the magnetic and the true heading of the iphone.

我正在编写一个指南针,但它正在工作(感谢教程)。给出了实际位置(经度和纬度)。指南针内的箭头是否有可能向我显示特定位置(经度 2 和纬度 2)的方向?我读过我必须考虑iphone的磁性和真实航向。

回答by Flori

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
    double heading = newHeading.trueHeading; //in degree relative to true north
    double bearing = ... //get current bearing in degree relative to true north
                         //with the JS library mentioned below it would be something like
                         //bearing = userLoc.bearingTo(destionation);

    //just two lines, the the sake of the example
    double rotationInDegree = (bearing - heading);  //the important line
    rotationInDegree = fmod((rotationInDegree + 360), 360);  //just to be on the safe side :-)
    compassViewPerhapsAsImage.transform=CGAffineTransformMakeRotation(DegreesToRadians(rotationInDegree));
}

Good links to calculate the bearing:

计算轴承的好链接:

回答by msgambel

If you don't need it to be too accurate, then it is fairly easy. All you need to do is calculate the angle between the 2 points, and show the angle on the compass. (As you haven't posted code, I'm not sure if you have a method like setAngleForCompass, but you should!) Try this:

如果你不需要它太准确,那么它很容易。您需要做的就是计算两点之间的角度,并在指南针上显示角度。(由于您还没有发布代码,我不确定您是否有类似 的方法setAngleForCompass,但您应该这样做!)试试这个:

float dx = Longitude2 - Longitude1;
float dy = Latitude2 - Latitude1;
float angle = atan2(dy, dx);
// Set the angle of the compass here.

Alternatively, you could use the CMMotionManagerto get access to the gyroscope, which will also point you in the right direction. (pun intended!) Hope that Helps!

或者,您可以使用CMMotionManager访问陀螺仪,这也将为您指明正确的方向。(双关语!)希望有帮助!

回答by progrmr

I also needed to calculate heading to a location. The best method I found was to use the Spherical Law of Cosines. I created C functions to implement this and they are available Here on github. Sounds like you need the headingInDegrees function. This will give you a true heading.

我还需要计算前往某个位置的航向。我发现的最好方法是使用余弦球定律。我创建了 C 函数来实现这一点,它们在 github上可用。听起来您需要 HeadingInDegrees 函数。这会给你一个真正的标题。

回答by user2014551

From the CLLocation-Bearing project by tadeldv: https://github.com/tadelv/CLLocation-Bearing

来自 tadeldv 的 CLLocation-Bearing 项目:https: //github.com/tadelv/CLLocation-Bearing

CGFloat DegreesToRadians(CGFloat degrees) {return degrees * M_PI / 180;}
CGFloat RadiansToDegrees(CGFloat radians) {return radians * 180 / M_PI;}

- (void)calculating bearing {  
        float lat1 = DegreesToRadians(YOURCURRENTPOSITION.coordinate.latitude);
        float lng1 = DegreesToRadians(YOURCURRENTPOSITION.coordinate.longitude);
        float lat2 = DegreesToRadians(YOURTARGETPOSITION.coordinate.latitude);
        float lng2 = DegreesToRadians(YOURTARGETPOSITION.coordinate.longitude);
        float deltalng = lng2 - lng1;
        double y = sin(deltalng) * cos(lat2);
        double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(deltalng);
        double bearing = atan2(y, x) + 2 * M_PI;
        float bearingDegrees = RadiansToDegrees(bearing);
        bearingDegrees = (int)bearingDegrees % 360;

        NSLog(@"%d", (int)bearingDegrees);
}

this gives you the angel between north pole and your target. then you have to add/subtract the angle of your current heading.

这为您提供了北极和目标之间的天使。那么您必须添加/减去当前航向的角度。

回答by moonix82

thanks and sry for my delated answer. I am working on an arrow showing to a specific direction. What I did now is, getting this arrow doing an animation but my problem is, that it should (at first) point to the same direction like the iphone is "heading".

感谢并感谢我迟到的回答。我正在处理显示特定方向的箭头。我现在所做的是,让这个箭头做一个动画,但我的问题是,它应该(首先)指向同一个方向,就像 iphone 正在“前进”一样。

My code so far:

到目前为止我的代码:

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{double heading = newHeading.trueHeading;
[self rotateCompass:heading];
}

- (void) rotateCompass: (double) degrees{
[UIView beginAnimations:nil context:NULL];
[UIView setAnimationDelegate:self];
[UIView setAnimationDuration:2];
[UIView setAnimationCurve:UIViewAnimationCurveEaseIn];
[UIView commitAnimations];
compassRose.transform = CGAffineTransformMakeRotation(degrees);
}

I have other routines to get the latitude, longitude and labels showing me the heading and it works. My arrow spins around madly without any reason. I want the arrow points on the same direction like the trueHeading. That means in the way the iPhone

我有其他例程来获取纬度、经度和标签,显示标题并且它有效。我的箭无缘无故的疯狂旋转。我希望箭头指向与 trueHeading 相同的方向。这意味着 iPhone 的方式