Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

osmdroid overlay to center on current location

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.

like image 821
Stealth Rabbi Avatar asked Feb 04 '16 20:02

Stealth Rabbi


1 Answers

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.

like image 68
Prokash Sarkar Avatar answered Oct 30 '22 10:10

Prokash Sarkar