GPS仅有时获得用户位置,并非总是如此

时间:2019-05-09 07:37:29

标签: android android-gps

我创建一个插入表格。当用户填写表格时,位置坐标也会与数据一起保存。我设置了高精度来获取位置。但是有时它不能在用户的手机上正常工作,纬度经度返回空值。我无法纠正问题,为什么会发生。

这是我的代码。

String latitude = "", longitude = "", latlngtime = "";

private Location mylocation;
GoogleApiClient mgoogleApiClient;
private final static int REQUEST_CHECK_SETTINGS_GPS = 21;

// set Google APi Client
private synchronized void setUpGClient() {
    mgoogleApiClient = new GoogleApiClient.Builder(this)
        .enableAutoManage(this, 0, this)
        .addConnectionCallbacks(this)
        .addOnConnectionFailedListener(this)
        .addApi(LocationServices.API)
        .build();
    mgoogleApiClient.connect();
}

// get Location
private void getMyLocation() {
    if (mgoogleApiClient != null) {
        if (mgoogleApiClient.isConnected()) {
            int permissionLocation = ContextCompat.checkSelfPermission(CrmTask.this, android.Manifest.permission.ACCESS_FINE_LOCATION);

            int permissionLocation1 = ContextCompat.checkSelfPermission(CrmTask.this, Manifest.permission.ACCESS_COARSE_LOCATION);
            if (permissionLocation == PackageManager.PERMISSION_GRANTED && permissionLocation1 == PackageManager.PERMISSION_GRANTED) {
                mylocation = LocationServices.FusedLocationApi.getLastLocation(mgoogleApiClient);
                LocationRequest locationRequest = new LocationRequest();
                locationRequest.setInterval(1000);
                locationRequest.setFastestInterval(500);
                locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
                LocationSettingsRequest.Builder builder = new LocationSettingsRequest.Builder()
                    .addLocationRequest(locationRequest);
                builder.setAlwaysShow(true);
                PendingResult result = LocationServices.SettingsApi.checkLocationSettings(mgoogleApiClient, builder.build());

                result.setResultCallback(new ResultCallback<LocationSettingsResult>() {
                    @Override
                    public void onResult(LocationSettingsResult result) {
                        final Status status = result.getStatus();
                        switch (status.getStatusCode()) {
                            case LocationSettingsStatusCodes.SUCCESS:
                                try {
                                    int permissionLocation = ContextCompat.checkSelfPermission(CrmTask.this, android.Manifest.permission.ACCESS_FINE_LOCATION);
                                    if (permissionLocation == PackageManager.PERMISSION_GRANTED) {
                                        mylocation = LocationServices.FusedLocationApi.getLastLocation(mgoogleApiClient);
                                        updateLocationData(mylocation);
                                    }
                                } catch (Exception e) {
                                    e.printStackTrace();
                                    //Toast.makeText(GetLocationActivity.this,""+e.getMessage(), Toast.LENGTH_LONG).show();
                                }
                                Log.i("TAG", "All location settings are satisfied.");

                                break;
                            case LocationSettingsStatusCodes.RESOLUTION_REQUIRED:
                                try {
                                    status.startResolutionForResult(CrmTask.this, REQUEST_CHECK_SETTINGS_GPS);
                                } catch (IntentSender.SendIntentException e) {
                                    // Ignore the error.
                                }
                                break;
                            case LocationSettingsStatusCodes.SETTINGS_CHANGE_UNAVAILABLE:
//                              Log.i(TAG, "Location settings are inadequate, and cannot be fixed here. Dialog not created.");
                                break;
                            }
                        }
                    });
                }
            }
        }
    }

    // update location latitude and longitude
    private void updateLocationData(Location location) {
        if (location != null) {
            // here I get coordinates

            Double lat = location.getLatitude();
            Double lng = location.getLongitude();
            latitude = String.valueOf(lat);  
            longitude = String.valueOf(lng);
            latlngtime = String.valueOf(location.getTime());
        }
    }

0 个答案:

没有答案