Android onMapReady()方法在构建GoogleAppClient之前调用-获取LatLong 0.0
您好,我在5月份获得LatLong应用程序,如下所示:Android onMapReady()方法在构建GoogleAppClient之前调用-获取LatLong 0.0,android,google-maps,location,android-mapview,latitude-longitude,Android,Google Maps,Location,Android Mapview,Latitude Longitude,您好,我在5月份获得LatLong应用程序,如下所示: private void loadDefault(GoogleMap googleMap) { try { if (Prefrences.checkPref(mContext, NEAR_ME_SEARCH)) googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(Double.parseDouble(Prefren
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);
}
}
在my fragment中创建了一个用于构建GoogleAppClient的方法:
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);
}
}
但是,我在loadDefault()方法的下面一行得到了mCurrentLaltitude和mcurrentlatude0.0
请提供解决方案。谢谢
编辑:
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);
}
调用
onMapReady
并加载映射时,不确定是否连接了google api客户端,因为这两个操作是异步执行的。因此,您必须添加一个逻辑,以便先完成的逻辑等待另一个逻辑结束,然后在地图中显示坐标。谁在调用方法buildGoogleAppClient()
?我猜这个方法要么根本没有被调用,要么在onMapReady()
之后被调用。在我使用Android版谷歌地图时,如果我需要在方向更改或重新加载过程中保持位置,我会将位置存储在实例状态。@TimBiegeleisen,你能提供获取lat lngs的正确方法吗(可以在整个课程中使用)在上述情况下,初始化地图的速度会更快吗?我不能给出确切的答案,因为我不知道你想做什么。当活动加载时加载值,然后将它们存储到实例状态,至少我是这样做的。如果是,您的解决方案可能会有所不同,例如,从首选项或SQLite数据库获取它们。您好编辑:添加了初始化()方法,我正在调用BuildGoogleAppClient()方法…是的,这就是我要问的。。有什么解决方案吗?对不起,我不能提供一个副本过去的解决方案。一个不那么优雅的解决方案是在地图加载时保留一个参考,当你从GoogleAPI客户端获取坐标时,检查mMap=null,如果为真,则继续进行映射操作。相应地,当您的onMapReady被调用时,检查您的mCurrentLatitude、mCurrentLatitude!=0,如果为真,则继续