iPhone compass showing to a specific location

依然范特西╮ 提交于 2019-12-03 00:43:49
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:

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:

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 CMMotionManager to get access to the gyroscope, which will also point you in the right direction. (pun intended!) Hope that Helps!

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.

From the CLLocation-Bearing project by tadeldv: 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.

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".

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!