Get the distance between two geo points

I want to make a apps that check what’s the nearest place from where the user is. I can easily get the location of the user and I have a list of places with latitude and longitude.

What would be the best way to know the nearest place of the list against the current position.

  • App crashes after first Facebook Login
  • Getting street name from Address/Location object in Android
  • Design pattern for fragments in android
  • Can I exclude regional resources (e.g. values-fr) when building a particular Android Product Flavor with gradle
  • android: how do i open another app from my app?
  • Can one use cardview for listview item and how
  • I could not find anything in the google APIs.

    I am worried I need to resort to my calculate and have to do math to calculate it.

    What do you guys think ?

    Cheers and thanks for reading or replying.

    Related posts:

    Starting app only if its not currently running
    Font size too large to fit in cache
    How to save and reuse same instance of fragments?
    How to turn on camera flash light programmatically in Android?
    With default resources in the drawable folder (MDPI sized) what's the point of the drawable...
    how can i determine internet speed in my android application
  • Android return object as a activity result
  • Android - Is it possible to create a custom library to use across several applications?
  • C# on Android: Xamarin or Unity?
  • Android mkdirs() creates a zero byte file instead of a folder
  • Whatsapp Message Layout - How to get time-view in the same row
  • Remove Contact select option form file select options
  • 6 Solutions collect form web for “Get the distance between two geo points”

    Location loc1 = new Location("");
    loc1.setLatitude(lat1);
    loc1.setLongitude(lon1);
    
    Location loc2 = new Location("");
    loc2.setLatitude(lat2);
    loc2.setLongitude(lon2);
    
    float distanceInMeters = loc1.distanceTo(loc2);
    

    Reference: http://developer.android.com/reference/android/location/Location.html#distanceTo(android.location.Location)

    http://developer.android.com/reference/android/location/Location.html

    Look into distanceTo or distanceBetween. You can create a Location object from a latitude and longitude:

    Location location = new Location("");
    location.setLatitude(lat);
    location.setLongitude(lon);
    

    An approximated solution (based on an equirectangular projection), much faster (it requires only 1 trig and 1 square root).

    This approximation is relevant if your points are not too far apart. It will always over-estimate compared to the real haversine distance. For example it will add no more than 0.05382 % to the real distance if the delta latitude or longitude between your two points does not exceed 4 decimal degrees.

    The standard formula (Haversine) is the exact one (that is, it works for any couple of longitude/latitude on earth) but is much slower as it needs 7 trigonometric and 2 square roots. If your couple of points are not too far apart, and absolute precision is not paramount, you can use this approximate version (Equirectangular), which is much faster as it uses only one trigonometric and one square root.

    // Approximate Equirectangular -- works if (lat1,lon1) ~ (lat2,lon2)
    int R = 6371; // km
    double x = (lon2 - lon1) * Math.cos((lat1 + lat2) / 2);
    double y = (lat2 - lat1);
    double distance = Math.sqrt(x * x + y * y) * R;
    

    You can optimize this further by either:

    1. Removing the square root if you simply compare the distance to another (in that case compare both squared distance);
    2. Factoring-out the cosine if you compute the distance from one master point to many others (in that case you do the equirectangular projection centered on the master point, so you can compute the cosine once for all comparisons).

    For more info see: http://www.movable-type.co.uk/scripts/latlong.html

    There is a nice reference implementation of the Haversine formula in several languages at: http://www.codecodex.com/wiki/Calculate_Distance_Between_Two_Points_on_a_Globe

    There are a couple of methods you could use, but to determine which one is best we first need to know if you are aware of the user’s altitude, as well as the altitude of the other points?

    Depending on the level of accuracy you are after, you could look into either the Haversine or Vincenty formulae…

    These pages detail the formulae, and, for the less mathematically inclined also provide an explanation of how to implement them in script!

    Haversine Formula: http://www.movable-type.co.uk/scripts/latlong.html

    Vincenty Formula: http://www.movable-type.co.uk/scripts/latlong-vincenty.html

    If you have any problems with any of the meanings in the formulae, just comment and I’ll do my best to answer them 🙂

    There are two ways to get distance between LatLng.

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

    See this

    and second

    public float distanceTo (Location dest) as answered by praveen.

    Just use the following method, pass it lat and long and get distance in meter:

    private static double distance_in_meter(final double lat1, final double lon1, final double lat2, final double lon2) {
        double R = 6371000f; // Radius of the earth in m
        double dLat = (lat1 - lat2) * Math.PI / 180f;
        double dLon = (lon1 - lon2) * Math.PI / 180f;
        double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
                Math.cos(latlong1.latitude * Math.PI / 180f) * Math.cos(latlong2.latitude * Math.PI / 180f) *
                        Math.sin(dLon/2) * Math.sin(dLon/2);
        double c = 2f * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
        double d = R * c;
        return d;
    }
    
    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.