简体   繁体   中英

Google Maps Android Api v2 distanceBetween method innacurate

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM