找出手机当前的经度和纬度是否在gps位置的半径范围内

时间:2018-03-23 22:09:12

标签: android google-maps google-maps-api-3 geolocation android-gps

我希望有人可以帮忙解决这个问题。 我看过很多帖子和答案,但似乎都没有解决我的具体问题,但如果我错过了一个,我会提前道歉。
我正在构建一个小型应用程序,将当前经度和纬度存储为双倍电话。这是通过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/

我已经到了现在我要去交叉的地步所以任何帮助都会受到赞赏。

1 个答案:

答案 0 :(得分:0)

与@EugenPechanec一样,您可以使用SphericalUtil.computeDistanceBetween()中的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);