如何在 android 7 中获取当前位置?
How to get current location in android 7?
我正在尝试获取 android 中的当前位置,当我在 android M 或更低版本上使用时它没问题,但是当我 运行 我的应用程序在 android N,位置为空,当我想从中获取纬度时,它将 returns 0。这是我的代码
try {
if (!isGPSEnabled && !isNetworkEnabled) {
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
and its OK when i'm using on android M or lower
不,不是。 getLastKnownLocation()
在 Android.
的所有版本上经常 returns null
getLastKnownLocation()
是一种优化。使用它,但要准备好在 getLastKnownLocation()
returns null
或过时值时依赖 onLocationChanged()
。
这在 the documentation 以及有关 Android 应用程序开发的书籍和课程中都有介绍。
要获取最后已知位置,您只需致电
mLocationClient.getLastLocation();
连接定位服务后。
阅读如何使用新位置API
http://developer.android.com/training/location/retrieve-current.html#GetLocation
我正在尝试获取 android 中的当前位置,当我在 android M 或更低版本上使用时它没问题,但是当我 运行 我的应用程序在 android N,位置为空,当我想从中获取纬度时,它将 returns 0。这是我的代码
try {
if (!isGPSEnabled && !isNetworkEnabled) {
} else {
this.canGetLocation = true;
if (isNetworkEnabled) {
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
and its OK when i'm using on android M or lower
不,不是。 getLastKnownLocation()
在 Android.
null
getLastKnownLocation()
是一种优化。使用它,但要准备好在 getLastKnownLocation()
returns null
或过时值时依赖 onLocationChanged()
。
这在 the documentation 以及有关 Android 应用程序开发的书籍和课程中都有介绍。
要获取最后已知位置,您只需致电
mLocationClient.getLastLocation(); 连接定位服务后。
阅读如何使用新位置API
http://developer.android.com/training/location/retrieve-current.html#GetLocation