设置longitutde和lattitude时,我得到一个空位置。我不确定我做错了什么。我打开和关闭位置服务调试了这段代码。当它们打开时,GPS_PROVIDER if语句将为真,当它们关闭时,这两个条件都不会成立,它将转到else语句。但是,即使确认启用了提供程序,对getLastKnownLocation的调用也返回null。关于为什么的任何想法?
private void setCoordinates(Context context) {
LocationManager lm = (LocationManager) context
.getSystemService(Context.LOCATION_SERVICE);
Location location = null;
if (lm.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
longitude = location.getLongitude();
latitude = location.getLatitude();
} else if (lm.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
location = lm
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
longitude = location.getLongitude();
latitude = location.getLatitude();
} else {
Log.e("AlarmLocation", "unable to get location");
}