0
votes

I am trying to get to last known Location with the aid of GoogleApiClient I followed the guide in the link:

https://developer.android.com/training/location/retrieve-current.html

I have tested my code below also the buildGoogleApiClient() is being invoked but not the onConnected(). Therefor, the last known Location is not being displayed.

The Google map is being displayed.

How can I get the last known Location with this Approach?

Map activity:

public class Map extends FragmentActivity implements OnMapReadyCallback, ConnectionCallbacks, OnConnectionFailedListener{
    GoogleApiClient mGoogleApiClient;
    Location mLastLocation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        buildGoogleApiClient();
}

    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
         System.out.println("ABC buildGoogleApiClient map was invoked: ");
    }
    @Override
    public void onConnected(Bundle arg0) {
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        if (mLastLocation != null) {
             double lng = mLastLocation.getLongitude();
             double lat = mLastLocation.getLatitude();

            if(myLocatMarker != null){
                myLocatMarker.remove();
            }
             LatLng ll = new LatLng(lat, lng);
             MarkerOptions markerOpt = new MarkerOptions().title("my location")
                     .position(ll)
                     .icon(BitmapDescriptorFactory.fromResource(R.drawable.myloc));
             System.out.println("ABC onConnected map: "+ lat + " ; " + lng);
             myLocatMarker = map.addMarker(markerOpt);                

        }

    }
}

Edit map activity:

public class Map extends FragmentActivity implements OnMapReadyCallback, ConnectionCallbacks, OnConnectionFailedListener{ GoogleMap map; GoogleApiClient mGoogleApiClient; Location mLastLocation; Marker myLocatMarker;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);
        buildGoogleApiClient();

}
    @Override
public void onMapReady(GoogleMap arg0) {
            mGoogleApiClient.connect(); 
    System.out.println("ABC onMapReady");
}
private boolean initMap() {
    if (map == null) {
        SupportMapFragment mapFrag = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        map = mapFrag.getMap();
    }
    return (map != null);
}
    @Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);      
    @SuppressWarnings("unchecked")
    ArrayList<ItemDTO> list = (ArrayList<ItemDTO>) intent
            .getSerializableExtra("list_data");
    for (ItemDTO itemDTO : list) {
        int id = itemDTO.getBusId();
        double latitude = itemDTO.getLatitude();
        double longitude = itemDTO.getLongitude();
        int route1 = itemDTO.getRoute();
        String route = Integer.toString(route1);
        String direction = itemDTO.getDirection();
        String route_dirc = route + ", " + direction;

        if (initMap()) {
            gotoLocation(id, latitude, longitude, route_dirc);                                              

        } else {
            Toast.makeText(this, "Map not avialable", Toast.LENGTH_SHORT)
                    .show();
        }

    }
}

    protected synchronized void buildGoogleApiClient() {
        mGoogleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
         System.out.println("ABC buildGoogleApiClient map was invoked: ");
    }
    @Override
    public void onConnected(Bundle arg0) {
        mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
                mGoogleApiClient);
        if (mLastLocation != null) {
             double lng = mLastLocation.getLongitude();
             double lat = mLastLocation.getLatitude();

            if(myLocatMarker != null){
                myLocatMarker.remove();
            }
             LatLng ll = new LatLng(lat, lng);
             MarkerOptions markerOpt = new MarkerOptions().title("my location")
                     .position(ll)
                     .icon(BitmapDescriptorFactory.fromResource(R.drawable.myloc));
             System.out.println("ABC onConnected map: "+ lat + " ; " + lng);
             myLocatMarker = map.addMarker(markerOpt);                

        }

    }
}
1

1 Answers

2
votes

After you call buildGoogleApiClient() you need to call mGoogleApiClient.connect(). This should make onConnected() get called.