我是Android新手,我正在开发一个显示用户当前位置的Android应用程序。我正在使用Genymotion。现在我正在使用在Android中查找用户的当前位置
mLocation=LocationServices.FusedLocationApi.getLastLocation(mGoogleClient);
获取设备的最后位置。事情是我想获得当前的位置,但与此我得到mlocation
不为null这很好。但在地图中显示设备的最后位置始终不是当前位置。
代码段
@Override
public void onConnected(Bundle bundle) {
Log.i(TAG, "inside onconnected Location services connected");
Location mLocation=null;
mLocationRequest= new LocationRequest();
mLocationRequest.setInterval(10 * 1000) ;
mLocationRequest.setFastestInterval(1 * 1000);mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED &&
ActivityCompat.checkSelfPermission(this,
Manifest.permission.ACCESS_COARSE_LOCATION) !=
PackageManager.PERMISSION_GRANTED) {
}
mLocation=LocationServices.FusedLocationApi.getLastLocation(mGoogleClient);
if(mLocation==null)
{
Log.d("***Inside mlocation***", "***mlocation is null here");
}
if (mLocation != null) {
onLocationChanged(mLocation);
}
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleClient, mLocationRequest, this);
}
@Override
public void onLocationChanged(Location location) {
double currentLatitude = location.getLatitude();
double currentLongitude = location.getLongitude();
LatLng latLng = new LatLng(currentLatitude,currentLongitude);
MarkerOptions options = new MarkerOptions()
.position(latLng)
.title("Im here!");
mMap.addMarker(options);
mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
}
和onstart()
我称为googleapi.connect()
和予适当地使用setUpMapIfNeeded()
。
请告诉我这段代码有什么问题。我正在尝试这个3天。
在代码中没有提及GPS打开/关闭。 你应该在app forground打开gps。 –
嗨杰伊,我的模拟器中的gps已打开,但是我从您的声明中了解到的是我需要检查gps的可用性是否正确。但后来它也没有改变我的想法。 – Navi
查看我的回答@Navi –