最近在使用到高德地图这块地理编码和逆地理编码,感觉非常好用.
一,首先要导入高德地图SDK,高德开放者平台有详细的介绍,和如何获取key的方式
二,地理编码和逆地理编码比较简单直接上代码了,有些重要的意见通过注释说明了.在这上面没有UI,直接通过toast出来了.
/**
* 地理编码与逆地理编码功能介绍
*/
public class GeocoderActivity extends Activity implements OnGeocodeSearchListener {
private GeocodeSearch geocoderSearch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LatLonPoint latLonPoint = new LatLonPoint(39.90865, 116.39751); //创建经纬度LatLonPoint
/*
* 设置离线地图存储目录,在下载离线地图或初始化地图设置;
* 使用过程中可自行设置, 若自行设置了离线地图存储的路径,
* 则需要在离线地图下载和使用地图页面都进行路径设置
* */
//Demo中为了其他界面可以使用下载的离线地图,使用默认位置存储,屏蔽了自定义设置
// MapsInitializer.sdcardDir =OffLineMapUtils.getSdCacheDir(this);
init();
getLatlon("中关村","北京");
getAddress(latLonPoint);
}
/**
* 初始化
*/
private void init() {
geocoderSearch = new GeocodeSearch(this);
geocoderSearch.setOnGeocodeSearchListener(this);
}
/**
* 响应地理编码
*/
public void getLatlon(String name,String city) {
// 第一个参数表示地址,第二个参数表示查询城市,中文或者中文全拼,citycode、adcode,
GeocodeQuery query = new GeocodeQuery(name, city);
geocoderSearch.getFromLocationNameAsyn(query);// 设置同步地理编码请求
}
/**
* 响应逆地理编码
*/
public void getAddress(LatLonPoint latLonPoint) {
// 第一个参数表示一个Latlng,第二参数表示范围多少米,第三个参数表示是火系坐标系还是GPS原生坐标系
RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 200, GeocodeSearch.AMAP);
geocoderSearch.getFromLocationAsyn(query);// 设置异步逆地理编码请求
}
/**
* 地理编码查询回调 result获取的结果 rCode为状态码
*/
@Override
public void onGeocodeSearched(GeocodeResult result, int rCode) {
if (rCode == AMapException.CODE_AMAP_SUCCESS) {
if (result != null && result.getGeocodeAddressList() != null
&& result.getGeocodeAddressList().size() > 0) {
GeocodeAddress address = result.getGeocodeAddressList().get(0);
LatLonPoint latLonPoint = address.getLatLonPoint();
Toast.makeText(this, "latLonPoint"+latLonPoint, Toast.LENGTH_SHORT).show();
}
}
}
/**
* 逆地理编码回调 result获取的结果 rCode为状态码
*/
@Override
public void onRegeocodeSearched(RegeocodeResult result, int rCode) {
if (rCode == AMapException.CODE_AMAP_SUCCESS) {
if (result != null && result.getRegeocodeAddress() != null
&& result.getRegeocodeAddress().getFormatAddress() != null) {
String address = result.getRegeocodeAddress().getFormatAddress()
+ "附近";
Toast.makeText(this, "address"+address, Toast.LENGTH_SHORT).show();
}
}
}
}