Question

I'm build an app for an Android smartphone with Phonegape and jQuery mobile.

I want to build an app which uses an arrow which points to a given geolocation, so the user knows in which direction he/she has to go to reach that point.
So I need a function or formula which determines an angle (preferably in degrees) to the destination geo position. Does anyone know how I can do this?

Was it helpful?

Solution

What you're looking for is called an azimuth, that is the angle between a given object and the north.

To find that angle you use the formula: a = arctan(|(y2-y1)/(x2-x1)|) * 180/pi Where point A is (y2,x2) and point B is (y1,x1).

As to how you do it in code, I have no idea. I don't work with those platforms. Anyone has any input on this?

OTHER TIPS

Its necessary to compute using Geodesic distances, or the angle computed will be significantly off, esp. the closer to the poles.

For android it can be done simply as this.

data class GpsLocation(
        val latitude: Float, // Runs from south-pole to north pole
        val longitude: Float // Runs East-West
) 


fun GpsLocation.toLocation(): Location {
    val location = Location(LocationManager.PASSIVE_PROVIDER)
    location.latitude = latitude.toDouble()
    location.longitude = longitude.toDouble()
    //location.time = System.currentTimeMillis()
    return location
}

fun GpsLocation.angleToOther(other: GpsLocation): Float = 
    toLocation().bearingTo(other.toLocation())

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top