Search code examples
iphoneobjective-cioscllocation

CLLocation - Find magnetic declination / deviation at another location


I know how to find the true heading/magnetic heading for the location my phone is currently at, but is it possible to find the magnetic deviation/declination for a remote location?

What I would like to do is be able to drop a pin at a place on the map and find both the true bearing and the bearing with magnetic variance from that point.

Thanks!


Solution

  • Have you solved it? Otherwise you can calculate the azimuth angle of the remote location. Firstly with magnetic north heading and then with true north heading. Finally you subtract the two to get the magnetic deviation.

    Here is how to calculate an azimuth angle for a remote location:

    -(float)azimuthFromLocations:(CLLocationCoordinate2D)first toCoordinate:(CLLocationCoordinate2D)second{
    float longitudeDifference = second.longitude - first.longitude;
    float latitudeDifference = second.latitude  - first.latitude;
    float possibleAzimuth = (M_PI * .5f) - atan(latitudeDifference / longitudeDifference);
    
    if (longitudeDifference > 0)
        return possibleAzimuth;
    else if (longitudeDifference < 0)
        return possibleAzimuth + M_PI;
    else if (latitudeDifference < 0)
        return M_PI;
    
    return 0.0f;
    }