如何仅使用GPS获取经度和纬度

时间:2019-02-12 10:37:43

标签: android android-gps

如何仅通过GPS而不使用任何类型的Internet连接获取位置。我使用下面的代码来获取位置,但没有互联网就无法工作。请让我知道为什么没有互联网我无法获得位置

        boolean isGPSEnabled = manager
                .isProviderEnabled(LocationManager.GPS_PROVIDER);
       boolean isNetworkEnabled = manager
                .isProviderEnabled(LocationManager.NETWORK_PROVIDER);
        if (!isGPSEnabled && !isNetworkEnabled) {
            // no network provider is enabled
        } else {
            // First get location from Network Provider
            if (isNetworkEnabled) {

                if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                }
                manager.requestLocationUpdates(
                        LocationManager.NETWORK_PROVIDER,
                        MIN_TIME_BW_UPDATES,
                        MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                Log.d("Network", "Network");
                if (manager != null) {
                    location = manager
                            .getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
                    if (location != null) {
                        currentLatitude = location.getLatitude();
                        currentLongitude = location.getLongitude();
                    }
                }
            }
            // if GPS Enabled get lat/long using GPS Services
            if (isGPSEnabled) {
                if (location == null) {
                    manager.requestLocationUpdates(
                            LocationManager.GPS_PROVIDER,
                            MIN_TIME_BW_UPDATES,
                            MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
                    Log.d("GPS Enabled", "GPS Enabled");
                    if (manager != null) {
                        location = manager
                                .getLastKnownLocation(LocationManager.GPS_PROVIDER);
                        if (location != null) {
                            currentLatitude = location.getLatitude();
                            currentLongitude = location.getLongitude();
                        }
                    }
                }
            }
            Log.d(TAG, "getLocation: "+currentLatitude+"   "+currentLongitude);
        }

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

2 个答案:

答案 0 :(得分:1)

首先,您粘贴的代码范围是著名的,它是网络和GPS提供商最近的已知位置的组合。即使在getLastKnownLocation方法之后才调用requestLocationUpdates方法,也完全不信任这种用法。因为这些传感器不会以同步方式给出结果。

您有两种选择,一种是调用getLastKnownLocation来获取上一次获得的GPS位置,第二种是使用requestLocationUpdates进行位置请求。选择此项取决于您的情况。

答案 1 :(得分:0)

使用此短代码仅使用GPS即可获取纬度和经度

LocationManager manager = (LocationManager) getSystemService(LOCATION_SERVICE);
            if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                return;
            }
            Location location = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
            Log.d("Location = ","Longitude "+location.getLongitude()+",Latitude "+location.getLatitude());