Search code examples
androidgoogle-mapsgoogle-maps-api-3geolocationandroid-gps

Finding out if a phone's current longitude and latitude is within the radius of a gps location


I'm hoping someone can help with this. I've seen many posts and answers but none of them seem to address my specific problem, but if I've missed one, I apologise in advance.
I'm building a small app that stores a phones current longitude and latitude as doubles. This is done via android's LocationManager

    @TargetApi(23)
private void initLocationService(Context context) {
    if (Build.VERSION.SDK_INT >= 23 &&
            ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
            ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }

    try {
        this.longitude = 0.0;
        this.latitude = 0.0;
        this.locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);

        // Get GPS and network status
        this.isGPSEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
        this.isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

        if (forceNetwork) isGPSEnabled = false;

        if (!isNetworkEnabled && !isGPSEnabled) {
            // cannot get location
            this.locationServiceAvailable = false;
        }
        else
        {
            this.locationServiceAvailable = true;

            if (isNetworkEnabled) {
                locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                if (locationManager != null) {
                    location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    updateCoordinates();
                }
            }//end if

            if (isGPSEnabled) {
                locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);

                if (locationManager != null) {
                    location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
                    updateCoordinates();
                }
            }
        }
    } catch (Exception ex) {

    }
}

public void updateCoordinates()
{
    longitude = location.getLongitude();
    latitude = location.getLatitude();
}

I've got a set of longitude and latitude coordinates hardcoded into the app, but need a way to work out if my user is within a specific metre radius of a hardcoded point.

I'm using just the Android API's for the project and can't deviate from that.

I've already found out how to convert the longitude and latitude from degrees to metres:

public double longOffset(int offsetX, double lon){
    double longoff = offsetX / earthRadius;

    return lon + longoff * 180 / Math.PI;
}

public double latOffset(int offsetY, double lat){
    double latoff = offsetY / (earthRadius * cos(Math.PI*lat/180));

    return lat + latoff * 180 / Math.PI;
}

But I admit to being stumped on the rest. I'm wondering if creating a small bounding box around the longitude and latitude and then using a method similar to the one shown here: http://www.sanfoundry.com/java-program-check-whether-given-point-lies-given-polygon/

I'm getting to the point now where i'm going crosseyed so any help would be appreciated.


Solution

  • Like @EugenPechanec says, you can use SphericalUtil.computeDistanceBetween() from Google Maps Android API utility library . Something like this:

    Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    double longitude = location.getLongitude();
    double latitude = location.getLatitude();
    LatLng positionLatLng = new LatLng(latitude, longitude);
    
    // sample hardcoded point.
    LatLng specificPointLatLang = new LatLng(1.333434, 1.3333);
    
    // Returns the distance between two LatLngs, in meters.
    double distance = SphericalUtil.computeDistanceBetween(positionLatLng, specificPointLatLang);