osmdroid overlay to center on current location
Asked Answered
S

1

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.

Saxhorn answered 4/2, 2016 at 20:34 Comment(6)
Just get current location and set in to the map.Johanna
I'm asking about including a button as an overlay on the map itself that would fire that action though.Saxhorn
Why not add the button as a View above the map? Anyway it should be static.Johanna
Yes, I know I could add some random button and put it in the activity and execute the code to center the map. I'm asking about doing it as an overlay. It would integrate nicely, like the compass and zoom buttons.Saxhorn
I'm having the same problem. Did you find a solution for this?After
No. You may want to star / upvote the question though.Saxhorn
D
5

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.

Deberadeberry answered 6/11, 2019 at 10:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.