0

to the point. I am using Google Maps Android Api v2 distanceBetween method to grab distance between user location and some point on the map as you can see here:

float[] distance = new float[1];
Location currentLocation = PECApplication.getInstance().getLocationClient().getLastLocation();
Location.distanceBetween(currentLocation.getLatitude(), currentLocation.getLongitude(), marker.getPosition().latitude, marker.getPosition().longitude, distance);
TextView distanceView = (TextView) infoWindow.findViewById(R.id.eventDistanceValue);
distanceView.setText(formatDistance(distance[0]));

The problem is that the result don't match the result I receive from google maps website. In one of the cases the distance of Google Maps Website was 31km and the result from distanceBetween method was 23,328 KM. Someone have a clue? Thanks alot.

Marcos J.C Kichel
  • 6,887
  • 8
  • 38
  • 78
  • You might consider posting the actual latitude and longitude values that gave you these results. Also, `Location` has nothing to do with Maps V2. – CommonsWare Jul 19 '13 at 20:26
  • 1
    Just an educated guess: Google Maps will give you the length of travel over roads, footpaths etc, whereas the `distanceBetween()` method just calculates the distance between two points 'as the crow flies' (or more technically correct: the 'displacement'). See also: [distanceBetween() returns inaccurate result?](http://stackoverflow.com/questions/14618016/distancebetween-returns-inaccurate-result) – MH. Jul 20 '13 at 04:35

1 Answers1

1

MH is right. If you wanna get the travel distance along a road you have to use the web-service Google Directions. Therefor you have to do a http request with a start and an end point of your choice. The response is a JSON with detailed information about the route between your points, like the actual distance, turn by turn instructions and many more.

See this question for some details about the code.

Community
  • 1
  • 1
Steve Benett
  • 12,843
  • 7
  • 59
  • 79