how to find the correct distance between two geopo

2019-03-16 15:26发布

问题:

I need to develop app where user has to locate his car that he has parked and show distance between him and car parked.I used GPS and location services.

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

I tried a lot searching for solution in google but dint get any correct solution.

Can anyone give their suggestions?

回答1:

Google Docs have two methods

If you are getting lat/lon from GeoPoint then they are in microdegrees. You must multiply by 1e6.

But i preferred to use below method. (its based on Haversine Formula)

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;
}

At last i would like to share bonus information.

If you are looking for driving directions, route between two locations then head to

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



回答2:

distanceBetween() method will give you straight distance between two points. Got get route distance between two points see my ansewer Here



回答3:

Try using This method in android.location API

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

This method computes the approximate distance in meters between two locations, and optionally the initial and final bearings of the shortest path between them

NB: If you are getting lat/lon from GeoPoint then they are in microdegrees. You must multiply by 1E6

IF you want to calculate distace between 2 Geopoint by Haversine formula

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;
   }
}


回答4:

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

Geopoints have getLongitudeE6() and getLatitudeE6() to help. Remember that those are E6 so you need to divide by 1E6.



回答5:

The problem with the harvesine formula is that it doesn't calculate the real distance. It's the distance from 2 points on a sphere. The real distance depends on the streets, or the water ways. The harvesine formula is also a bit complicated hence it is easier to ask Google-Api to give the real distance. With Googlemaps Api you need to learn the directions api.



回答6:

distanceBetween does not reffer to the real distance (the road distance) So i suggest to you to visit this google source code and it will dispaly you the real road distance between 2 geopoints. Link there is 2 versions one for Android and one for blackberry check it out