我按照本教程http://developer.android.com/training/location/retrieve-current.html检索了设备的位置,但它无法正常工作。
当我这样做时:
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
" mLastLocation"是空的,为什么?
修改
我试图在Niru的回答中这样做:
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"是空的,为什么?
答案 0 :(得分:1)
您可以尝试以下代码段:
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;
}
}
您还可以获得最佳提供商,然后从最佳提供商处获取位置。
mLocationManager = (LocationManager) getApplicationContext().getSystemService(Context.LOCATION_SERVICE);
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
criteria.setPowerRequirement(Criteria.POWER_LOW);
bestProvider = mLocationManager.getBestProvider(criteria, true);
希望这会对你有所帮助。 : - )
答案 1 :(得分:0)
www.web1.com
这将返回最后一个已知位置...重新安装应用程序后,它将没有任何最后已知的位置...因此它将导致NPE ...而是使用下面的代码< / p>
<form action="http://www.web1.com/" method="post" name="form1" >
<table width="326" cellpadding="3" cellspacing="0">
<tr>
<td style="padding-left:7px; padding-right:7px; width:1px; border-right:1px solid #1e639e;">
<img src="logo1.png" height="32px" border="0"/>
</td>
<td style="padding-left:7px; width:1px; white-space:nowrap;">
<input name="textfield" type="text" class="input_text" placeholder="Username"/>
<input name="textfield2" type="password" class="input_text" placeholder="Password"/>
</td>
<td>
<img src="http://www.web1.com/app/autonum.php" align="absmiddle">
</td>
<td>
<input name="Authnum" type="text" size="4" maxlength="4" class="za_text" style=" width: 56px; ">
</td>
<td width="68"><input name="login" type="submit" class="but_submit" id="login" value="LOGIN"/></td>
<!--<td width="65">
<a href="register.php" class="iframe">
<div class="but_join" style="width:60px;">JOIN NOW</div>
</a>
</td>-->
</tr>
</table>
</form>
答案 2 :(得分:0)
文档本身表示,如果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);
}
}
在调用requestLocationUpdates之前,创建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;
}
您的位置对象将会更新。
请点击此链接:http://developer.android.com/training/location/receive-location-updates.html
答案 3 :(得分:0)
由于这篇文章我解决了类似的问题:Permission issues for location in android Marshmallow applicaton。
事实上,如果您使用compileSdkVersion 23
(或更高版本),则必须处理新的Android权限系统,如果您希望您的应用程序在Android 6.0+设备上正确运行...可以在此处找到更多信息: http://developer.android.com/training/permissions/index.html
用于测试应用的一种快速而肮脏的解决方法可能是将build.gradle中的compileSdkVersion
和targetSdkVersion
更改为22 ...
但是,我认为没有关于此的明确错误消息而只是一个空的......这是愚蠢的......