3

I am looking to figure out how I can replicate the follow current location feature that exists in Maps on Android (and iPhone) using the Google Maps external library. The way the feature works in Maps is that when you press the button it begins panning with your location until you touch (and/or move) the map. Using the following I have been able to get my app to keep the current location on the screen but not keep the current location in the center of the screen (it pans when the location gets near the edge of the screen whereas Maps keeps it in the center the whole time):

butMapCurrentLocation.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        runOnUiThread(new Runnable() {
            public void run() {
                mapController.animateTo(myLocationOverlay.getMyLocation());
            }
        });
    }
});

I also tried setCenter rather than animateTo and they seem to have the same effect.

Additionally it is worth noting I need to be able to stop this function that keeps the location in the center (such as when another button is pressed or the map is touched). The Runnable above doesn't stop right away and I cannot get it to stop properly when requested even when using .cancel as discussed here: Android: How do I stop Runnable?

Community
  • 1
  • 1
bbodenmiller
  • 3,101
  • 5
  • 34
  • 50
  • Also worth noting, once I stop the map from panning I still want it to show the user their location if they are looking at that part of the map. – bbodenmiller May 13 '12 at 03:59

1 Answers1

1

There is no official way yet to do that in Google Maps v2.

You can do what you want however. There is two things you have to do:

  1. Register for location updates from some LocationProviders (eg gps) to get the user's location. When you get the location animate map to the new location.
  2. Stop this when the user moved the map.

So doing 1. is easy, doing 2. is not possible officially with the Maps v2 api. Look at this issue for more details.

The workaround suggested there is to create your own MapView (extend the original MapView) and override it's dispatchTouchEvent method. There you can catch any touch events and can detect if the user touched/moved the map.

balazsbalazs
  • 4,071
  • 1
  • 24
  • 29