Java 获取Android中的最后一个位置
我按照本教程检索设备的位置,但它不起作用 当我这样做时:Java 获取Android中的最后一个位置,java,android,google-api,location,Java,Android,Google Api,Location,我按照本教程检索设备的位置,但它不起作用 当我这样做时: mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient); “mLastLocation”为空,为什么 编辑 我试着在尼鲁的安萨雷那样做: public Location setLocation() { Location location; final int MIN_TIME_BW_UP
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
“mLastLocation”为空,为什么
编辑
我试着在尼鲁的安萨雷那样做:
public Location setLocation()
{
Location location;
final int MIN_TIME_BW_UPDATES = 300;
final int MIN_DISTANCE_CHANGE_FOR_UPDATES = 50;
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
String bestProvider = locationManager.getBestProvider(criteria, true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
{
if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && checkSelfPermission(Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {}
}
location = locationManager.getLastKnownLocation(bestProvider);
localStorage.putFloat("latitude", (float) location.getLatitude());
localStorage.putFloat("longitude", (float) location.getLongitude());
localStorage.commit();
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, MIN_TIME_BW_UPDATES, MIN_DISTANCE_CHANGE_FOR_UPDATES, (LocationListener) this);
return location;
}
@Override
public void onLocationChanged(Location location)
{
localStorage.putFloat("latitude", (float) location.getLatitude());
localStorage.putFloat("longitude", (float) location.getLongitude());
localStorage.commit();
}
但是“location”为空,为什么?getLastKnownLocation()返回最后一个已知位置…重新安装应用程序后,它将没有任何最后一个已知位置…因此将导致NPE…而使用下面的代码
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(LOCATION_SERVICE);
// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);
// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
if (!isGPSEnabled && !isNetworkEnabled) {
// no network provider is enabled
} 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 Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS", "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();
}
return location;
您可以尝试以下代码段:
public class LocationProvider
{
private static final String TAG = "DEBUG";
private static final String[] provider = new String[]
{
LocationManager.GPS_PROVIDER,
LocationManager.NETWORK_PROVIDER,
LocationManager.PASSIVE_PROVIDER
};
public Location getLocationByProvider(Context context)
{
Location location = null;
// LocationManager locationManager = (LocationManager) context.getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
LocationManager locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
try
{
for (int i = 0; i < provider.length; i++)
{
if (locationManager.isProviderEnabled(provider[i]))
{
// locationManager.requestLocationUpdates(provider[i], 0, 0, locationListenerGps);
location = locationManager.getLastKnownLocation(provider[i]);
if (location != null)
{
break;
}
}
}
}
catch (IllegalArgumentException e)
{
Log.d(TAG, "Cannot acces Provider " + provider);
}
return location;
}
}
希望这对您有所帮助。:-) 文档本身说,当位置不可用时,getLastLocation可能返回null。 如果为null,则在OnConnected()方法中调用requestLocationUpdate方法,如下所示:
@Override
public void onConnected(Bundle connectionHint) {
mCurrentLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
if (mCurrentLocation == null) {
LocationServices.FusedLocationApi.requestLocationUpdates(
mGoogleApiClient, mLocationRequest, this);
}
}
在调用RequestLocationUpdate之前,请创建mLocationRequest对象:
mLocationRequest = new LocationRequest();
mLocationRequest.setInterval(10000);
mLocationRequest.setFastestInterval(5000);
mLocationRequest.setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);
当位置可用时,OnLocationChanged将自动触发。获取此回调的速率由创建请求对象时放入setInterval()的时间间隔定义
@Override
public void onLocationChanged(Location location) {
mCurrentLocation = location;
}
然后将更新您的位置对象
请点击此链接:多亏了这篇文章,我解决了类似的问题: 事实上,如果您使用
compileSdkVersion 23
(或更高版本),如果您希望应用程序在android 6.0+设备上正确运行,您必须处理新的android权限系统。。。更多信息可在此处找到:
测试应用程序的一个快速而肮脏的解决方法是将build.gradle中的compileSdkVersion
和targetSdkVersion
更改为22
然而,我认为这是愚蠢的,没有明确的错误信息,但只有一个空…可能重复相同的问题,在安卓6.0上的Nexus 6p和安卓6.0.1上的Nexus 5,但它在5.1.1上的Nexus 4上运行良好,所以可能是平台问题。。。你使用哪个版本的Android?冷静,祝你有一个愉快的一天:-)
@Override
public void onLocationChanged(Location location) {
mCurrentLocation = location;
}