Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 409
Steven Marlowe Avatar asked Jun 05 '12 09:06

Steven Marlowe


2 Answers

The code to calculate this must already exist in a framework somewhere since it's used by CLHeading when location services are available.

If anyone can find that code or has Objective C for the world magnetic model posting it would be appreciated.

UPDATE: I found a great open source iOS wrapper! Thanks Crookneck! https://github.com/stephent/ObjectiveWMM

Simple installation as a git submodule

run this command:
$git submodule add https://github.com/stephent/ObjectiveWMM.git ObjectiveWMM

Add these files to your Xcode project:
CCMagneticDeclination.h
CCMagneticDeclination.m
CCMagneticModel.h
CCMagneticModel.m
NSDate+DecimalYear.h
NSDate+DecimalYear.m
WMM/EGM9615.h
WMM/GeomagnetismHeader.h
WMM/GeomagnetismLibrary.c
WMM/WMM.COF
like image 101
Chris Hobbs Avatar answered Nov 03 '22 01:11

Chris Hobbs


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;
}
like image 22
Tíbó Avatar answered Nov 03 '22 00:11

Tíbó