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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With