【问题标题】:Google Location Services getLastLocation gets null when launching app for the first time首次启动应用程序时,Google 位置服务 getLastLocation 为空
【发布时间】:2017-02-20 14:45:34
【问题描述】:

所以我想显示我的位置的纬度和经度,但是它仅在我再次启动应用程序时才显示。 当我安装该应用程序并首次启动它时,没有收到任何位置。 第一次获取当前位置的简单方法是什么?我做错了什么?我在带有 API 24 的模拟器上运行它。

这是我的代码:

private final static String LOG_TAG = MainActivity.class.getName();
private GoogleApiClient googleApiClient;
private Location lastLocation;
private LocationRequest locationRequest;
private TextView locationLatitude;
private TextView locationLongitude;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    locationLatitude = (TextView) findViewById(R.id.latitude_text);
    locationLongitude = (TextView) findViewById(R.id.longitude_text);

    buildGoogleApiClient();
}

@Override
protected void onStart() {
    super.onStart();
    googleApiClient.connect();
}

@Override
protected void onStop() {
    super.onStop();
    googleApiClient.disconnect();
}

private void buildGoogleApiClient() {
    googleApiClient = new GoogleApiClient.Builder(this)
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(LocationServices.API)
            .build();
}

@Override
public void onConnected(@Nullable Bundle bundle) {
    Log.d(LOG_TAG, "onConnected");
    locationRequest = LocationRequest.create();
    locationRequest.setInterval(1000);
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);

    if(Build.VERSION.SDK_INT < 21){
        lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
    }
    else {
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
        }
        else{
            lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
        }
    }


    if(lastLocation != null){
        locationLatitude.setText(String.valueOf(lastLocation.getLatitude()));
        locationLongitude.setText(String.valueOf(lastLocation.getLongitude()));
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if(requestCode == 1){
        if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
            if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
                lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
            }
        }
    }
}

@Override
public void onConnectionSuspended(int i) {
    Log.d(LOG_TAG, "onConnectionSuspended");
}

@Override
public void onConnectionFailed(@NonNull ConnectionResult connectionResult) {
    Log.d(LOG_TAG, "onConnectionFailed");
}

@Override
public void onLocationChanged(Location location) {
    Log.d(LOG_TAG, "onLocationChanged");
    locationLatitude.setText(String.valueOf(location.getLatitude()));
    locationLongitude.setText(String.valueOf(location.getLongitude()));
}

【问题讨论】:

  • 有时最后一个位置不可用,将为空。考虑在此方案的首选项中保存最后一个位置,或回退到应用首次下载时遵循的过程。

标签: android google-play-services google-location-services


【解决方案1】:

您从不请求位置更新。

试试这个:

@Override
public void onConnected(@Nullable Bundle bundle) {
    Log.d(LOG_TAG, "onConnected");
    locationRequest = LocationRequest.create();
    locationRequest.setInterval(1000);
    locationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
LocationServices.FusedLocationApi.requestLocationUpdates(
            googleApiClient, locationRequest, this);


    if(Build.VERSION.SDK_INT < 21){
        lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
    }
    else {
        if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION}, 1);
        }
        else{
            lastLocation = LocationServices.FusedLocationApi.getLastLocation(googleApiClient);
        }
    }


    if(lastLocation != null){
        locationLatitude.setText(String.valueOf(lastLocation.getLatitude()));
        locationLongitude.setText(String.valueOf(lastLocation.getLongitude()));
    }
}

【讨论】:

  • 我已将我的 onConnected 方法更改为您的,但现在应用程序崩溃,这是一个例外:java.lang.SecurityException:客户端必须具有 ACCESS_FINE_LOCATION 权限才能请求 PRIORITY_HIGH_ACCURACY 位置。但是我的清单中有这个权限。
  • 你能展示你的权限实现吗?
  • 我刚刚把这个LocationServices.FusedLocationApi.requestLocationUpdates( googleApiClient, locationRequest, this); 放在我请求getLastLocation 的同一个地方。现在也不例外,但我仍然遇到与开始时相同的问题。我只更改了我的 onConnected 方法,其余的就像我的问题的开头(我提供了整个代码)
  • requestLocationUpdates() 方法调用是异步的,它的结果不能立即获得,所以仅仅在它之后调用requestLocationUpdates()getLastLocation() 不会给你位置。等待触发onLocationChanged() 回调。如果设备在室内,则可能需要数十秒(使用 GPS)或永远不会发生。
猜你喜欢
  • 2016-11-16
  • 2011-07-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多