Hi there,
I'm trying to make an image aka compass point to a certain coordinate.
With this code I get my compass to point to the North:
Now what I'm trying to is the following:
Somehow it's not working quite right... I took this function 'setLatLonForDistanceAndAngle' one of all other pages I been searching around about bearing with coordinates.
The big problems I'm facing:
- The locationManager stops getting current location after ... x amount of seconds.
- The needle is not rotating correctly.
Is there anybody that has made something like this before, and is willing to help me out?
Kind regards!
I'm trying to make an image aka compass point to a certain coordinate.
With this code I get my compass to point to the North:
Code:
- (void)viewDidLoad
{
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate = self;
[locationManager startUpdatingLocation];
[locationManager startUpdatingHeading];
}
-(BOOL) shouldAutorotate {
return NO;
}
- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
return UIInterfaceOrientationPortrait;
}
#pragma mark -
#pragma mark CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading{
// Convert Degree to Radian and move the needle
float oldRad = -manager.heading.trueHeading * M_PI / 180.0f;
float newRad = -newHeading.trueHeading * M_PI / 180.0f;
CABasicAnimation *theAnimation;
theAnimation=[CABasicAnimation animationWithKeyPath:@"transform.rotation"];
theAnimation.fromValue = [NSNumber numberWithFloat:oldRad];
theAnimation.toValue=[NSNumber numberWithFloat:newRad];
theAnimation.duration = 0.1f;
[compassImage.layer addAnimation:theAnimation forKey:@"animateMyRotation"];
compassImage.transform = CGAffineTransformMakeRotation(newRad);
}
Now what I'm trying to is the following:
Code:
-(IBAction)setLocation:(id)sender {
tLat = locationManager.location.coordinate.latitude;
tLon = locationManager.location.coordinate.longitude;
}
//Using
#define RadiansToDegrees(radians)(radians * 180.0/M_PI)
#define DegreesToRadians(degrees)(degrees * M_PI / 180.0)
-(float)setLatLonForDistanceAndAngle:(CLLocation *)userlocation
{
float lat1 = DegreesToRadians(userlocation.coordinate.latitude);
float lon1 = DegreesToRadians(userlocation.coordinate.longitude);
float lat2 = DegreesToRadians(tLat);
float lon2 = DegreesToRadians(tLon);
float dLon = lon2 - lon1;
float y = sin(dLon) * cos(lat2);
float x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon);
float radiansBearing = atan2(y, x);
if(radiansBearing < 0.0)
{
radiansBearing += 2*M_PI;
}
return radiansBearing;
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
GeoAngle = [self setLatLonForDistanceAndAngle:newLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
{
float direction = -newHeading.trueHeading;
theCompass.transform=CGAffineTransformMakeRotation((direction* M_PI / 180)+ GeoAngle);
}
Somehow it's not working quite right... I took this function 'setLatLonForDistanceAndAngle' one of all other pages I been searching around about bearing with coordinates.
The big problems I'm facing:
- The locationManager stops getting current location after ... x amount of seconds.
- The needle is not rotating correctly.
Is there anybody that has made something like this before, and is willing to help me out?
Kind regards!