cgeek
cgeek

Reputation: 578

LocationServices.FusedLocationApi.requestLocationUpdates is never getting called

Instead of LocationManager, I have use LocationServices method to get current gps location and location updates.

this is the code:

public void onConnected(@Nullable Bundle bundle) {
    mLocationRequest = new LocationRequest();//PRIORITY_BALANCED_POWER_ACCURACY
    mLocationRequest.setInterval(1000*30*1);
    mLocationRequest.setFastestInterval(1000*30*1);
    mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.ACCESS_FINE_LOCATION)
            == PackageManager.PERMISSION_GRANTED) {
        LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest,this);
    }
}
public void onLocationChanged(Location location) { mLastLocation = location;
        if (mCurrLocationMarker != null)
        {
            mCurrLocationMarker.remove();
        }
    //Place current location marker
     latLng = new LatLng(location.getLatitude(), location.getLongitude());
     lat= location.getLatitude();
     log=location.getLongitude();
     trim_check_location_change(lat,log);
     MarkerOptions markerOptions = new MarkerOptions();
     markerOptions.position(latLng);
     markerOptions.title(""+s);
     markerOptions.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));
     mCurrLocationMarker = mMap.addMarker(markerOptions);
     //move map camera
     mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));
     CurrentLocation = latLng;

}

As I move from from one place to another, blue marker moves accordingly. however onlocationchanged method never gets called.

Upvotes: 1

Views: 236

Answers (1)

User
User

Reputation: 4211

I've developed fused location api demo application and utility pack here.

General Utilities

Try it if useful for you. To get location using fused location api, you just have to write following snippet...

new LocationHandler(this)
    .setLocationListener(new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {
        // Get the best known location
    }
}).start();

And if you want to customise it, simply find documentation here...

https://github.com/abhishek-tm/general-utilities-android/wiki/Location-Handler

Update

I've written a sample code according to your need, try this one...

import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;

import com.google.android.gms.location.LocationListener;
import com.google.android.gms.location.LocationRequest;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;

import in.teramatrix.utilities.service.LocationHandler;
import in.teramatrix.utilities.util.MapUtils;

/**
 * Lets see how to use utilities module by implementing location listener.
 *
 * @author Khan
 */

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback, LocationListener {

    private GoogleMap map;
    private Marker marker;
    private LocationHandler locationHandler;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Obtaining an instance of map
        FragmentManager manager = getSupportFragmentManager();
        SupportMapFragment mapFragment = (SupportMapFragment) manager.findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

        this.locationHandler = new LocationHandler(this)
                .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)
                .setInterval(5000)
                .setFastestInterval(10000)
                .setLocationListener(this);
    }

    @Override
    public void onMapReady(GoogleMap map) {
        this.map = map;
        this.locationHandler.start();
    }

    @Override
    public void onLocationChanged(Location location) {
        LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());
        if (marker == null) {
            marker = MapUtils.addMarker(map, latLng, R.drawable.ic_current_location);
            map.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 14), 500, null);
        } else {
            marker.setPosition(latLng);
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (locationHandler != null) {
            locationHandler.stop();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == LocationHandler.REQUEST_LOCATION) {
            locationHandler.start();
        }
    }
}

Hope it will help you.

Upvotes: 1

Related Questions