onMapReady() method calling before building GoogleApiClient - Getting LatLong 0.0

662 Views Asked by At

Hello I am getting LatLong in may app as below :

Created a method in my fragment to build 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() method is as below : Calling loadDefault() method to load current location.

public void onMapReady(final GoogleMap googleMap) {
    loadDefault(googleMap);
}

My loadDefault() method is as below :

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);
    }
}

But, I am getting mCurrentLaltitude and mCurrentLongitude 0.0 at below line inside loadDefault() method.

googleMap.moveCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(mCurrentLatitude, mCurrentLongitude), 8f));

please, provide solution. thanks.

EDIT :

 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);
}
1

There are 1 best solutions below

2
Antonis Lat On

When onMapReady is called and the map has been loaded, is not sure that google api client is connected, because these two operations executed asynchronously. So you have to add a logic so the ones that finishes first wait until the other is over and then show the coordinates in the map.