【发布时间】: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/
我现在已经到了眼花缭乱的地步,所以任何帮助都将不胜感激。
【问题讨论】:
-
您在寻找 SphericalUtil.computeDistanceBetween 吗? github.com/googlemaps/android-maps-utils/blob/master/library/…
-
听起来你需要地理围栏。查看文档:developer.android.com/training/location/geofencing.html
标签: android google-maps google-maps-api-3 geolocation android-gps