你好我得到經緯度的,可以按以下應用程序:onMapReady()方法調用構建GoogleApiClient前 - 獲取的LatLong 0.0
創建於我的片段的方法來構建GoogleApiClient:
private synchronized void buildGoogleApiClient() {
MyApp.getGoogleApiHelper().setConnectionListener(new GoogleApiHelper.ConnectionListener() {
@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
}
@Override
public void onConnectionSuspended(int i) {
}
@Override
public void onConnected(Bundle bundle) {
//this function will call whenever google api connected or already connected when setting listener
//You are connected do what ever you want
//Like i get last known location
try {
if (!Constant.checkPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION)) {
Constant.requestPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION, PLACE_PICKER_REQUEST);
} else {
if (Constant.isOnline(mContext)) {
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
mCurrentLatitude = mLastLocation.getLatitude();
mCurrentLongitude = mLastLocation.getLongitude();
locationUpdate();
} else {
Constant.displayToast(mContext, getResources().getString(R.string.msg_internet));
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
OnMapReady( )方法如下:調用loadDefault()方法加載當前位置。
public void onMapReady(final GoogleMap googleMap) {
loadDefault(googleMap);
}
我loadDefault()方法如下:
private void loadDefault(GoogleMap googleMap) {
try {
if (Prefrences.checkPref(mContext, NEAR_ME_SEARCH))
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Double.parseDouble(Prefrences.getPref(mContext, NEAR_ME_LAT)), Double.parseDouble(Prefrences.getPref(mContext, NEAR_ME_LNG))), 8));
else
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mCurrentLatitude, mCurrentLongitude), 8f));
} catch (MalformedParameterizedTypeException e) {
handleException(mContext, e);
} catch (Exception e) {
handleException(mContext, e);
}
}
但是,下面的線我得到mCurrentLaltitude和mCurrentLongitude 0.0在裏面loadDefault()方法。
googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mCurrentLatitude, mCurrentLongitude), 8f));
請提供解決方案。謝謝。
編輯:
private void initialization(View rootView, Bundle savedInstanceState) {
try {
mModelBarList = BarListModel.getBarListInstance();
if (!Constant.checkPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION)) {
Constant.requestPermission(mContext, Manifest.permission.ACCESS_FINE_LOCATION, PLACE_PICKER_REQUEST);
} else {
if (Constant.isOnline(mContext)) {
mGoogleApiClient = MyApp.getGoogleApiHelper().getGoogleApiClient();
buildGoogleApiClient();
} else {
Constant.displayToast(mContext, getResources().getString(R.string.msg_internet));
}
}
mMapView = (MapView) rootView.findViewById(R.id.map);
mMapView.onCreate(savedInstanceState);
mMapView.onResume();
mMapView.getMapAsync(MapFragment.this);
}
誰在調用'buildGoogleApiClient()'方法?我的猜測是,這個方法要麼根本不被調用,要麼在'onMapReady()'之後被調用。在我使用Google Maps for Android時,如果我需要在定位更改或重新加載時保留它,我會將我的位置存儲爲實例狀態。 –
@TimBiegeleisen,你能否提供正確的方法來獲取經緯度(可以在整個課程中使用)並在上述條件下更快地初始化地圖? – Krantiz
我不能給出確切的答案,因爲我不知道你在做什麼。在活動加載時加載值,然後將它們存儲到實例狀態中,至少這是我所做的。如果您的解決方案可能會有所不同,例如,從首選項或從SQLite數據庫獲取它們,可能會有所不同。 –