CLLocation - Find magnetic declination / deviation at another location
Asked Answered
F

3

7

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!

Fervor answered 5/6, 2012 at 9:8 Comment(1)
One of my ideas was to write it to CLLocation's location property, but that ended up being readonly, so that is out the window.Fervor
P
4

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;
}
Preponderance answered 14/11, 2012 at 22:44 Comment(3)
Thanks, the solution ended up being a little bit more complicated though. I had to use the world magnetic model to figure out the deviation for a given coordinate.Fervor
*variation. Deviation is in the sensor.Damnatory
This solution is only (approximately) true near the equator. As you get nearer the poles, the azimuth must be based on distances, not lat/lon coordinates.Trude
B
5

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
Broncobuster answered 25/1, 2014 at 0:5 Comment(0)
P
4

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;
}
Preponderance answered 14/11, 2012 at 22:44 Comment(3)
Thanks, the solution ended up being a little bit more complicated though. I had to use the world magnetic model to figure out the deviation for a given coordinate.Fervor
*variation. Deviation is in the sensor.Damnatory
This solution is only (approximately) true near the equator. As you get nearer the poles, the azimuth must be based on distances, not lat/lon coordinates.Trude
C
1

I needed the same solution in Swift, based on the World Magnetic Model.

Here is my implementation: https://github.com/kanchudeep/Geomagnetism-Swift

It's a simple single class which can be dropped into any project and used.

Condon answered 11/12, 2017 at 9:34 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.