简体   繁体   中英

Can't get device position

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");
           }
       }
   }

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
            }

    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM