0

我正在尝试使用LocationManager教程检索位置。

我正在运行 Marshmallow 的 android 设备上运行代码,并且我已经检查了权限并在授予权限后运行以下代码。

这是我的代码:

public Location getLocation() {
        try {
            locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);

            // getting GPS status
            isGPSEnabled = locationManager
                    .isProviderEnabled(LocationManager.GPS_PROVIDER);

            // getting network status
            isNetworkEnabled = locationManager
                    .isProviderEnabled(LocationManager.NETWORK_PROVIDER);

            if (!isGPSEnabled && !isNetworkEnabled) {
                // no network provider is enabled

                Log.d("noProviderEnabled", "called");

            } else {
                this.canGetLocation = true;
                // First get location from Network Provider
                if (isNetworkEnabled) {
                    locationManager.requestLocationUpdates(
                            LocationManager.NETWORK_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("Network", "Network");
                    if (locationManager != null) {
                        location = locationManager
                                .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                        if (location != null) {
                            currentLatDouble = location.getLatitude();
                            currentLngDouble = location.getLongitude();

                        }
                    }
                }
                // if GPS Enabled get lat/long using GPS Services
                if (isGPSEnabled) {
                    if (location == null) {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        Log.d("GPS Enabled", "GPS Enabled");
                        if (locationManager != null) {
                            location = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (location != null) {
                                currentLatDouble = location.getLatitude();
                                currentLngDouble = location.getLongitude();

                            }
                        }
                    }
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

        return location;
    }

即使 WiFi 已启用,isNetworkEnabled=locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);正在返回false并被Log.d("noProviderEnabled", "called");打印出来。

更新:当我打开GPS然后运行上面的代码时..只有isNetworkEnabled返回true并且isGPSEnabled仍然返回false

这里有什么问题?LocationManager.NETWORK_PROVIDER为什么即使启用了 WiFi,我也无法检索位置?

PS:这在 android L 及以下版本上运行良好。

4

0 回答 0