I switched to v2 of Maps for Android, and I am trying to port over the following functionality:
With MyLocationOverlay I can show the devices current location (Blue Dot). When the user's location changes and the dot reaches the edge of the visible area, the map animates out so the dot becomes the center of the view, in real time.
In v2, I am using SupportMapFragment with getMap().setMyLocationEnabled(true). The current location appears as a blue dot(arrow), but when the device changes location, the map view does not shift and the dot eventually leaves the view.
Any ideas?
Just use
OnMyLocationChangeListener
, override the methodpublic void onMyLocationChange(Location location)
. UseGoogleMap.setOnMyLocationChangeListener(your listener)
to register your listener.UPDATE: Google introduced the new
LocationClient
and associatedLocationListener
(theOnMyLocationChangeListener
interface is now deprecated). So now automatic centering of the camera is a trivial task.Although spotdog already answered the question I wanted to share my own example, which I hope will help fellow Android beginners better understand what it takes to make a custom
LocationSource
for the my-location layer. Hopefully you will find that the code is well documented/commented.You need to add a LocationSource to your GoogleMap and respond to onLocationChanged events. Here is a simple class that asks for the user's location, then waits until the user's location is available and animates the map to center on their location.
This should follow the user and keep centering the map on their location as it changes - if you'd like to only center the map on the user when they go "off screen", then you could check to see if the user's location is within the visible bounds of the map
Here's a short write up on the topic: Google Maps Android API V2 MyLocation LocationSource and event handling
UPDATE
I edited how I'm obtaining the user's location (see locationManager code in onCreate). I've found that using the "getBestProvider" methods are not reliable and just plain don't work on several devices. I had several users complain that their devices would never find their location, no matter how relaxed I made the criteria. It seems that manually picking GPS or Network works universally.