问题
I am new to objective-c so I want to apologize if my question is too rubbish.
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.
回答1:
- (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:
- http://www.movable-type.co.uk/scripts/latlong.html with eplanations and Javascript exampless, but very easy to convert! :-)
- https://github.com/tadelv/CLLocation-Bearing as Objective C Project
回答2:
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!
回答3:
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.
回答4:
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.
回答5:
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
来源:https://stackoverflow.com/questions/7298193/iphone-compass-showing-to-a-specific-location