為了訪問(wèn) fused location provider,我們需要?jiǎng)?chuàng)建一個(gè) Google Play services API client 的實(shí)例。關(guān)于如何連接 client,請(qǐng)見(jiàn)連接 Google Play Services 。
public final class Constants {
public static final int SUCCESS_RESULT = 0;
public static final int FAILURE_RESULT = 1;
public static final String PACKAGE_NAME =
"com.google.android.gms.location.sample.locationaddress";
public static final String RECEIVER = PACKAGE_NAME + ".RECEIVER";
public static final String RESULT_DATA_KEY = PACKAGE_NAME +
".RESULT_DATA_KEY";
public static final String LOCATION_DATA_EXTRA = PACKAGE_NAME +
".LOCATION_DATA_EXTRA";
}
No location data provided - Intent 的附加數(shù)據(jù)沒(méi)有包含反向地理編碼需要用到的 Location 對(duì)象。
Invalid latitude or longitude used - Location 對(duì)象提供的緯度和/或者經(jīng)度無(wú)效。
No geocoder available - 由于網(wǎng)絡(luò)錯(cuò)誤或者 IO 異常,導(dǎo)致后臺(tái)地理編碼服務(wù)不可用。
Sorry, no address found - geocoder 找不到指定緯度/經(jīng)度對(duì)應(yīng)的地址。
使用 Address 類中的 getAddressLine()) 方法來(lái)獲得地址對(duì)象的個(gè)別行。然后將這些行加入一個(gè)地址 fragment 列表當(dāng)中。其中,這個(gè)地址 fragment 列表準(zhǔn)備好返回到發(fā)出地址請(qǐng)求的 activity。
當(dāng)用戶請(qǐng)求查找地理地址時(shí),調(diào)用上述的 startIntentService() 方法。例如,用戶可能會(huì)在我們應(yīng)用的 UI 上面點(diǎn)擊提取地址按鈕。在啟動(dòng) intent 服務(wù)之前,我們需要檢查是否已經(jīng)連接到 Google Play services。下面的代碼片段介紹在一個(gè)按鈕 handler 中調(diào)用 startIntentService() 方法。
public void fetchAddressButtonHandler(View view) {
// Only start the service to fetch the address if GoogleApiClient is
// connected.
if (mGoogleApiClient.isConnected() && mLastLocation != null) {
startIntentService();
}
// If GoogleApiClient isn't connected, process the user's request by
// setting mAddressRequested to true. Later, when GoogleApiClient connects,
// launch the service to fetch the address. As far as the user is
// concerned, pressing the Fetch Address button
// immediately kicks off the process of getting the address.
mAddressRequested = true;
updateUIWidgets();
}
如果用戶點(diǎn)擊了應(yīng)用 UI 上面的提取地址按鈕,那么我們必須在 Google Play services 連接穩(wěn)定之后啟動(dòng) intent 服務(wù)。下面的代碼片段介紹了調(diào)用 Google API Client 提供的 onConnected()) 回調(diào)函數(shù)中的 startIntentService() 方法。
public class MainActivity extends ActionBarActivity implements
ConnectionCallbacks, OnConnectionFailedListener {
...
@Override
public void onConnected(Bundle connectionHint) {
// Gets the best and most recent location currently available,
// which may be null in rare cases when a location is not available.
mLastLocation = LocationServices.FusedLocationApi.getLastLocation(
mGoogleApiClient);
if (mLastLocation != null) {
// Determine whether a Geocoder is available.
if (!Geocoder.isPresent()) {
Toast.makeText(this, R.string.no_geocoder_available,
Toast.LENGTH_LONG).show();
return;
}
if (mAddressRequested) {
startIntentService();
}
}
}
}
public class MainActivity extends ActionBarActivity implements
ConnectionCallbacks, OnConnectionFailedListener {
...
class AddressResultReceiver extends ResultReceiver {
public AddressResultReceiver(Handler handler) {
super(handler);
}
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
// Display the address string
// or an error message sent from the intent service.
mAddressOutput = resultData.getString(Constants.RESULT_DATA_KEY);
displayAddressOutput();
// Show a toast message if an address was found.
if (resultCode == Constants.SUCCESS_RESULT) {
showToast(getString(R.string.address_found));
}
}
}
}
更多建議: