0

NOTE: I DO NOT need to monitor the user location continuously, just perform the check once off on a button click

I have a Set of Latitude and Longitude, and need to check if the user is within given distance of that point, how can I do it? I checked out Geofencing, but it works like a service, while I need to perform the check once in the main thread

Ayush Gupta
  • 8,716
  • 8
  • 59
  • 92

2 Answers2

1

just check with it.

for a location having latitude and longitude set boundaries with the below code:

public LatLngBounds getLocationBounds(LatLng sourceLocation, double distance) 
{
  LatLng southwest = SphericalUtil.computeOffset(sourceLocation, distance, 225);
   LatLng northeast = SphericalUtil.computeOffset(sourceLocation, distance, 45);
    return new LatLngBounds(southwest, northeast);
}
sanjana
  • 220
  • 1
  • 15
0

This is the method used to calculate the distance between two points:

public double CalculationByDistance(LatLng StartP, LatLng EndP) {
        int Radius = 6371;// radius of earth in Km
        double lat1 = StartP.latitude;
        double lat2 = EndP.latitude;
        double lon1 = StartP.longitude;
        double lon2 = EndP.longitude;
        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.asin(Math.sqrt(a));
        double valueResult = Radius * c;
        double km = valueResult / 1;
        DecimalFormat newFormat = new DecimalFormat("####");
        int kmInDec = Integer.valueOf(newFormat.format(km));
        double meter = valueResult % 1000;
        int meterInDec = Integer.valueOf(newFormat.format(meter));
        Log.i("Radius Value", "" + valueResult + "   KM  " + kmInDec
                + " Meter   " + meterInDec);

        return Radius * c;
    }

This is also a duplicate of this question: Find distance between two points on map using Google Map API V2

Please try to see if your question has been answered before asking in future.

Community
  • 1
  • 1
  • I am not using a map actually, just a static check.. maybe thats why I didnt stumble upon that question – Ayush Gupta Dec 01 '16 at 08:40
  • No problem, this will still give you the functionality you require. Just call this method and pass your Lat/Lng values, then check the values returned from this method against whatever distance you require and proceed accordingly. – JamesSwinton Dec 01 '16 at 08:42