简体   繁体   English

无法获取设备位置

[英]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 用户权限检查后,请使用fusedLocationProvider之类的

 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
            }

    }
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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