I'm working on an app that uses osmdroid and I'm currently trying to just display the current user location on the map. I haven't set up a marker to actually mark where the current location is mainly because I don't think any of the approaches I've taken have really worked. Below is my most recent attempt taken from the following question (Question code is from). Using the debugger I can see that its having trouble firing whats inside the run() method. It just seems to skip those lines when I'm stepping through the code. Anyone have any help they could give here? Do I have to explicitly call run? I thought that the the purpose of runOnFirstFix but maybe I have it wrong. Thanks!
Main Activity:
GpsMyLocationProvider provider = new GpsMyLocationProvider(this);
provider.addLocationSource(LocationManager.NETWORK_PROVIDER);
locationOverlay = new MyLocationNewOverlay(provider, map);
locationOverlay.enableFollowLocation();
locationOverlay.runOnFirstFix(new Runnable() {
public void run() {
Log.d("MyTag", String.format("First location fix: %s", locationOverlay.getLastFix()));
mapController.animateTo(locationOverlay.getMyLocation());
}
});
map.getOverlayManager().add(locationOverlay);
map.invalidate();
}
public void onResume(){
super.onResume();
Configuration.getInstance().load(this, PreferenceManager.getDefaultSharedPreferences(this));
locationOverlay.enableMyLocation();
}
Android Manifest:
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature
android:name="android.hardware.location.network"
android:required="false" />
<uses-feature
android:name="android.hardware.location.gps"
android:required="false" />
<uses-feature
android:name="android.hardware.telephony"
android:required="false" />
<uses-feature
android:name="android.hardware.wifi"
android:required="false" />
I ended up solving this problem a few weeks ago. I wasn't properly getting permissions from the device. As in I wasn't explicitly asking the device for permission to use location. I'll post the code I used to fix the issue here.
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION)!= PackageManager.PERMISSION_GRANTED) {
// Check Permissions Now
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
} else {
Log.d("Location", "Location Permissions granted");
locationListener = new MyLocationListener();
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
}
I probably pieced this together from the community on Stack Overflow so thanks to anyone that helped me with answers on other questions!