Current location button to navigate back to the current location

34 views
Skip to first unread message

Aamir

unread,
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);
mLocationOverlay
.enableFollowLocation();
mLocationOverlay
.enableMyLocation();
this.map.getOverlayManager().add(mLocationOverlay);
map
.setBuiltInZoomControls(true);
map
.setMultiTouchControls(true);
IMapController mapController = map.getController();
mapController
.setZoom(9.5);

Screenshot_20200424-004859_OpenStreetMapAndroid.jpg


Diego Ramírez

unread,
Apr 24, 2020, 11:45:21 PM4/24/20
to osmd...@googlegroups.com
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)) {
mLocationManager
.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() {
@Override
public void onLocationChanged(final Location location) {
Log.d(TAG, "onLocationChanged: Provider = " + location.getProvider());
Log.d(TAG, "onLocationChanged: accuracy = " + location.getAccuracy());
Log.d(TAG,
"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));
mController.setZoom(mZoomLevel);
}

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

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

@Override
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(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
centerToCurrentLocation();
}
});

--
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 osmdroid+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/osmdroid/677f1a78-07c5-4955-8167-ee7ee70113fe%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages