Current location button to navigate back to the current location

Skip to first unread message


Apr 23, 2020, 3:53:16 PM4/23/20
to osmdroid
I have enabled my current location in OSM and it moves to my current location successfully as the app starts but there is no button to navigate back to the current location in case I swipe the map to some different location as the Google map does. Only zoom buttons are visible (screenshot is given below). Thanks
Code I am using

mLocationOverlay = new MyLocationNewOverlay(new GpsMyLocationProvider(this), map);
IMapController mapController = map.getController();


Diego Ramírez

Apr 24, 2020, 11:45:21 PM4/24/20
OK you already should have Location permissions, and a LocationManager in your app (not seeing in your code).

You will need a method handling all those permissions checks and using the location manager to get the current location, once you get
this location  you will need to pass this to a LocationListener

private void centerToCurrentLocation() {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "centerToCurrentLocation: COARSE LOCATION ON");
if (mLocationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)) {
.requestSingleUpdate(LocationManager.NETWORK_PROVIDER, mLocationListener, null);
} else {
Log.w(TAG, "centerToCurrentLocation: There is no NETWORK_PROVIDER");

} else {
Log.d(TAG, "centerToCurrentLocation: COARSE LOCATION OFF");

if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)
== PackageManager.PERMISSION_GRANTED) {
Log.d(TAG, "centerToCurrentLocation: FINE LOCATION ON");
Log.d(TAG, "centerToCurrentLocation: GPS ENABLED");
if (mLocationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
mLocationManager.requestSingleUpdate(LocationManager.GPS_PROVIDER, mLocationListener, null);
} else {
Log.w(TAG, "centerToCurrentLocation: There is no GPS_PROVIDER");
} else {
Log.d(TAG, "centerToCurrentLocation: FINE LOCATION OFF");

Here you see that mLocationListener, this is responsible of doing something with you current location

private final LocationListener mLocationListener = new LocationListener() {
public void onLocationChanged(final Location location) {
Log.d(TAG, "onLocationChanged: Provider = " + location.getProvider());
Log.d(TAG, "onLocationChanged: accuracy = " + location.getAccuracy());
"onLocationChanged: lon = " + location.getLongitude() + " lat = " + location.getLatitude()
+ " alt = " + location.getAltitude());
Bundle extras = location.getExtras();
for (String s : extras.keySet()) {
Log.d(TAG, "onLocationChanged: " + s + " = " + extras.get(s));
mController.setCenter(new GeoPoint(location));

public void onStatusChanged(String provider, int status, Bundle extras) {
Log.d(TAG, "onStatusChanged: " + provider + " status = " + status);
for (String s : extras.keySet()) {
Log.d(TAG, "onStatusChanged: " + s + " = " + extras.get(s));

public void onProviderEnabled(String provider) {
Log.d(TAG, "onProviderEnabled: " + provider);

public void onProviderDisabled(String provider) {
Log.d(TAG, "onProviderDisabled: " + provider);

This is all good and all fine, but you will need something on the screen like a button to trigger this, could be in onCreate like this FloatingActionButton.
Always refer to docs and if somenthing fails to provide a little bit more of code of what you are trying.

FloatingActionButton fab = findViewById(;
fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {

You received this message because you are subscribed to the Google Groups "osmdroid" group.
To unsubscribe from this group and stop receiving emails from it, send an email to
To view this discussion on the web visit
Reply all
Reply to author
0 new messages