0

I need to take the current position of the device at the beginning of the main activity, so I used location manager and all this stuff. But I can't get the current position of the device and I don't know why. Here I post the manifest permission and my code. (the full stacktrace is so long)

MANIFEST

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission
    android:name="com.example.mapexdemo.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />
<uses-feature android:name="android.hardware.location.gps" />
<uses-feature android:name="android.hardware.location.network" />

CODE

ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
   if(!locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
       Toast.makeText(MainActivity.this, "NO GPS", Toast.LENGTH_LONG).show();
       System.out.print("OknO");
   } else if (locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
       System.out.print("OK0");
       if(ActivityCompat.checkSelfPermission(MainActivity.this, Manifest.permission.ACCESS_FINE_LOCATION)
               != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission
               (MainActivity.this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED){
           ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, REQUEST_LOCATION);
           System.out.print("ok01");
       } else {
           Location location = locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

           Location location1 = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);

           Location location2 = locationManager.getLastKnownLocation(LocationManager. PASSIVE_PROVIDER);

           System.out.print("ok011");

           if (location != null) {
               double latti = location.getLatitude();
               double longi = location.getLongitude();
               lattitude = String.valueOf(latti);
               longitude = String.valueOf(longi);
               Toast.makeText(MainActivity.this, "Your current location is"+ lattitude + longitude, Toast.LENGTH_SHORT).show();
               System.out.print("OK1");

       } else if(location1 != null){
               double latti = location1.getLatitude();
               double longi = location1.getLongitude();
               lattitude = String.valueOf(latti);
               longitude = String.valueOf(longi);
               System.out.print("OK2");
               Toast.makeText(MainActivity.this, "Your current location is"+ lattitude + longitude, Toast.LENGTH_SHORT).show();
           } else if (location2 != null){
               double latti = location2.getLatitude();
               double longi = location2.getLongitude();
               lattitude = String.valueOf(latti);
               longitude = String.valueOf(longi);
               Toast.makeText(MainActivity.this, "Your current location is"+ lattitude + longitude, Toast.LENGTH_SHORT).show();
                System.out.print("OK3");
           } else {
               Toast.makeText(MainActivity.this, "Unable to trace your location", Toast.LENGTH_SHORT).show();
                System.out.print("OK4");
           }
       }
   }
Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
  • The `locationManager.getLastKnownLocation()` call returns `null` if the device location isn't known. And it isn't necessarily known if no applications (including yours) have recently requested location updates. Even if it returns a location, it might not be up to date. – Markus Kauppinen Jan 23 '19 at 10:10
  • Where does it fail? Is it failing on the permissions or are you simply not getting any data in your Locations? – Jesper Purup Jan 23 '19 at 10:11
  • @JesperPurup If I check latitude and longitude they are null – Andrea Di Martino Jan 23 '19 at 10:33
  • @MarkusKauppinen sorry but I did not understand your comment well – Andrea Di Martino Jan 23 '19 at 10:34
  • Possible duplicate of [Android getlastknownlocation returns null](https://stackoverflow.com/questions/10689372/android-getlastknownlocation-returns-null) – Markus Kauppinen Jan 23 '19 at 10:43
  • As @MarkusKauppinen said .getLastKnwonLocation() CAN return null in some cases. Try having a look at https://developer.android.com/training/location/retrieve-current#java – Jesper Purup Jan 23 '19 at 10:44
  • There's some more explanation: [https://stackoverflow.com/questions/36747349/getlastknownlocation-return-null-using-gps-provider-and-network-provider/36752030#36752030](https://stackoverflow.com/questions/36747349/getlastknownlocation-return-null-using-gps-provider-and-network-provider/36752030#36752030). This is actually a very common confusion with getLastKnownLocation(). You'll just need to understand how an Android device handles location updates. In short: an up-to-date location isn't automatically maintained all the time and you can request location updates yourself when you need them. – Markus Kauppinen Jan 23 '19 at 10:45
  • okok thank you, i'll see this stackoverflow question – Andrea Di Martino Jan 23 '19 at 10:49

1 Answers1

0

After user permission check use fusedLocationProvider like

 public FusedLocationProviderClient mFusedLocationProvider;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //In onCreate define the provider like this
    mFusedLocationProvider =LocationServices.getFusedLocationProviderClient(this);
}

// then in your listener or anywhere you want to get the location use the provider like this

mFusedLocationProvider.getLastLocation().addOnSuccessListener(new OnSuccessListener<Location>() {
    @Override
    public void onSuccess(Location location) {
    // Got last known location. In some rare situations this can be null.
            if (location != null) {
                // Logic to handle location object
                // here you can handler the location location.getLatitude &   location.getLongitude
            }

    }
});
Ahmed Karam
  • 386
  • 1
  • 7
  • Thanks but iI can't solve the problem. I need the position once when the fragments starts. I need a clever solution because i find only solution of tracking position – Andrea Di Martino Jan 23 '19 at 16:55