Android高德地圖(二)[逆地理編碼、poi搜索]

上篇文章已經(jīng)跟大家講解了從零開(kāi)始高德地圖的配置和定位功能,接下來(lái)的這篇是在上篇的基礎(chǔ)上增加逆地理編碼、poi搜索兩個(gè)功能并且把整個(gè)地圖、定位、poi搜索功能做一個(gè)完整的整合


上篇我們講到顯示當(dāng)前地圖和獲取到經(jīng)緯度。但是通常我們需要的是地址,要經(jīng)緯度也沒(méi)用哦!接下來(lái)就用這個(gè)接口來(lái)獲取到地址。

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, "沒(méi)有結(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)而獲取地址,那如果我不是通過(guò)定位的方式,比如只是有經(jīng)緯度的數(shù)據(jù),如何通過(guò)經(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)求 
  } 

接下來(lái)就是poi搜索,有時(shí)候我們需要定位到一個(gè)位置后,向用戶展示附近的一些商店或者公司之類的,這就要用到poi搜索還是繼承一個(gè)類

PoiSearch.OnPoiSearchListener

首先是PoiSearch.Query這個(gè)類,我們就是用這個(gè)類來(lái)搜索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)聽(tīng)
        //poiSearch.setBound(new PoiSearch.SearchBound(lat, 1000));//設(shè)置周邊搜索的中心點(diǎn)以及半徑
        poiSearch.searchPOIAsyn(); //調(diào)用搜索

第二部,剛才我們說(shuō)到繼承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開(kāi)始

            if (poiItems != null && poiItems.size() > 0) {
               adapter = new MyAdapter(AddressMapActivity.this, poiItems);          
                    listView.setAdapter(adapter);
                        }

 
                    } else {
                        Toast.makeText(AddressMapActivity.this, "沒(méi)有搜索到相關(guān)數(shù)據(jù)", Toast.LENGTH_SHORT).show();
                    }
                }
            } else {
                Toast.makeText(AddressMapActivity.this, "沒(méi)有搜索到相關(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()) {//判斷是位置有沒(méi)改變,如果有則↓
            //移動(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í),有什么問(wèn)題及時(shí)溝通。

以上。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容