IOS: get angle from geolocation from current location without map

I am working on some iPhone application, and I want to make a bearing, an image that moves to a specific geographic location, depending on the user's location with an accelerometer.

I read a lot of answers here, but didn't get a solution.

I have the coordinates of the current location and destination.

Do you have an idea or sample code? Thank.

+5
source share
1 answer

top define it

#define RadiansToDegrees(radians)(radians * 180.0/M_PI)
#define DegreesToRadians(degrees)(degrees * M_PI / 180.0)

define a variable in the .h file

float GeoAngle;

in the location manager delegation method: -

-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation 
{
    GeoAngle = [self setLatLonForDistanceAndAngle:newLocation];
}

And the function will look like this: -

-(float)setLatLonForDistanceAndAngle:(CLLocation *)userlocation
{
    float lat1 = DegreesToRadians(userlocation.coordinate.latitude);
    float lon1 = DegreesToRadians(userlocation.coordinate.longitude);

    float lat2 = DegreesToRadians(locLat);
    float lon2 = DegreesToRadians(locLon);

    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;
}

GeoAngle. , IBOutlet ImageView .

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading 
{
    float direction = -newHeading.trueHeading;

    arrowImageView.transform=CGAffineTransformMakeRotation((direction* M_PI / 180)+ GeoAngle);
}
+9

All Articles