LocationClient
works similarly to the original Android Location API in that it takes in a LocationListener as an argument to listen for location updates:
http://developer.android.com/reference/com/google/android/gms/location/LocationClient.html
The LocationListener.onLocationChanged(Location location)
method then gets called by the platform with new locations.
Therefore, you can use the same strategy for passing location updates to the Maps API v2 via the LocationSource
and LocationSource.OnLocationChangedListener
to control what locations get shown on the map.
First, declare an OnLocationChangedListener
object in your Activity
:
private OnLocationChangedListener mListener; //Used to update the map with new location
Then, implement LocationSource for your activity, something like:
public class MapScreen extends FragmentActivity implements LocationSource{
In onCreate()
, set up the LocationSource
for this Activity
when you're setting up the Map object:
...
//Show the location on the map
mMap.setMyLocationEnabled(true);
//Set location source
mMap.setLocationSource(this);
...
Then, add the methods required for the LocationSource
interface:
/**
* Maps V2 Location updates
*/
@Override
public void activate(OnLocationChangedListener listener) {
mListener = listener;
}
/**
* Maps V2 Location updates
*/
@Override
public void deactivate() {
mListener = null;
}
The final part is passing in the location updates from a normal LocationListener
to the Activity
implementing the LocationSource
:
//Update real-time location on map
if(mListener != null){
mListener.onLocationChanged(location);
}