4

I have an interesting question. I have a latitude value of 51.445376 and a longitude value of -0.190624 (as an example, say this was recieved by an androids location listener). This particular location (and it's values) is a point of interest and it is stored in my database. I am using a location listener on my android, and I will send the users current location to the server to detect if the user has come within a one kilometre radius of that particular point of interest. Does anyone know what algorithm to use to check if the users current longitude and latitude is within that one kilometre radius of the point of interests longitude and latitude? Or something I can see?

Perhaps someone could help me out, as my math is not the greatest in the world.

Jack Gleeman
  • 593
  • 1
  • 6
  • 19
  • [This](http://stackoverflow.com/questions/481144/how-do-you-test-if-a-point-is-inside-a-circle) might help determine if a point is within a circle. – thisisdee Sep 26 '11 at 16:01
  • Can you instead retrieve the POI from the server, pass it to the client and use the [Location.distanceTo(or Between)](http://developer.android.com/reference/android/location/Location.html) API call? – Alex Sep 26 '11 at 16:23
  • An interesting concept and I will definitely use that also. Thankyou. – Jack Gleeman Sep 26 '11 at 16:35

3 Answers3

4

For geographic coordinates, use this function (public domain):

public static double distanceInKM(
           double latStart,double lonStart,
           double latEnd,double lonEnd
){
      latStart=Math.toRadians(latStart);
      latEnd=Math.toRadians(latEnd);
      return 6370.997*Math.acos(
              Math.cos(Math.toRadians(lonEnd - lonStart))*Math.cos(latStart)*
              Math.cos(latEnd)+Math.sin(latStart)*Math.sin(latEnd));
}

Calculating the shortest geographic path is called the "inverse geodesic problem", and this is discussed in C.F.F. Karney's article "Algorithms for geodesics, 2012. The method above shows a distance function based on the spherical law of cosines, which is a less accurate way to compute this path, especially because it assumes Earth is a perfect sphere.

Peter O.
  • 32,158
  • 14
  • 82
  • 96
1

This post from 2008 might be helpful.

It links to a MovableType description of how to use the Haversine formula to calculate the distance between two points on a sphere. It's important to note that it's not 100% accurate since the Earth is not a perfect sphere (it's equatorial radius is some 20km different from its polar radius) but should be sufficient for your needs.

The MovableType link also describes how to use an equi-rectangular projection (translation of lat/long to rectangular coordinates) and the pythagorean theorem to determine a rougher (but faster) approximation of the distance (good for small distances of ~1-2km.)

Community
  • 1
  • 1
ParoXoN
  • 582
  • 2
  • 10
0

A simple solution: Simply find the distance, by first finding dx and dy.

Say I have point 1 (p1) and point 2 (p2)

dx = p2.x - p1.x; dy = p2.y - p1.y;

Now find the distance d as follows

d = sqrt(dx^2 + dy^2)

In a programming language like java, you can use the following lines:

double d = Math.sqrt(dx*dx + dy*dy);

Hope this helps as your starting point!

Note: x & y must be calculated accordingly for your lat&lon info. I was going to write a short code snippet but I saw someone else has a nice solution (I upvoted his response).

omt66
  • 4,765
  • 1
  • 23
  • 21
  • 2
    That won't work with latitude and longitude, it only works on a planar surface. – interjay Sep 26 '11 at 16:12
  • That's why I said "A simple solution" in deed. Most of the time, it is sufficient, since he is concerned about a 1 km distance. If this is not accurate enough then use the Great-Circle distance: http://en.wikipedia.org/wiki/Great-circle_distance – omt66 Sep 26 '11 at 16:17
  • Are you suggeting to directly use the latitude and longitude values as `p1.x` and `p1.y`? If so, the results will be completely meaningless. If not, this answer is only "simple" because it is incomplete - where do the `x` and `y` values come from? – interjay Sep 26 '11 at 16:23
  • Look interjay, if this is not sufficient for you, you can come up with your own solution, you don't need to pay attention to mine! I said "A simple solution", not necessarily a complete and sufficient solution for your problem domain! There is no need to argue or downgrade the entry! – omt66 Sep 26 '11 at 16:54