11

I'm using osmdroid and I have added a MyLocationNewOverlay to my map to show my current location. THis looks fine. What I want is a button that will center the map on to my current location, similar to Google Maps.

Is there a way to do this built in? If not, how do I go about creating a custom overlay to do this?

I realize that MyLocationNewOverlay has a feature for enabling following the location. I do not want this. I just want an on-demand center button.

Stealth Rabbi
  • 10,156
  • 22
  • 100
  • 176

1 Answers1

4

The setCenter() method along with animateTo() in mapView.controller should center the map.

val mMyLocationOverlay = MyLocationNewOverlay(GpsMyLocationProvider(this), myMapView)
                    val mapController = mapView.controller
                    mMyLocationOverlay.disableMyLocation()
                    mMyLocationOverlay.disableFollowLocation()
                    mMyLocationOverlay.isDrawAccuracyEnabled = true
                    mMyLocationOverlay.runOnFirstFix {
                        runOnUiThread {
                       mapController.setCenter(mMyLocationOverlay.myLocation);                        
                       mapController.animateTo(mMyLocationOverlay.myLocation)
                        }
                    }
            mapView.overlays.add(mMyLocationOverlay)

Regarding the overlay button, You can put a static button on top of the map. It might not be the perfect solution but it should do the trick.

Prokash Sarkar
  • 11,723
  • 1
  • 37
  • 50