0

On location_switch click User go online and check for current location.But on switch click, it is not showing my current location. I am using an emulator for checking app. Even I change location in emulator but it is not working. Once the location is checked it will store in Firebase Database which is working but My current location is not showing.

location_switch.setOnCheckedChangeListener(new MaterialAnimatedSwitch.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(boolean isOnline) {

            if (isOnline) {
                startLocationUpdates();
                displayLocation();
                Snackbar.make(mapFragment.getView(), "You are online", Snackbar.LENGTH_SHORT)
                        .show();
            } else {
                stopLocationUpdates();
                mCurrent.remove();
                Snackbar.make(mapFragment.getView(), "You are offline", Snackbar.LENGTH_SHORT)
                        .show();
            }

        }
    });
}

private void stopLocationUpdates() {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);

}

private void displayLocation() {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    mLastLocation = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);
    if (mLastLocation != null) {
        if (location_switch.isChecked()) {
            final double latitute = mLastLocation.getLatitude();
            final double longitude = mLastLocation.getLongitude();

           /*Update To FireBase*/
            geoFire.setLocation(FirebaseAuth.getInstance().getCurrentUser().getUid(), new GeoLocation(latitute, longitude), new GeoFire.CompletionListener() {
                @Override
                public void onComplete(String key, DatabaseError error) {
                   /*Add Marker*/
                    if (mCurrent != null) {
                        mCurrent.remove();
                        mCurrent = mMap.addMarker(new MarkerOptions()
                                .icon(BitmapDescriptorFactory.fromResource(R.drawable.car))
                                .position(new LatLng(latitute, longitude))
                                .title("You"));
                       /*Move camera to this postion*/
                        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(latitute, latitute), 15.0f));

                    }
                }
            });
        }
    } else {
        Log.d("ERROR", "Can Not Get Your Location: ");
    }
}



private void startLocationUpdates() {
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED &&
            ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        return;
    }
    LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);

}


@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;

}

@Override
public void onLocationChanged(Location location) {
    mLastLocation = location;
    displayLocation();

}


@Override
public void onConnected(@Nullable Bundle bundle) {
    displayLocation();
    startLocationUpdates();

}

@Override
public void onConnectionSuspended(int i) {
    mGoogleApiClient.connect();

}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {

}
}

Can anyone help me to figure out where I am doing wrong?

Piyu
  • 43
  • 2
  • 8

1 Answers1

0

You have called displayLocation in many places. So, on goOnline just call startLocationUpdate and in onLocationChanged call displayLocation. As locationUpdates wait for the next event to be fired by android system depending on ur FASTEST INTERVAL, you will not get the latest location. Also set the FASTEST INTERVAL to 1 second for instant location update. Hope this helps.

Khader
  • 16
  • 2
  • I remove displayLocation() from isOnline. And set FASTEST INTERVAL to 1 sec but it is Steel it is not showing my current location – Piyu Nov 13 '17 at 12:04
  • `W/GCoreFlp: No location to return for getLastLocation()` – Piyu Nov 13 '17 at 12:10