首页 文章

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

提问于
浏览
0

我看过很多帖子和答案,但似乎都没有解决我的具体问题,但如果我错过了一个,我会提前道歉 .
我是'm building a small app that stores a phones current longitude and latitude as doubles. This is done via 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();
}

我已经将一组经度和纬度坐标硬编码到应用程序中,但如果我的用户位于硬编码点的特定米径范围内,则需要一种方法来计算 .

我'm using just the 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

    就像@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);
    

相关问题