我已经读过一些关于此的问题,但我找不到我需要的答案.
所以,情况是我设置了我的地图,我想获取当前的gps位置.我已经检查过我的变量不是NULL但是我的结果来自:
getLastKnownLocation(provider, false);
Run Code Online (Sandbox Code Playgroud)
虽然给了我null,所以这是我需要帮助的地方.我已经为COARSE + FINE位置添加了权限.但我通常会为手机禁用各种网络数据,因为我对手机账单中不可预测的数据流费用感到不满.所以我只启用了WiFi并连接了此测试.
还有什么我需要启用才能实现这一目标吗?我认为WiFi应该足够了吗?
我想在Android应用程序中找到我当前位置的纬度和经度.当我用手机稳定时,我只需要它.
我的代码是:
LocationManager locationManager;
locationManager = (LocationManager)getSystemService
(Context.LOCATION_SERVICE);
Location location = locationManager.getLastKnownLocation
(LocationManager.GPS_PROVIDER);
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,0,0,(LocationListener) this);
updateWithNewLocation(location);
}
private void updateWithNewLocation(Location location) {
TextView myLocationText = (TextView)findViewById(R.id.myLocationText);
String latLongString;
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
latLongString = "Lat:" + lat + "\nLong:" + lng;
} else {
latLongString = "No location found";
}
myLocationText.setText("Your Current Position is:\n" +
latLongString);
}
Run Code Online (Sandbox Code Playgroud)
在模拟器上运行后,我总是找到"找不到位置".为什么会这样?