0

I have implemented a way to retrieve the current location from Google Maps and it worked well.

But it does not work suddenly.

so I checked the code back into the past, but it still does not work.

I find what functions did not work, and I noticed that onLocationChanged() was not called.

I do not know what the problem is because no error message is displayed in the log.

This is the relevant code.

the code in a fragment with Google Maps

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    previous_marker = new ArrayList<>();
    getActivity().getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    final ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.activity_main, container, false);
    ButterKnife.bind(this, rootView);

    mActivity = getActivity();
    mcontext = getContext();
    Initialize();

    mGoogleApiClient = new GoogleApiClient.Builder(mActivity)
            .enableAutoManage(mActivity, 1, this)
            .addConnectionCallbacks(this)
            .addApi(Places.GEO_DATA_API)
            .addApi(Places.PLACE_DETECTION_API)
            .addApi(LocationServices.API)
            .build();

    mapFragment = (SupportMapFragment) getChildFragmentManager().findFragmentById(R.id.googleMap);
    mapFragment.getMapAsync(this);

    locationRequest = LocationRequest.create();
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    locationRequest.setInterval(UPDATE_INTERVAL_MS);
    locationRequest.setFastestInterval(FASTEST_UPDATE_INTERVAL_MS);

other codes

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    switch (requestCode) {
        case GPS_ENABLE_REQUEST_CODE:
            if (checkLocationServicesStatus()) {
                if (checkLocationServicesStatus()) {
                    if ( mGoogleApiClient.isConnected() == false ) {
                        Log.d( TAG, "onActivityResult : mGoogleApiClient connect ");
                        mGoogleApiClient.connect();
                    }
                    return;
                }
            }
            break;


 @Override
public void onLocationChanged(Location location) {
    setCurrentLocation(location);
    currentPosition = new LatLng(location.getLatitude(), location.getLongitude());
    saveLat = location.getLatitude();
    saveLng = location.getLongitude();

    chooseLat  = location.getLatitude();
    chooseLng = location.getLongitude();
    district = getDistrict(mcontext, location.getLatitude(), location.getLongitude());
    getToken();
    setDefaultPlace(location.getLatitude(), location.getLongitude());
}

@Override
public void onResume() {
    super.onResume();
    if (mGoogleApiClient.isConnected()) {
        if (!mRequestingLocationUpdates) startLocationUpdates();
    }
    if (askPermissionOnceAgain) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            askPermissionOnceAgain = false;
            checkPermissions();
        }
    }
}

private void startLocationUpdates() {
    if (!checkLocationServicesStatus()) {
        Log.d(TAG, "startLocationUpdates : call showDialogForLocationServiceSetting");
        showDialogForLocationServiceSetting();
    }else {
        if (ActivityCompat.checkSelfPermission(mcontext, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED
                && ActivityCompat.checkSelfPermission(mcontext, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            return;
        }
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, locationRequest, this);
        mRequestingLocationUpdates = true;
        mGoogleMap.setMyLocationEnabled(false);
    }
}

public boolean checkLocationServicesStatus() {
    locationManager = (LocationManager) mActivity.getSystemService(LOCATION_SERVICE);

    return locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)
            || locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
}


@Override
public void onStart() {
    if(mGoogleApiClient != null && mGoogleApiClient.isConnected() == false){
        Log.d(TAG, "onStart: mGoogleApiClient connect");
        mGoogleApiClient.connect();
    }
    if(!firstLoad){
        mRequestingLocationUpdates = true;
    }
    super.onStart();
}

@Override
public void onStop() {
    firstLoad = false;
    if (mRequestingLocationUpdates) {
        Log.d(TAG, "onStop : call stopLocationUpdates");
        stopLocationUpdates();
    }
    if ( mGoogleApiClient.isConnected()) {
        Log.d(TAG, "onStop : mGoogleApiClient disconnect");
        mGoogleApiClient.disconnect();
    }
    super.onStop();
}

@Override
public void onConnected(Bundle connectionHint) {
    if ( mRequestingLocationUpdates == false ) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            int hasFineLocationPermission = ContextCompat.checkSelfPermission(mcontext,
                    android.Manifest.permission.ACCESS_FINE_LOCATION);
            if (hasFineLocationPermission == PackageManager.PERMISSION_DENIED) {
                ActivityCompat.requestPermissions(mActivity,
                        new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                        PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
            } else {
                Log.d(TAG, "onConnected : call startLocationUpdates");
                startLocationUpdates();
                mGoogleMap.setMyLocationEnabled(true);
            }

        } else {
            Log.d(TAG, "onConnected : call startLocationUpdates");
            startLocationUpdates();
            mGoogleMap.setMyLocationEnabled(true);
        }
    }
}

@Override
public void onConnectionFailed(ConnectionResult connectionResult) {
    setDefaultLocation();
}

@Override
public void onConnectionSuspended(int cause) {
    if (cause == CAUSE_NETWORK_LOST)
        Log.e(TAG, "onConnectionSuspended(): Google Play services " +
                "connection lost.  Cause: network lost.");
    else if (cause == CAUSE_SERVICE_DISCONNECTED)
        Log.e(TAG, "onConnectionSuspended():  Google Play services " +
                "connection lost.  Cause: service disconnected");
}

and this is my permission

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

If you know the reason or the wrong part of the code, I would appreciate it if you let me know.

Thank you for reading this article. Have a nice day

Jshin
  • 131
  • 2
  • 9

1 Answers1

0

check these lines there in mapready method

public void onMapReady(GoogleMap googleMap) {


            mMap = googleMap;

            mMap.setMyLocationEnabled(true);

    // Setting event handler for location change
            mMap.setOnMyLocationChangeListener(this);
}