Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MyLocationNewOverlay.getLocation() is null and runOnFirstFix not executed #1303

Closed
bmalex88 opened this Issue Mar 28, 2019 · 5 comments

Comments

Projects
None yet
2 participants
@bmalex88
Copy link

commented Mar 28, 2019

Issue Type

[ ] Question
[x] Bug
[ ] Improvement
[ ] Build system related
[ ] Performance
[ ] Documentation

Description and/or steps/code to reproduce the problem

I have a straightforward method that is called after the users grants location access permissions from the main activity's onPermissionsGranted() method. Problem is, not only the location is not retrieved (which could also be caused by latency) but the Runnable specified as argument for runOnFirstFix is never run either.
Location is enabled and permissions are all alright. I don't find any reason for which this should not work or at least try to run.
I am running this on Android Pie (9) and compileSdkVersion is 28.

private void getCurrentLocation() {
    LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    String bestProvider = locationManager.getBestProvider(criteria, true);

    Timber.i("bestProvider %s", bestProvider);

    GpsMyLocationProvider provider = new GpsMyLocationProvider(this.getBaseContext());
    provider.addLocationSource(LocationManager.GPS_PROVIDER);
    provider.addLocationSource(LocationManager.NETWORK_PROVIDER);
    provider.setLocationUpdateMinDistance(100);
    provider.setLocationUpdateMinTime(10000);

    myLocationOverlay = new MyLocationNewOverlay(provider, mMapView);
    if (!myLocationOverlay.enableMyLocation()) {
        Timber.i("Location not enabled");
    }

    myLocationOverlay.setDrawAccuracyEnabled(true);
    Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_target);
    myLocationOverlay.setPersonIcon(icon);

    myLocationOverlay.runOnFirstFix(new Runnable() {
        @Override
        public void run() {
            // never reaches this point
            Timber.i("runOnFirstFix");
            if (myLocationOverlay.getMyLocation() == null) {
                Timber.i("Location nott retrieved");
            }
        }
    });

    mMapView.getOverlays().add(myLocationOverlay);

    if (myLocationOverlay.getMyLocation() == null) {
        // could be too early
        Timber.i("Location not retrieved");
    }
}

Environment

If it's a bug, version(s) of android this affects:

All

Version of osmdroid the issue relates to:

6.1.0

@bmalex88

This comment has been minimized.

Copy link
Author

commented Mar 29, 2019

I have tried with the default LocationListener and it works. I can get the location

        // Acquire a reference to the system Location Manager
        LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

        // Define a listener that responds to location updates
        LocationListener locationListener = new LocationListener() {
            public void onLocationChanged(Location location) {
                // Called when a new location is found by the network location provider.
                Timber.i("Location: %f", location.getLatitude());
            }

            public void onStatusChanged(String provider, int status, Bundle extras) {
                Timber.i("Location status: %s %d", provider, status);
            }

            public void onProviderEnabled(String provider) {
                Timber.i("Provider enabled %s", provider);
            }

            public void onProviderDisabled(String provider) {
                Timber.i("Provider disabled %s", provider);
            }
        };

        // Register the listener with the Location Manager to receive location updates
        if (ContextCompat.checkSelfPermission( this, android.Manifest.permission.ACCESS_COARSE_LOCATION ) == PackageManager.PERMISSION_GRANTED ) {
            Timber.i("Location access granted");
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, 100, locationListener);
        }
@spyhunter99

This comment has been minimized.

Copy link
Collaborator

commented Mar 30, 2019

did this work correctly for you on the previous version of osmdroid? as a reminder, the runtime permission must be granted before creating the mylocation overlay

@bmalex88

This comment has been minimized.

Copy link
Author

commented Mar 30, 2019

No, it did not work

spyhunter99 added a commit that referenced this issue Apr 4, 2019

@spyhunter99

This comment has been minimized.

Copy link
Collaborator

commented Apr 4, 2019

i just tested it too and added some sample code to one of the sample fragments. Both reported issues work provided permissions are granted. Whatever see you have, it may be device specific issue. What version of android?

@bmalex88

This comment has been minimized.

Copy link
Author

commented Apr 10, 2019

I have the Pie version. I am rewriting the entire application an will dedicate to this issue separately. If this happens again, I will create another issue with specific details. This can be closed. Thanks

@bmalex88 bmalex88 closed this Apr 10, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.