【发布时间】:2015-07-10 15:17:36
【问题描述】:
在我的应用程序中,我尝试使用 networkProvider 搜索 android 设备的粗略位置。我只在我的位置管理器中使用 networkProvider,但如果我不打开 GPS 传感器,它将无法工作。
无论 GPS 传感器是打开还是关闭,网络提供商都应该提供粗略的位置吗?
这是我的代码。
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
if(isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER))
Log.e("Network","suc");
else
Log.e("Network", "fail");
if (!isNetworkEnabled) {
} else {
this.isGetLocation = true;
if (isNetworkEnabled) {
Log.e("GpsInfo", "isNetworkEnabled true");
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
if (locationManager != null) {
Log.e("GpsInfoClass", "Location manager not NULL");
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
lat = location.getLatitude();
lon = location.getLongitude();
Log.e("GpsInfoClass", lon + ", " + lat );
}else{
Log.e("GpsInfoClass", "Location NULL");
}
}else{
Log.e("GpsInfo", "Location Manager is null");
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
return location;
}
在我的日志中,它打印网络(标签)失败(日志)。
这是什么意思,
locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)
如果 GPS 传感器关闭,则始终返回 false。为什么会这样?我在获取粗略位置时缺少什么?
【问题讨论】:
标签: android gps geolocation locationmanager