I'm messing with the LocationClient
in combination with Google Maps V2
, because I wanna integrate it in my app and outdate the LocationListener, LocationSource combination
.
One requirement
of the app I'm working on is to grab the Location
provided from the LocationListener, modify
that Location and pass it to Maps
via LocationSource.
This is working but I don't know how to manage this using LocationClient. Because it seems that it automatically provide the Location to Maps and there is no point to grab the Location before where I can modify it.
Do I miss something or is that a fact?
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);
}