-3

I want to develop a small app that calculates the distance while traveling. I have tried the solution below, based on the GPS of Android, but the result is wrong.

Please help me on this

My code:

locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 10, this);
  public void onLocationChanged(Location location) {

    if ((location != null)) {
        if (location.getAccuracy()<50 && location.getSpeed()>0) {
            onlocationChangeMethod(location);
            lastlatitude = location.getLatitude();
            lastlongitude = location.getLongitude();

        }

    }
}
public void onlocationChangeMethod(Location location){
    if (lastlatitude > 0 ) {
            float[] results = new float[5];
            location.distanceBetween(location.getLatitude(), location.getLongitude(),
                    lastlatitude, lastlongitude, results);
    }
}
Shlublu
  • 10,917
  • 4
  • 51
  • 70
Mallikarjun P
  • 83
  • 2
  • 7

2 Answers2

2

distance can be accurate only if you get the gps locations in very frequent intervals. So, one way is to calculate the distance based on gps coordinate shifts. This cant be 100% accurate

Also got to hear that there are internal tracking devices fitted inside the Ola/Uber car dash which can track the fleet accurately and communicate with their server(tracking devices comes with sim so that they can communicate to the servers which have their gateways installed). That means, if Ola and Uber are so accurate with the distance, they may be relying upon the tracking devices installed within the car dash and sourcing the info through the server.

Aun
  • 1,883
  • 1
  • 17
  • 26
0

GPS: Consider A as starting Point.

Keep adding GPS distance between 2 points(A and current location) every X seconds (say 10 sec). Check Android Location.distanceTo or distanceBetween. Check My Tracks app, it is open source. GPS is not available indoors and would have error if user is changing direction very frequently (read every 1-2 second)

For getting Accurate Distance,call Google WebService with your location latitude and Longitude,it will return distance perfectly close to Accurate

private String getDistanceOnRoad(double latitude, double longitude,
            double prelatitute, double prelongitude) {
        String result_in_kms = "";
        String url = "http://maps.google.com/maps/api/directions/xml?origin="
                + latitude + "," + longitude + "&destination=" + prelatitute
                + "," + prelongitude + "&sensor=false&units=metric";
        String tag[] = { "text" };
        HttpResponse response = null;
        try {
            HttpClient httpClient = new DefaultHttpClient();
            HttpContext localContext = new BasicHttpContext();
            HttpPost httpPost = new HttpPost(url);
            response = httpClient.execute(httpPost, localContext);
            InputStream is = response.getEntity().getContent();
            DocumentBuilder builder = DocumentBuilderFactory.newInstance()
                    .newDocumentBuilder();
            Document doc = builder.parse(is);
            if (doc != null) {
                NodeList nl;
                ArrayList args = new ArrayList();
                for (String s : tag) {
                    nl = doc.getElementsByTagName(s);
                    if (nl.getLength() > 0) {
                        Node node = nl.item(nl.getLength() - 1);
                        args.add(node.getTextContent());
                    } else {
                        args.add(" - ");
                    }
                }
                result_in_kms = String.format("%s", args.get(0));
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result_in_kms;
    }

Refernce : Get the distance between two locations in android?

Community
  • 1
  • 1
Rajan Kali
  • 12,627
  • 3
  • 25
  • 37
  • Thanks for info. Same methods I am using but wrong distance is coming.I am requesting location updates on every 10m and I am adding that distance. But that distance is wrong. – Mallikarjun P Sep 30 '15 at 11:30
  • Thanks. I need the app work without internet also. Some times GPS coordinates are coming very far from my current location. In this case distance is wrong – Mallikarjun P Sep 30 '15 at 11:59
  • @MallikarjunP it is working fine your taxi app ? now ? – Developine Jul 12 '16 at 17:32
  • I've never coded for a taxi App , but knew the way how it works ..give it a shot – Rajan Kali Jul 13 '16 at 04:10