Search code examples
androidlocationproximity

Best way to instantly check proximity to one of two locations in Android?


I'm writing an app that utilizes parse for cloud data storage. I recently was told that the people I'm writing this for are opening a second location, and would like to know which location a user is checking in to/recording data at. I've found the google code at: http://developer.android.com/training/location/geofencing.html

that defines how to use geofencing, as well as how to get the last known gps/network location.
http://developer.android.com/guide/topics/location/strategies.html

But I can't tell if either one of these are what I need. Basically I'm trying to do the following:

  1. User starts app
  2. User clicks "Check In" to check in to a class at the gym
  3. When the "check in" activity loads, the user can click a class. At this point in time I'd like to know if the user is say...within 5 miles of a central lat/long. The two locations are around 20 miles apart, so a rough estimate putting them even within a few miles should be fine (are they in Sunnyvale or San Jose?)
  4. When user clicks a class, the check-in gets sent to a Parse database, along with the location (either one city or the other)

I'm not sure if geofencing is proper for this case, as i just need to know rough area from a point..but the basic location getting sample doesn't seem to show how to check when the phone is near a point.

To this point, I've found the following: http://www.javacodegeeks.com/2011/01/android-proximity-alerts-tutorial.html

This seems to be the best option, except it constantly checks for "distance" from the set point, and I need to tell once if the user is near one or the other locations.


Solution

  • Something like this maybe:

    Location me   = new Location("");
    Location dest = new Location("");
    
    me.setLatitude(myLat);
    me.setLongitude(myLong);
    
    dest.setLatitude(destLat);
    dest.setLongitude(destLong);
    
    float dist = me.distanceTo(dest);
    

    You can then repeat for the second location and store both distances then compare to see the smallest.