前面获取到了位置数据,现在实现逆地理编码,根据经纬度数据得到地址。
一、定义IntentService去获取地址
1.在manifest中定义intent service:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mark.maptrackdemo" >
<application
...
<service
android:name=".FetchAddressIntentService"
android:exported="false"/>
</application>
...
</manifest>
2.创建一个地理编码器:
@Override
protected void onHandleIntent(Intent intent) {
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
...
}
3.检索地址数据
使用Geocoder的getFromLocation()方法。
定义常量类:
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.mark.maptrackdemo";
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";
}
4.返回地址给请求者
通过ResultReceiver的send()方法。
定义FetchAddressIntentService类继承IntentService,实现onHandleIntent()方法:
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import android.app.IntentService;
import android.content.Intent;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.os.Bundle;
import android.os.ResultReceiver;
import android.text.TextUtils;
import android.util.Log;
public class FetchAddressIntentService extends IntentService {
public final String TAG = "FetchAddressIntentService";
protected ResultReceiver mReceiver;
public FetchAddressIntentService() {
super(null);
}
@Override
protected void onHandleIntent(Intent intent) {
mReceiver = intent.getParcelableExtra(Constants.RECEIVER);
Location location = intent.getParcelableExtra(Constants.LOCATION_DATA_EXTRA);
// 创建一个地理编码器
Geocoder geocoder = new Geocoder(this, Locale.getDefault());
List<Address> addresses = null;
String errorMessage = "";
try {
// 获取地址
addresses = geocoder.getFromLocation(location.getLatitude(), location.getLongitude(), 1);
} catch (IOException ioException) {
errorMessage = getString(R.string.service_not_available);
Log.e(TAG, errorMessage, ioException);
} catch (IllegalArgumentException illegalArgumentException) {
errorMessage = getString(R.string.invalid_lat_long_used);
Log.e(TAG, errorMessage + ". " +
"Latitude = " + location.getLatitude() +
", Longitude = " +
location.getLongitude(), illegalArgumentException);
}
if (addresses == null || addresses.size() == 0) {
if (errorMessage.isEmpty()) {
errorMessage = getString(R.string.no_address_found);
Log.e(TAG, errorMessage);
}
deliverResultToReceiver(Constants.FAILURE_RESULT, errorMessage);
} else {
Address address = addresses.get(0);
ArrayList<String> addressFragments = new ArrayList<String>();
for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
addressFragments.add(address.getAddressLine(i));
}
deliverResultToReceiver(Constants.SUCCESS_RESULT,
TextUtils.join(System.getProperty("line.separator"),
addressFragments));
}
}
// 返回结果
private void deliverResultToReceiver(int resultCode, String message) {
Bundle bundle = new Bundle();
bundle.putString(Constants.RESULT_DATA_KEY, message);
mReceiver.send(resultCode, bundle);
}
}
二、开启IntentService
在Activity中开启服务,将结果处理对象和位置数据传过去:
protected void startIntentService() {
Intent intent = new Intent(this, FetchAddressIntentService.class);
intent.putExtra(Constants.RECEIVER, mResultReceiver);
intent.putExtra(Constants.LOCATION_DATA_EXTRA, mLastLocation);
startService(intent);
}
三、接收地理编码结果
定义类AddressResultReceiver继承ResultReceiver,对结果进行处理:
class AddressResultReceiver extends ResultReceiver {
public AddressResultReceiver(Handler handler) {
super(handler);
}
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
Log.i(TAG, "onReceiveResult : " + resultData.getString(Constants.RESULT_DATA_KEY));
if (resultCode == Constants.SUCCESS_RESULT) {
// 地址被找到
infoTextView.setText(resultData.getString(Constants.RESULT_DATA_KEY));
}
}
}
显示地址,只获取到宝安区 ?_?: