LocationServices.FusedLocationApi.getLastLocation不在onCreate()方法中工作

时间:2016-04-04 08:47:49

标签: android geolocation oncreate fusedlocationproviderapi

以下是我使用相同方法的代码 - 一次在我onCreate()的{​​{1}}期间和用户点击一次按钮后的一次

MainActivity

那为什么会这样呢?我基本上想要在加载应用程序时获取用户的当前位置,而不是强迫用户按下按钮以获得相同的内容。

// Below code not working during onCreate StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); // Create an instance of GoogleAPIClient. From Google API demo code if (mGoogleApiClient == null) { mGoogleApiClient = new GoogleApiClient.Builder(this) .addConnectionCallbacks(this) .addOnConnectionFailedListener(this) .addApi(LocationServices.API) .build(); } startLocationOnScreen.setText(getCurrentLocationViaPhoneLocation()); // Surprisingly same method works if it's called after a button press! startButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startLocationOnScreen.setText(getCurrentLocationViaPhoneLocation()); 方法的实施 - (主要来自Google API文档)

getCurrentLocationViaPhoneLocation()

修改:我在protected String getCurrentLocationViaPhoneLocation() { if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { // TODO: Consider calling // ActivityCompat#requestPermissions // here to request the missing permissions, and then overriding // public void onRequestPermissionsResult(int requestCode, String[] permissions, // int[] grantResults) // to handle the case where the user grants the permission. See the documentation // for ActivityCompat#requestPermissions for more details. return "Error - location services not available!"; } startLocation = LocationServices.FusedLocationApi.getLastLocation( mGoogleApiClient); if (startLocation != null) { Log.e("We are at ", String.valueOf(startLocation.getLatitude())); Log.e("We are at ", String.valueOf(startLocation.getLongitude())); Geocoder geocoder = new Geocoder(this, Locale.getDefault()); try { List<Address> addressList = geocoder.getFromLocation(startLocation.getLatitude(), startLocation.getLongitude(), 1); if (addressList != null && addressList.size() > 0) { currentCity = addressList.get(0).getLocality(); Address address = addressList.get(0); StringBuilder sb = new StringBuilder(); for (int i = 0; i < address.getMaxAddressLineIndex(); i++) { sb.append(address.getAddressLine(i)).append("\n"); } return (sb.toString()); } } catch (IOException e) { e.printStackTrace(); } } return ("Error - current location unavailable!"); } 期间获得Error - current location unavailable!,这意味着在onCreate()期间调用该方法时startLocation==null

1 个答案:

答案 0 :(得分:1)

我怀疑在您第一次调用该方法时(即onCreate期间),GoogleApiClient尚未连接。

您已经调用了Builder的addConnectionCallbacks方法。因此,您应该在Activity中实施onConnected(Bundle) GoogleApiClient.ConnectionCallbacks方法。在此处调用getCurrentLocationViaPhoneLocation方法而不是onCreated,然后您可以确定GoogleApiClient已正确连接。

如果未调用onConnected方法,请在onCreate中创建实例后尝试添加mGoogleApiClient.connect()

我希望有所帮助!