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

问题描述 投票:0回答:2

如何在没有任何互联网连接的情况下通过GPS获取位置。我使用下面的代码获取位置,但没有互联网不工作。请让我知道为什么我没有上网的地方

        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();
    }
android android-gps
2个回答
1
投票

首先,您粘贴的代码范围很有名,它是网络和GPS提供商的最后已知位置的组合。甚至在getLastKnownLocation方法之后调用requestLocationUpdatesmethods,这种用法完全不受信任。因为那些传感器不会给出同步结果。

你有两个选择,一个是调用getLastKnownLocation来获取最后获得的GPS位置,第二个是用requestLocationUpdates发出位置请求。选择此选项将取决于您的方案。


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());
© www.soinside.com 2019 - 2024. All rights reserved.