Skip to content Skip to sidebar Skip to footer

Android Location Listener Is Not Working

I would like to get current device location and open Google Maps with this: if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManage

Solution 1:

"Why..."

Because requestLocationUpdates() is an asynchorous operation and the result (the location) is returned in the onLocationChanged() callback. The location isn't available immediately.

"...and how to fix it ?"

Move your Google map intent code there:

@OverridepublicvoidonLocationChanged(Location loc) {

    longitude = loc.getLongitude();
    latitude = loc.getLatitude();

    Intentintent=newIntent(android.content.Intent.ACTION_VIEW, Uri.parse("http://maps.google.com/maps?saddr=" + latitude + "," + longitude + "&daddr=55.877526, 26.533898"));
    startActivity(intent);

}

Post a Comment for "Android Location Listener Is Not Working"