【问题标题】:Finding out if a phone's current longitude and latitude is within the radius of a gps location查明手机当前的经度和纬度是否在 GPS 位置的半径范围内
【发布时间】:2018-09-02 16:15:41
【问题描述】:

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

我现在已经到了眼花缭乱的地步,所以任何帮助都将不胜感激。

【问题讨论】:

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


【解决方案1】:

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

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

【讨论】:

  • 抱歉回复晚了。我的笔记本电脑坏了,我几乎失去了整个项目。备份至关重要。值得庆幸的是,吸取了教训,把它整理好了,你的答案很有效。谢谢你:)
  • 有时坏事会发生,让我们意识到需要正确地做一些事情。我们有经验并从同样的坏事中吸取教训。加油;)
猜你喜欢
  • 1970-01-01
  • 2021-09-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多