1

I am developing an application in which I need to calculate the distance travelled. I am facing a problem while calculating the distance. Here is my code:

 locationmanager.requestLocationUpdates(bestProvider,0,0,new Listener());

 public class Listener implements LocationListener{

@Override
public void onLocationChanged(Location location) {
    if(location!=null) {
        if(loc1==null) {
            loc1=new Location(location);
        }

        speed=location.getSpeed()*3.6;
        loc2=new Location(location);
        dist+=loc1.distanceTo(loc2);
        loc1=loc2;
        breaktv.setText(""+dist/1000);

My intention is calculate the total distance travelled by car or something. But distanceTo() method is not working well for me.

gniourf_gniourf
  • 44,650
  • 9
  • 93
  • 104
Taruni
  • 1,671
  • 4
  • 22
  • 43
  • "is not working well for me" doesn't give us much to go on. What are you seeing? How do you know it's not working? –  Nov 15 '11 at 06:17
  • What is the question and what is the input? Euclidian distance is often quite easy to compute (using square roots, squares, additions, substractions of coordinates)? – Basile Starynkevitch Nov 15 '11 at 06:18
  • Here I requested for updates frequently.So onLocationChanged() will be called everytime when there is a change in location.I am facing problem at this point of code while calculating the distance.I am adding distance every time here,is it correct.? loc2=new Location(location); dist+=loc1.distanceTo(loc2); loc1=loc2; breaktv.setText(""+dist/1000); – Taruni Nov 15 '11 at 06:27

2 Answers2

2

Technically it should work.

I have some suggestions :

  • dist must be float (if it isn't already)
  • dist += loc2.distanceTo(location), there is no need to create loc1

This is the code I've used

                if(lastLoc != null)
                {
                    ttf = (location.getTime() - lastLoc.getTime()) / 1000;
                    int R = 6371;
                    double lat1 = Math.PI / 180.0 *lastLoc.getLatitude();
                    double lon1 = Math.PI / 180.0 *lastLoc.getLongitude();
                    double lat2 = Math.PI / 180.0 *location.getLatitude();
                    double lon2 = Math.PI / 180.0 *location.getLongitude();
                    //  double dLon = Math.PI / 180.0 * (location.getLongitude() - lastLoc.getLongitude());
                    double dLat = (lat2-lat1);
                    double dLon = (lon2-lon1);
                    double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                    Math.cos(lat1) * Math.cos(lat2) * 
                    Math.sin(dLon/2) * Math.sin(dLon/2); 
                    double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
                    double d = R * c;
                   totalDistance += d;

                }

                lastLoc = location;
Reno
  • 33,594
  • 11
  • 89
  • 102
  • +1 Hello,Can you please look at this question,I have been searching for this from long long time http://stackoverflow.com/questions/7696112/gettting-different-distance-when-using-the-google-map-and-user-defined-function/8050014#8050014 ..also If you post anything there..please also do a comment on my answer so i get to know. – MKJParekh Nov 15 '11 at 07:10
1

Here is another Question on SO where I have posted 2 Answers one for the User Defined function to get the distance and other to User android.Location class method to calculate it. Hope this will help you.

When You start your jurney take the CurrentLocation and store it may be using SharedPreferences, Then I think you need to Continuosly keep updating the Distance Travelled, You just use OnLocationChanged same way and Call the method above two mentioned from there to know the Current Distance Travelled.

EDIT: here is a good example that may help you,Link To OpenSource Project

Community
  • 1
  • 1
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
  • Hii Frankenstein thank you for your reply.This is exactly what I am trying to do.I stored the previous location in loc1 object and new location in loc2,and I am trying to use both these objects in distanceTo() method and adding the result everytime.Am I going In right direction???? – Taruni Nov 15 '11 at 06:39
  • I dont see any problem...please see updated ans i have add one more link. – MKJParekh Nov 15 '11 at 06:46