我是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 –