无法找到我的设备

时间:2016-12-25 14:27:41

标签: android gps geolocation

位置有问题...我正在使用方法获取纬度和经度。但我的Location变量为null。所以我的坐标是0,0。 有人可以帮助我吗? 这是代码:

   public Location getLocation() {

        try {
            locationManager = (LocationManager) mContext
                    .getSystemService(LOCATION_SERVICE);

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

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

            if (!checkGPS && !checkNetwork) {
                Toast.makeText(mContext, R.string.no_service, Toast.LENGTH_SHORT).show();
            } else {
                this.canGetLocation = true;
                // First get location from Network Provider
                if (checkNetwork) {

                    try {
                        locationManager.requestLocationUpdates(
                                LocationManager.NETWORK_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        if (locationManager != null) {
                            loc = locationManager
                                    .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);

                        }

                        if (loc != null) {
                            latitude = loc.getLatitude();
                            longitude = loc.getLongitude();
                        }
                    } catch (SecurityException e) {

                    }
                }
            }

            // if GPS Enabled get lat/long using GPS Services
            if (checkGPS) {
                if (loc == null) {
                    try {
                        locationManager.requestLocationUpdates(
                                LocationManager.GPS_PROVIDER,
                                MIN_TIME_BW_UPDATES,
                                MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                        if (locationManager != null) {
                            loc = locationManager
                                    .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                            if (loc != null) {
                                latitude = loc.getLatitude();
                                longitude = loc.getLongitude();
                            }
                        }
                    } catch (SecurityException e) {

                    }
                }
            }

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

        return loc;
    }

loc无论如何都是null。当我正在编译应用程序时,我的手机上启用了GPS和wifi。

1 个答案:

答案 0 :(得分:0)

鉴于您正在吞下SecurityExceptions,我猜测您的应用没有请求permissionACCESS_COARSE_LOCATIONACCESS_FINE_LOCATION,这是网络和GPS定位所需要的,分别

我一般建议不要悄悄吞咽异常。这些特殊的SecurityExceptions,你应该永远不需要为它们添加一个catch语句(这是你的错误的线索),b / c你应该在尝试访问资源之前调用checkSelfPermission()。 / p>