How can I compare two positions in onLocationChanged? [closed]

0

I want to obtain the coordinates of the method onLocationChanged can compare if the distance to the previous one is greater than 2 meters, and so not register it in database.

    
asked by Jhonatan Garcia 05.10.2016 в 23:06
source

2 answers

2

It's very easy to do and you do not have to calculate anything, the Android Location class includes a method that helps you calculate the distance between two points. link

The process would be such that:

/ ** Location A ** /

Location locationA = new Location ("point A");

locationA.setLatitude (latA);

locationA.setLongitude (lngA);

/ ** Location B ** /

Location locationB = new Location ("point B");

locationB.setLatitude (latB);

locationB.setLongitude (lngB);

/ ** Distance between the two objects Location ** /

float distance = locationA.distanceTo (locationB);

Greetings

(In English link )

    
answered by 12.10.2016 в 21:14
0

The way to solve it is with this function

private double getDistance(double fromLat , double fromLong, double toLat, double toLong) {
    double d2r = Math.PI / 180;
    double dLong = (toLong - fromLong) * d2r;
    double dLat = (toLat - fromLat) * d2r;
    double a = Math.pow(Math.sin(dLat / 2.0), 2) + Math.cos(fromLat * d2r)
            * Math.cos(toLat * d2r) * Math.pow(Math.sin(dLong / 2.0), 2);
    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
    double d = 6367000 * c;
    return Math.round(d);
}

Receives 4 parameters latitude and longitude of the first position and latitude and longitude of the second position.

The method returns the distance in meters between the points.

    
answered by 07.10.2016 в 18:26