无法获得位置坐标?纬度经度

时间:2017-05-02 08:47:56

标签: java android

我的纬度和经度都有问题。当我想获取坐标时,只有24个API检索坐标。其他人不会。我不知道为什么

我使用GPSTracker服务

public Location getLocation() {
    try {
        locationManager = (LocationManager) mContext
                .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
        } else {
            this.canGetLocation = true;
            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) {

                }
                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) {
                        latitude = location.getLatitude();
                        longitude = 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) {
                            latitude = location.getLatitude();
                            longitude = location.getLongitude();
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return location;
}

我通过这种方法将数据保存到数据库中:

private void getCoordinates() {
        GPSTracker gpstracker = new GPSTracker(this);
        if(gpstracker.canGetLocation)
        {
            double locationLng = gpstracker.getLongitude();
            double locationLat = gpstracker.getLatitude();

            databaseReference.child("lng").setValue(locationLng);
            databaseReference.child("lat").setValue(locationLat);
        }else
            gpstracker.showSettingsAlert();
    }

在这里你可以看到我的数据库,第一个用户已经被24个Api模拟器保存,第二个被25个保存,其他人也没有工作:

db

1 个答案:

答案 0 :(得分:1)

您需要23和25 api的许可请求

将其添加到课程

{{1}}