查明 phone 的当前经度和纬度是否在 gps 位置的半径范围内

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

我希望有人可以帮助解决这个问题。 我看过很多帖子和答案,但其中 none 似乎解决了我的具体问题,但如果我错过了一个,我提前道歉。
我正在构建一个小应用程序,将手机当前的经度和纬度存储为双精度。这是通过 android 的 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();
}

我已经将一组经度和纬度坐标硬编码到应用程序中,但需要一种方法来计算我的用户是否在硬编码点的特定米半径范围内。

我在项目中仅使用 Android API,并且不能偏离它。

我已经找到了如何将经度和纬度从度数转换为米数:

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;
}

但我承认被其他人难住了。 我想知道是否在经度和纬度周围创建一个小边界框,然后使用类似于此处显示的方法: http://www.sanfoundry.com/java-program-check-whether-given-point-lies-given-polygon/

我现在已经到了我要斜视的地步,所以任何帮助将不胜感激。

就像@EugenPechanec 说的,你可以使用 SphericalUtil.computeDistanceBetween() from Google Maps Android API utility library 。像这样:

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);