Android gets distance using Location object

Id would like to take a series of coordinate samples returned by GPS and calculate the distance (straight line) between them so that I can calculate the distances via Excel. I see the method distanceBetweenand the distanceToclass Location, but Im related to this does not return the distance in a straight line.

Does anyone know what distance these methods return, or are there ways to calculate the distance in a straight line based on the latitude / longitude values ​​returned by the class Location?

+3
source share
4 answers

A Google search will offer solutions if you somehow want to do this calculation yourself. For example, the Haversin 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;

. , ..

+2

+ Java?

Java Haversine.

+1

Here is 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()));
0
source

source = starting location;

destination = current location;

/ * this method will give you the distance between two geographical points. * /

source.distanceTo (destination);

-1
source

All Articles