Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to find the correct distance between two geopoints in map?

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?

like image 855
9 revs Avatar asked Jul 02 '12 13:07

9 revs


Video Answer


1 Answers

Google Docs have two methods

enter image description here

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/

like image 50
Vipul Avatar answered Sep 23 '22 20:09

Vipul