Android Question Magnetic Declination

Lakhtin_V

Active Member
Licensed User
Longtime User
I'd like to know if there are any working ways to find the magnetic declination for a specific location. I looked on the forum and found that this was a problem before. Perhaps the situation has changed with modern Android versions? Is there an example of how to download this value from the internet?
 

TILogistic

Expert
Licensed User
Longtime User
B4X:
Dim jo As JavaObject
jo.InitializeNewInstance("android.hardware.GeomagneticField", Array( _
    Latitude, _
    Longitude, _
    Altitude, _
    DateTime.Now))
    
Dim declinacion As Float = jo.RunMethod("getDeclination", Null)

Log("Declinación magnética: " & declinacion & "°")

Note:

* `Latitude` → `Float` (ex. `-33.4489`)
* `Longitude` → `Float` (ex. `-70.6693`)
* `Altitude` → `Float` meters (ex. `570`)
* `DateTime.Now` → `Long` current datetime.

Other Data:
B4X:
Dim inclinacion As Float = jo.RunMethod("getInclination", Null)
Dim intensidad As Float = jo.RunMethod("getFieldStrength", Null)
Dim horizontal As Float = jo.RunMethod("getHorizontalStrength", Null)
Dim vertical As Float = jo.RunMethod("getZ", Null)
Dim norte As Float = jo.RunMethod("getX", Null)
Dim este As Float = jo.RunMethod("getY", Null)
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User
Class Module: Geomagnetic.bas
B4X:
Sub Class_Globals
    Private jo As JavaObject
    Public Declination As Float
    Public Inclination As Float
    Public TotalStrength As Float
    Public HorizontalStrength As Float
    Public X As Float
    Public Y As Float
    Public Z As Float
End Sub

'Initialize the geomagnetic
Public Sub Initialize(Latitude As Float, Longitude As Float, Altitude As Float, Time As Long)
    jo.InitializeNewInstance("android.hardware.GeomagneticField", Array As Object(Latitude, Longitude, Altitude, Time))
    Refresh
End Sub

'Reload all properties
Public Sub Refresh
    Declination = jo.RunMethod("getDeclination", Null)
    Inclination = jo.RunMethod("getInclination", Null)
    TotalStrength = jo.RunMethod("getFieldStrength", Null)
    HorizontalStrength = jo.RunMethod("getHorizontalStrength", Null)
    X = jo.RunMethod("getX", Null)
    Y = jo.RunMethod("getY", Null)
    Z = jo.RunMethod("getZ", Null)
End Sub

'Converts a magnetic heading into a true heading.
Public Sub MagneticToTrue(MagneticHeading As Float) As Float

    Dim h As Float = MagneticHeading + Declination

    Do While h < 0
        h = h + 360
    Loop

    Do While h >= 360
        h = h - 360
    Loop

    Return h

End Sub

'Converts a true heading to a magnetic heading
Public Sub TrueToMagnetic(TrueHeading As Float) As Float

    Dim h As Float = TrueHeading - Declination

    Do While h < 0
        h = h + 360
    Loop

    Do While h >= 360
        h = h - 360
    Loop

    Return h

End Sub

USE:

B4X:
Dim Geo As Geomagnetic

Geo.Initialize( _
    gps.Latitude, _
    gps.Longitude, _
    gps.Altitude, _
    DateTime.Now)

Log("Declinación = " & Geo.Declination)
Log("Inclinación = " & Geo.Inclination)
Log("Intensidad = " & Geo.TotalStrength & " nT")
Log("Horizontal = " & Geo.HorizontalStrength & " nT")
Log("X = " & Geo.X)
Log("Y = " & Geo.Y)
Log("Z = " & Geo.Z)

Dim RumboVerdadero As Float = Geo.MagneticToTrue(125)
Log("Rumbo verdadero = " & RumboVerdadero)
 
Upvote 0

John Naylor

Active Member
Licensed User
Longtime User
I'd like to know if there are any working ways to find the magnetic declination for a specific location. I looked on the forum and found that this was a problem before. Perhaps the situation has changed with modern Android versions? Is there an example of how to download this value from the internet?
Try this
 
Upvote 0

TILogistic

Expert
Licensed User
Longtime User

The Android API uses the same World Magnetic Model (WMM) locally

1783897323873.png


without using external calculations such as: https://geomag.bgs.ac.uk/web_service/GMModels

1783897389068.png
 
Upvote 0
Top