上一篇讲述了地理编码的使用,没看的朋友可以看看http://blog.csdn.net/hedong_77/article/details/54287292,Ok,我们继续来看你地理编码,首先要理解它的概念,逆地理编码,又称地址解析服务,是指从已知的经纬度坐标到对应的地址描述(如行政区划、街区、楼层、房间等)的转换。常用于根据定位的坐标来获取该地点的位置详细信息,与定位功能是黄金搭档。也就是坐标转地址。
逆地理编码就很实用了,很多时候后台都会返回经纬度,然后APP端根据经纬度来定位,最常见的,好的,不说废话,来看看:
跟地理编码类似,只是在不同的方法里面做请求返回操作而已:
1、继承 OnGeocodeSearchListener 监听。
2、构造 GeocodeSearch 对象,并设置监听。
geocoderSearch = new GeocodeSearch(this);
geocoderSearch.setOnGeocodeSearchListener(this);
3、通过 RegeocodeQuery(LatLonPoint point, float radius, java.lang.String latLonType) 设置查询参数,调用 GeocodeSearch 的 getFromLocationAsyn(RegeocodeQuery regeocodeQuery) 方法发起请求。
// 第一个参数表示一个Latlng,第二参数表示范围多少米,第三个参数表示是火系坐标系还是GPS原生坐标系
RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 200,GeocodeSearch.AMAP);
geocoderSearch.getFromLocationAsyn(query);
4、通过回调接口 onRegeocodeSearched 解析返回的结果。
这里跟地理编码不一样的地方,就是返回方法不同,地理编码是在onGeocodeSearched里面做相应操作。
@Override
public void onRegeocodeSearched(RegeocodeResult result, int rCode) {
dismissDialog();
if (rCode == 1000) {
if (result != null && result.getRegeocodeAddress() != null
&& result.getRegeocodeAddress().getFormatAddress() != null) {
addressName = result.getRegeocodeAddress().getFormatAddress()
+ "附近";
aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
AMapUtil.convertToLatLng(latLonPoint), 15));
regeoMarker.setPosition(AMapUtil.convertToLatLng(latLonPoint));
ToastUtil.show(ReGeocoderActivity.this, addressName);
} else {
ToastUtil.show(ReGeocoderActivity.this, R.string.no_result);
}
} else {
ToastUtil.showerror(this, rCode);
}
}
来看全部代码:
/**
* 逆地理编码功能介绍
*/
public class ReGeocoderActivity extends Activity implements
OnGeocodeSearchListener, OnClickListener, OnMarkerClickListener {
private ProgressDialog progDialog = null;
private GeocodeSearch geocoderSearch;
private String addressName;
private AMap aMap;
private MapView mapView;
private LatLonPoint latLonPoint = new LatLonPoint(39.90865, 116.39751);
private Marker regeoMarker;
private ExecutorService mExecutorService;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.geocoder_activity);
/*
* 设置离线地图存储目录,在下载离线地图或初始化地图设置;
* 使用过程中可自行设置, 若自行设置了离线地图存储的路径,
* 则需要在离线地图下载和使用地图页面都进行路径设置
* */
//Demo中为了其他界面可以使用下载的离线地图,使用默认位置存储,屏蔽了自定义设置
// MapsInitializer.sdcardDir =OffLineMapUtils.getSdCacheDir(this);
mapView = (MapView) findViewById(R.id.map);
mapView.onCreate(savedInstanceState);// 此方法必须重写
init();
}
/**
* 初始化AMap对象
*/
private void init() {
if (aMap == null) {
aMap = mapView.getMap();
regeoMarker = aMap.addMarker(new MarkerOptions().anchor(0.5f, 0.5f)
.icon(BitmapDescriptorFactory
.defaultMarker(BitmapDescriptorFactory.HUE_RED)));
aMap.setOnMarkerClickListener(this);
}
geocoderSearch = new GeocodeSearch(this);
geocoderSearch.setOnGeocodeSearchListener(this);
progDialog = new ProgressDialog(this);
getAddresses();
}
/**
* 方法必须重写
*/
@Override
protected void onResume() {
super.onResume();
mapView.onResume();
}
/**
* 方法必须重写
*/
@Override
protected void onPause() {
super.onPause();
mapView.onPause();
}
/**
* 方法必须重写
*/
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mapView.onSaveInstanceState(outState);
}
/**
* 方法必须重写
*/
@Override
protected void onDestroy() {
super.onDestroy();
mapView.onDestroy();
if (mExecutorService != null) {
mExecutorService.shutdownNow();
}
}
/**
* 显示进度条对话框
*/
public void showDialog() {
progDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progDialog.setIndeterminate(false);
progDialog.setCancelable(true);
progDialog.setMessage("正在获取地址");
progDialog.show();
}
/**
* 隐藏进度条对话框
*/
public void dismissDialog() {
if (progDialog != null) {
progDialog.dismiss();
}
}
/**
* 响应逆地理编码
*/
public void getAddress(final LatLonPoint latLonPoint) {
showDialog();
RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 200,
GeocodeSearch.AMAP);// 第一个参数表示一个Latlng,第二参数表示范围多少米,第三个参数表示是火系坐标系还是GPS原生坐标系
geocoderSearch.getFromLocationAsyn(query);// 设置异步逆地理编码请求
}
/**
* 地理编码查询回调
*/
@Override
public void onGeocodeSearched(GeocodeResult result, int rCode) {
}
/**
* 逆地理编码回调
*/
@Override
public void onRegeocodeSearched(RegeocodeResult result, int rCode) {
dismissDialog();
if (rCode == AMapException.CODE_AMAP_SUCCESS) {
if (result != null && result.getRegeocodeAddress() != null
&& result.getRegeocodeAddress().getFormatAddress() != null) {
addressName = result.getRegeocodeAddress().getFormatAddress()
+ "附近";
aMap.animateCamera(CameraUpdateFactory.newLatLngZoom(
AMapUtil.convertToLatLng(latLonPoint), 15));
regeoMarker.setPosition(AMapUtil.convertToLatLng(latLonPoint));
ToastUtil.show(ReGeocoderActivity.this, addressName);
} else {
ToastUtil.show(ReGeocoderActivity.this, R.string.no_result);
}
} else {
ToastUtil.showerror(this, rCode);
}
}
@Override
public void onClick(View v) {
}
/**
* 响应逆地理编码的批量请求
*/
private void getAddresses() {
if (mExecutorService == null) {
mExecutorService = Executors.newSingleThreadExecutor();
}
List geopointlist = readLatLonPoints();
for (final LatLonPoint point : geopointlist) {
mExecutorService.submit(new Runnable() {
@Override
public void run() {
try {
RegeocodeQuery query = new RegeocodeQuery(point, 200,
GeocodeSearch.AMAP);// 第一个参数表示一个Latlng,第二参数表示范围多少米,第三个参数表示是火系坐标系还是GPS原生坐标系
RegeocodeAddress result = geocoderSearch.getFromLocation(query);// 设置同步逆地理编码请求
if (result != null && result.getFormatAddress() != null) {
aMap.addMarker(new MarkerOptions()
.position(new LatLng(point.getLatitude(), point.getLongitude()))
.title(result.getFormatAddress()));
}
} catch (AMapException e) {
Message msg = msgHandler.obtainMessage();
msg.arg1 = e.getErrorCode();
msgHandler.sendMessage(msg);
}
}
});
}
}
private Handler msgHandler = new Handler() {
public void handleMessage(Message msg) {
ToastUtil.showerror(ReGeocoderActivity.this, msg.arg1);
}
};
private List readLatLonPoints() {
List points = new ArrayList();
for (int i = 0; i < coords.length; i += 2) {
points.add(new LatLonPoint(coords[i + 1], coords[i]));
}
return points;
}
//测试的经纬度
private double[] coords = {116.339925, 39.976587,
116.328467, 39.976357,
116.345289, 39.966556,
116.321428, 39.967477,
116.358421, 39.961556,
116.366146, 39.961293,
116.359666, 39.953234,
116.373013, 39.948628,
116.355374, 39.94037,
116.41713, 39.940666,
116.433309, 39.940929,
116.461933, 39.949319,
116.473907, 39.938461,
116.478971, 39.933854,
116.491631, 39.96603,
116.489399, 39.971029,
116.495364, 39.98517,
116.530812, 39.99556,
116.5607, 39.996023,
116.525982, 40.022825,
116.568511, 40.016843,
116.584046, 40.014608,
116.422599, 40.012439,
116.44131, 40.00616,
116.39303, 40.026998,
116.384147, 40.039222,
116.388352, 39.928242};
@Override
public boolean onMarkerClick(Marker marker) {
marker.showInfoWindow();
return false;
}
}
getAddresses() 方法里面用到线程同步去做请求,因为我们这里是批量加载的。你编码就这么多了。