3

I’d like to take a series of samples of coordinates returned by GPS and calculate the (straight line) distance between them so I can graph the distances via Excel. I see the method distanceBetween and distanceTo of the Location class, but I’m concerned these don’t return the straight line distance.

Does anyone know what distance is returned by these methods or if there are any ways to calculate straight line distance based on latitude/longitude values returned by the Location class?

user432209
  • 20,007
  • 10
  • 56
  • 75
  • What do you mean by not returning straight line distance? The earth's surface *is* curved and the distance between two close points *should* be almost as good as a straight line. – Reno Mar 15 '11 at 17:11
  • Did I say they were close together? – user432209 Mar 15 '11 at 17:22
  • @432209 The earth will still have a curved surface. The answers below takes into account the curvature. – Reno Mar 15 '11 at 17:42

4 Answers4

2

A Google search will offer solutions should you somehow desire to do this calculation yourself. For example the Haversine approach:

var R = 6371; // km
var dLat = (lat2-lat1).toRad();
var dLon = (lon2-lon1).toRad(); 
var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
        Math.cos(lat1.toRad()) * Math.cos(lat2.toRad()) * 
        Math.sin(dLon/2) * Math.sin(dLon/2); 
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
var d = R * c;

is reported here. Note that this is straight line and does not account for irregularities in elevation, etc.

DJC
  • 3,243
  • 3
  • 27
  • 31
1

How can I measure distance and create a bounding box based on two latitude+longitude points in Java?

provides a Java implementation of the Haversine approach.

Community
  • 1
  • 1
Sean
  • 7,562
  • 10
  • 27
  • 29
0

Here my code

float[] result=new float[1];
        if(cordenatalar.size()>1)
        {

            LatLong add_kor=(LatLong)cordenatalar.get(cordenatalar.size()-1);
            Location.distanceBetween(add_kor.getLat(), add_kor.getLongg(), location.getLatitude(), location.getLongitude(), result);
            kilometr+=result[0];
            //KMTextView.setText(String.valueOf(kilometr));
        }
        KMTextView.setText("sss: "+String.valueOf(cordenatalar.size()+"  res: "+kilometr+" metr"));
        cordenatalar.add(new LatLong(location.getLatitude(), location.getLongitude()));
DasturchiUZ
  • 69
  • 1
  • 12
-1

source = starting location;

destination = current location;

/* this method will fetch you the distance between two geo points. */

source.distanceTo(destination);

  • The documentation of distanceTo() states it's defined using the WGS84 ellipsoid. The OP needs the straight line distance, so this isn't a valid answer. – Sandy Sep 25 '14 at 15:35