-1

I want to implement setLocationSource() to handle the battery High of use, i tried from the doc and search for samples here, but i get NullPointerException on

mMap.setLocationSource(this);

what am i missing..

I have long code..this is just related code i display..not all code.

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,
        GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener,
        LocationSource,
        LocationListener {
and so on...

..

private OnLocationChangedListener mMapLocationListener;
private GoogleMap mMap;

...

    @Override
        public void onMapReady(final GoogleMap googleMap) {
            if (mGoogleApiClient == null) {
                buildGoogleApiClient();
            }
            mMap.setLocationSource(this);
            mMap = googleMap;
and so on....

...

private static final LocationRequest REQUEST = LocationRequest.create()
            .setInterval(INTERVAL)
            .setFastestInterval(8000)
            .setSmallestDisplacement(SMALLEST_DISPLACEMENT)
            .setPriority(LocationRequest.PRIORITY_BALANCED_POWER_ACCURACY);

...

@Override
    public void activate(OnLocationChangedListener onLocationChangedListener) {
        mMapLocationListener = onLocationChangedListener;
    }

    @Override
    public void deactivate() {
        mMapLocationListener = null;
    }

...

@Override
    public void onConnected(@Nullable Bundle bundle) {
        isOnline();
        if (mGoogleApiClient == null) {
            buildGoogleApiClient();
        }
        if (mGoogleApiClient.isConnected()) {
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, REQUEST, this);
        }else{
            Toast.makeText(this, "Koneksi ke Google API Terganggu, Coba lagi.", Toast.LENGTH_SHORT).show();
        }
        Toast.makeText(this, "Terhubung dari Google Play Services.", Toast.LENGTH_SHORT).show();
    }

...

@Override
    public void onLocationChanged(Location location) {
        if (mGoogleApiClient == null) {
            buildGoogleApiClient();
        }
        if (mMapLocationListener != null) {
            mMapLocationListener.onLocationChanged(location);
        }
        latLng = new LatLng(location.getLatitude(), location.getLongitude());
and so on...
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
stuck
  • 57
  • 8

1 Answers1

1

Change your code to

@Override
        public void onMapReady(final GoogleMap googleMap) {
            if (mGoogleApiClient == null) {
                buildGoogleApiClient();
            }
            mMap = googleMap;
            mMap.setLocationSource(this);
and so on....
Muhammad Umar
  • 11,391
  • 21
  • 91
  • 193
  • holy mother.... i must have tired, i did not notice such that error..haha.. thank you so much, i need to sleep from long hours of code.. it works. – stuck Jul 27 '18 at 08:20