无法获取设备位置

时间:2019-01-23 10:04:15

标签: java android location

我需要在主要活动开始时获取设备的当前位置,因此我使用了位置管理器和所有这些东西。但是我无法获得设备的当前位置,我也不知道为什么。在这里,我发布清单权限和我的代码。 (整个堆栈跟踪很长)

清单

<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" />

代码

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

1 个答案:

答案 0 :(得分:0)

在用户权限检查之后,使用

之类的fusedLocationProvider
 public FusedLocationProviderClient mFusedLocationProvider;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    //In onCreate define the provider like this
    mFusedLocationProvider =LocationServices.getFusedLocationProviderClient(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
            }

    }
});