views:

72

answers:

4

What is the structure of a generic (or device independent) physical location? My guess is it might be a struct with two long fields, or something similar.

Also, given one destination location, and two candidate locations, is there a simple algorithm for determining which candidate is closest to the destination? I'm not really looking for a library or service that handles all of this, though that could be an option (in Java), rather I want some very simple low level concepts that I can actually manipulate myself.

Thanks!

Edit Given the intricasies of the calculations noted by f1sh - is there a nice small Java library that handles haversine calculations?

A: 

Yes, a class like

public Geo {
  private double lat;
  private double lon;
}

is sufficient to store a geographical location. You might want to add setter method to make sure, that lat, lon are always in a valid range, otherwise a Geo object might have an invalid state.

Andreas_D
ok thanks, do you know what are the specific ranges?
MalcomTucker
@MalcomTucker - f1sh already included the ranges in his answer.
Andreas_D
he hadnt posted when I commented but thanks! :)
MalcomTucker
+1  A: 

GPS devices are commonly provides following data about location:

  1. Latitude
  2. Longitude
  3. Altitude
  4. Horizontal accuracy
  5. Vertical accuracy

(not to speak about speed, destination, satellites, etc.)

Now second part of your question: you can use haversine formula to calculate distance between all candidates and your location, and then sort them by this distance. Not sure about some more generic / scientific approach.

EDIT: Take a look at haversine formula here. Code example is also there. I don't think that you need some library for this.

Haspemulator
+4  A: 

Storing latitude and longitude in a class should't be a problem to anybody, as you and Andreas_D already mentioned (2 double fields).

The tricky part is that calculating the distance between 2 Points on the surface of this planet is not as simple as the common distance formula between 2 2D-Points. The following facts have to be considered:

  • While the latitude ranges from -90° (South Pole) to +90°, the longitude is periodical. That means that the point (0°, 179°) has a distance of only 1° to the point (0°, -180°).
  • Earth is a sphere. That results in the fact that conversion from lat/long to the metric system (which I hope you are using...) is not quite easy. 1° of longitude at the equator (which is at 0° latitude) is about 111km, whereas at the exact North pole 1° of longitude is 0 (km/inches/feet/whatever).
  • More math.
f1sh
A: 

This is apparently the most accurate formula, the haversine being inaccurate over short diatances:

//L = latitude, G = longtitude
double delta = G1 - G2;  
double p1 = cos(L2) * sin(delta);
double p2 = cos(L1) * sin(L2) - sin(L1) * cos(L2) * cos(delta);  
double p3 = sin(L1) * sin(L2) + cos(L1) * cos(L2) * cos(delta);
distance = 60 * Math.atan2(Math.sqrt(p1*p1 + p2*p2), p3);

Reference

MalcomTucker