How to find the correct distance between two geotomes on a map?

I need to develop an application where the user needs to find his car, which he parked, and show the distance between him and the parking lot. I used GPS and location services.

For the distance, I used the haversine formula , but the distance always shows 0 meters.

I searched a lot on google for a solution, but did not find the right solution.

Can anyone give their suggestions?

+5
source share
6 answers

Google Docs have two methods.

enter image description here

If you get lat / lon from GeoPoint, then they are located in microdeggregs. You must multiply by 1e6.

. ( )

http://www.codecodex.com/wiki/Calculate_Distance_Between_Two_Points_on_a_Globe

double dist = GeoUtils.distanceKm(mylat, mylon, lat, lon);

 /**
 * Computes the distance in kilometers between two points on Earth.
 * 
 * @param lat1 Latitude of the first point
 * @param lon1 Longitude of the first point
 * @param lat2 Latitude of the second point
 * @param lon2 Longitude of the second point
 * @return Distance between the two points in kilometers.
 */

public static double distanceKm(double lat1, double lon1, double lat2, double lon2) {
    int EARTH_RADIUS_KM = 6371;
    double lat1Rad = Math.toRadians(lat1);
    double lat2Rad = Math.toRadians(lat2);
    double deltaLonRad = Math.toRadians(lon2 - lon1);

    return Math.acos(Math.sin(lat1Rad) * Math.sin(lat2Rad) + Math.cos(lat1Rad) * Math.cos(lat2Rad) * Math.cos(deltaLonRad)) * EARTH_RADIUS_KM;
}

, .

, ,

http://code.google.com/p/j2memaprouteprovider/

+12

API android.location

distanceBetween (double startLatitude, double startLongitude, double endLatitude, double endLongitude, float [] results)

, ,

NB: lat/lon GeoPoint, . 1E6

2-

public class DistanceCalculator {
   // earth’s radius = 6,371km
   private static final double EARTH_RADIUS = 6371 ;
   public static double distanceCalcByHaversine(GeoPoint startP, GeoPoint endP) {
      double lat1 = startP.getLatitudeE6()/1E6;
      double lat2 = endP.getLatitudeE6()/1E6;
      double lon1 = startP.getLongitudeE6()/1E6;
      double lon2 = endP.getLongitudeE6()/1E6;
      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.atan2(Math.sqrt(a), Math.sqrt(1-a));
      return EARTH_RADIUS * c;
   }
}
+3

distanceBetween() . , . ansewer

+3

android.location.Location.distanceBetween(double startLatitude, double startLongitude, double endLatitude, double endLongitude, float [] results)

Geopoints getLongitudeE6() getLatitudeE6(), . , E6, 1E6.

+1

, . 2 . . , Google-Api . Googlemaps Api api.

+1

distanceBetween ( ) google, . 2 Android Blackberry.

0

All Articles