3

Till now, I have inserted all the sets of latitude and longitude in my database after every 5 sec from the path I have travelled.

Now after reaching the destination I have to calculate the distance from the sets of Latitude, longitude. It's not about calculating the travelling distance between two points but to get the total distance travelled from set of lat,long in my database .

I have thought a way of calculating the total distance by getting the distance between the two points and add them up at last

Is there any better solution than this? please suggest and help

Already read this SO post but not getting how I will get the total distance by using the Google Maps Distance Matrix API

///////service to insert coordinates into the database.

private class MyReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context arg0, Intent arg1) {
            String name = arg1.getAction();
            if (name.equalsIgnoreCase("Location_Changed")) {
                db.insertlatlong(new Latilongi(arg1.getStringExtra("latitude"),
                        arg1.getStringExtra("longitude"),currentDateandTime));


                   } else if (arg1.getAction().matches("android.location.PROVIDERS_CHANGED")) {
                Toast.makeText(MapActivity.this, "in android.location.PROVIDERS_CHANGED",
                        Toast.LENGTH_SHORT).show();
            }
        }
    }
Community
  • 1
  • 1
Nilabja
  • 4,206
  • 5
  • 27
  • 45

3 Answers3

0

you can use the following method that will give you acurate result

public double CalculationByDistance(LatLng StartP, LatLng EndP) {
        int Radius = 6371;// radius of earth in Km
        double lat1 = StartP.latitude;
        double lat2 = EndP.latitude;
        double lon1 = StartP.longitude;
        double lon2 = EndP.longitude;
        double dLat = Math.toRadians(lat2 - lat1);
        double dLon = Math.toRadians(lon2 - lon1);
        double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
                + Math.cos(Math.toRadians(lat1))
                * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
                * Math.sin(dLon / 2);
        double c = 2 * Math.asin(Math.sqrt(a));
        double valueResult = Radius * c;
        double km = valueResult / 1;
        DecimalFormat newFormat = new DecimalFormat("####");
        int kmInDec = Integer.valueOf(newFormat.format(km));
        double meter = valueResult % 1000;
        int meterInDec = Integer.valueOf(newFormat.format(meter));
        Log.i("Radius Value", "" + valueResult + "   KM  " + kmInDec
                + " Meter   " + meterInDec);

        return Radius * c;
    }
Rathod Vijay
  • 417
  • 2
  • 7
0

You can insert only distance between two co-ordinates and lastly sum up all distances you had in database. Distance achieve like below.

float distanceInMeters =  lastLocation.distanceTo(myLocation);

where "lastLocation" and "myLocation" are of Location object.

If you dont have Location object than you can try this.

Location lastLocation = new Location("");
lastLocation.setLatitude(latitude);
lastLocation.setLongitude(longitude);

Location myLocation = new Location("");
myLocation.setLatitude(otherlatitude);
myLocation.setLongitude(otherlongitude);

float distanceInMeters =  lastLocation.distanceTo(myLocation);
Akash Patel
  • 2,757
  • 1
  • 22
  • 30
0

try this one

 private  double distance(double lat1, double lon1, double lat2, double lon2, String unit) {
    double theta = lon1 - lon2;
    double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
    dist = Math.acos(dist);
    dist = rad2deg(dist);
    dist = dist * 60 * 1.1515;
    if (unit == "K") {
        dist = dist * 1.609344;
    } else if (unit == "N") {
        dist = dist * 0.8684;
    }

    return (dist);
}

/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*::    This function converts decimal degrees to radians                        :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
private static double deg2rad(double deg) {
    return (deg * Math.PI / 180.0);
}
anu
  • 213
  • 1
  • 3
  • 10