3

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?

Janusz
  • 187,060
  • 113
  • 301
  • 369
Siggy Petersen
  • 195
  • 3
  • 6
  • 16

2 Answers2

4

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?

Michael
  • 2,910
  • 3
  • 15
  • 26
  • Hey Michael, thanks for reply! So i have to determine the azimuth for both positions and after that i can determine the angle of position 1 to position 2, right? – Siggy Petersen Aug 12 '11 at 12:23
  • No, you see, once you have the XY (or cartesian, same thing) coordinates of both points you plug them into the formula and get the angle between the north and the line connecting both points. Say you're standing at point A whose coordinates are (100, 100) and you want to go to point B whose coordinates are (200,200). You plug those values into the formula to find the angle at which you should walk: `a = arctan(|(200-100)\(200-100)|) = arctan(|100\100|) = arctan 1 = 0.78` This means you should go 0.78 degrees from point A to reach point B. – Michael Aug 12 '11 at 12:36
  • OK, sorry, that's wrong. The result you get is in radians (another form to messure angles). What you need to do is to multiply the result by 180/pi. In our example that would give you 45 degrees, which makes more sense. A revised formula: 'a = arctan(|(y2-y1)/(x2-x1)|) * 180\pi' – Michael Aug 12 '11 at 12:48
  • Ok i build your formula in javascript `var dLat = rad(200 - 100); var dLon = rad(200 - 100); var a = Math.atan2(dLat,dLon);` This works fine, but if i use geopositions the return value is not possible – Siggy Petersen Aug 12 '11 at 12:56
  • This will off by quite a lot the closer to the poles we get, if we do not use Geocedic distances, and computing those is not simple. Using euclidic (numeric deltaX,deltaY) it is quite a lot off in Europe/Denmark. See code in my answer. – arberg Jun 13 '20 at 07:45
0

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())

arberg
  • 4,148
  • 4
  • 31
  • 39