上篇文章已經(jīng)跟大家講解了從零開始高德地圖的配置和定位功能,接下來的這篇是在上篇的基礎(chǔ)上增加逆地理編碼、poi搜索兩個(gè)功能并且把整個(gè)地圖、定位、poi搜索功能做一個(gè)完整的整合
上篇我們講到顯示當(dāng)前地圖和獲取到經(jīng)緯度。但是通常我們需要的是地址,要經(jīng)緯度也沒用哦!接下來就用這個(gè)接口來獲取到地址。
GeocodeSearch.OnGeocodeSearchListener
Activity繼承這個(gè)接口,并且重寫
public void onRegeocodeSearched(RegeocodeResult result, int rCode)
這個(gè)方法就可以很簡(jiǎn)單的實(shí)現(xiàn)獲取地址的功/**
/**
* 逆地理編碼回調(diào)
*/
@Override
public void onRegeocodeSearched(RegeocodeResult result, int rCode) {
if (rCode == 1000) {
if (result != null && result.getRegeocodeAddress() != null
&& result.getRegeocodeAddress().getFormatAddress() != null) {
String addressName = result.getRegeocodeAddress().getFormatAddress(); // 逆轉(zhuǎn)地里編碼不是每次都可以得到對(duì)應(yīng)地圖上的opi
searchName = addressName;
//search();
ToastUtils.showToast(AddressMapActivity.this, addressName);
// L.d("逆地理編碼回調(diào) 得到的地址:" + addressName);
// mAddressEntityFirst = new AddressSearchTextEntity(addressName, addressName, true, convertToLatLonPoint(mFinalChoosePosition));
} else {
ToastUtils.showToast(AddressMapActivity.this, "沒有結(jié)果");
}
} else if (rCode == 27) {
ToastUtils.showToast(AddressMapActivity.this, "網(wǎng)絡(luò)錯(cuò)誤");
} else {
ToastUtils.showToast(AddressMapActivity.this, "未知錯(cuò)誤");
}
}
這一種方式是定位成功后回調(diào)接口進(jìn)行解碼進(jìn)而獲取地址,那如果我不是通過定位的方式,比如只是有經(jīng)緯度的數(shù)據(jù),如何通過經(jīng)緯度轉(zhuǎn)換成地址呢?很簡(jiǎn)單↓
public void getAddress(LatLonPoint latLonPoint) {
// 第二參數(shù)表示范圍200米,第三個(gè)參數(shù)表示是火系坐標(biāo)系還是GPS原生坐標(biāo)系
RegeocodeQuery q = new RegeocodeQuery(latLonPoint, 200, GeocodeSearch.GPS);
RegeocodeQuery query = new RegeocodeQuery(latLonPoint, 200, GeocodeSearch.AMAP);
geocoderSearch.getFromLocationAsyn(query);// 設(shè)置同步逆地理編碼請(qǐng)求
}
接下來就是poi搜索,有時(shí)候我們需要定位到一個(gè)位置后,向用戶展示附近的一些商店或者公司之類的,這就要用到poi搜索還是繼承一個(gè)類
PoiSearch.OnPoiSearchListener
首先是PoiSearch.Query這個(gè)類,我們就是用這個(gè)類來搜索poi
第一步,設(shè)置Query的一些參數(shù)
PoiSearch.Query query = new PoiSearch.Query(searchName, type, "深圳市");//第二個(gè)參數(shù)為類型可不填
query.setPageSize(30);// 設(shè)置每頁(yè)最多返回多少條poiitem
query.setPageNum(0);//設(shè)置查第一頁(yè)
poiSearch = new PoiSearch(this, query);
poiSearch.setOnPoiSearchListener(this);//設(shè)置監(jiān)聽
//poiSearch.setBound(new PoiSearch.SearchBound(lat, 1000));//設(shè)置周邊搜索的中心點(diǎn)以及半徑
poiSearch.searchPOIAsyn(); //調(diào)用搜索
第二部,剛才我們說到繼承PoiSearch.OnPoiSearchListener這個(gè)類,那么需要重寫onPoiSearched(PoiResult result, int rcode)
@Override
public void onPoiSearched(PoiResult result, int rcode) {
if (rcode == 1000) {
if (result != null && result.getQuery() != null) {// 搜索poi的結(jié)果
if (result.getQuery().equals(query)) {// 是否是同一條
listView.setVisibility(View.VISIBLE);
poiResult = result;
if (poiResult.getPois().size() > 0) {
poiItems = poiResult.getPois();// 取得第一頁(yè)的poiitem數(shù)據(jù),頁(yè)數(shù)從數(shù)字0開始
if (poiItems != null && poiItems.size() > 0) {
adapter = new MyAdapter(AddressMapActivity.this, poiItems);
listView.setAdapter(adapter);
}
} else {
Toast.makeText(AddressMapActivity.this, "沒有搜索到相關(guān)數(shù)據(jù)", Toast.LENGTH_SHORT).show();
}
}
} else {
Toast.makeText(AddressMapActivity.this, "沒有搜索到相關(guān)數(shù)據(jù)", Toast.LENGTH_SHORT).show();
}
}
inputGone();
mWaitDialog.dismiss();
}
以上就是poi搜索的核心代碼。下面放點(diǎn)常用的api
//地圖點(diǎn)擊事件
@Override
public void onMapClick(LatLng latLng) {
//點(diǎn)擊地圖后清理圖層插上圖標(biāo),在將其移動(dòng)到中心位置
aMap.clear();
isHandDrag = false;
latitude = latLng.latitude;
longitude = latLng.longitude;
//地圖移動(dòng)到點(diǎn)擊點(diǎn)為中心 aMap.moveCamera(CameraUpdateFactory.changeLatLng(latLng));
lat = new LatLonPoint(latitude, longitude);
getAddress(lat);//調(diào)用獲取地址
doSearchQuery();//調(diào)用poi搜索
}
/**
* 拖動(dòng)地圖 結(jié)束回調(diào)
*
* @param cameraPosition 當(dāng)?shù)貓D位置發(fā)生變化,就重新查詢數(shù)據(jù)(手動(dòng)拖動(dòng)或者代碼改變地圖位置都會(huì)調(diào)用)
*/
@Override
public void onCameraChangeFinish(CameraPosition cameraPosition) {
LatLng mFinalChoosePosition = cameraPosition.target;
Log.d("log", "拖動(dòng)地圖 經(jīng)度" + mFinalChoosePosition.longitude + " 緯度:" + mFinalChoosePosition.latitude);
if (isHandDrag) {//手動(dòng)去拖動(dòng)地圖
LatLonPoint latLonPoint = new LatLonPoint(mFinalChoosePosition.latitude, mFinalChoosePosition.longitude);
if (lat.getLatitude() != latLonPoint.getLatitude() || lat.getLongitude() != latLonPoint.getLongitude()) {//判斷是位置有沒改變,如果有則↓
//移動(dòng)地圖時(shí)候最中間的標(biāo)桿的動(dòng)畫
mIvCenter.startAnimation(animationMarker);
lat = latLonPoint;
getAddress(lat);
doSearchQuery();
}
}
isHandDrag = true;
}
動(dòng)畫效果代碼↓
繼承Animation.AnimationListener并重寫以下方法
@Override
public void onAnimationStart(Animation animation) {
mIvCenter.setImageResource(R.mipmap.map_location);
}
@Override
public void onAnimationEnd(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
mIvCenter.setImageResource(R.mipmap.map_location);
}
地圖上每次移動(dòng)地圖,標(biāo)桿圖標(biāo)都會(huì)自動(dòng)移動(dòng)到中心,其實(shí)是把圖標(biāo)一直放在布局的最中心就可以了。
下篇文章會(huì)講到poi搜索(附近的位置搜索),逆地理編碼(經(jīng)緯度轉(zhuǎn)具體地址),大家稍等,先貼出效果圖!
多謝耐心看完,大家相互學(xué)習(xí),有什么問題及時(shí)溝通。
以上。