實(shí)現(xiàn)地圖選擇地址裕循,附近poi檢索等....

地圖選點(diǎn)
/**
 * 地址-地圖選點(diǎn)
 * 定位->經(jīng)緯度->轉(zhuǎn)地址信息PoiList
 * 移動(dòng)地圖->中心點(diǎn)經(jīng)緯度->轉(zhuǎn)地址信息PoiList
 * 關(guān)鍵字搜索->PoiList
 */
public class AddressMapActivity extends AppCompatActivity implements BaiduMap.OnMapStatusChangeListener, OnGetGeoCoderResultListener, OnGetPoiSearchResultListener {

    @BindView(R.id.mAddressMapSearch)
    EditText mSearchContent;
    @BindView(R.id.mAddressMapView)
    MapView mapView;
    @BindView(R.id.mAddressMapRcy1)
    RecyclerView rcy1;
    @BindView(R.id.mAddressMapContentLayout1)
    LinearLayout mContentLayout;
    @BindView(R.id.mAddressMapRcy2)
    RecyclerView rcy2;
    private BaiduMap mBaiduMap;
    private GeoCoder geoCoder;
    private LocationClient mLocClient;
    private MyLocationListener mLocationListener;
    private PoiListAdapter mPoiListAdapter;
    private List<PoiInfo> poiInfos = new ArrayList<>();
    private List<PoiInfo> searchInfos = new ArrayList<>();
    private PoiSearchAdapter mPoiSearchAdapter;
    private String city;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_address_map);
        ButterKnife.bind(this);

        initContentView1();
        initContentView2();

        startLocation();
    }

    private void initContentView1() {
        mBaiduMap = mapView.getMap();
        MapStatus mapStatus = new MapStatus.Builder().zoom(15).build();
        MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mapStatus);
        mBaiduMap.setMapStatus(mMapStatusUpdate);
        // 地圖狀態(tài)改變相關(guān)監(jiān)聽
        mBaiduMap.setOnMapStatusChangeListener(this);//讓MainActivity實(shí)現(xiàn)OnMapStatusChangeListener

        // 創(chuàng)建GeoCoder實(shí)例對象
        geoCoder = GeoCoder.newInstance();
        // 設(shè)置查詢結(jié)果監(jiān)聽者
        geoCoder.setOnGetGeoCodeResultListener(this);//讓MainActivity實(shí)現(xiàn)OnGetGeoCoderResultListener

        rcy1.setLayoutManager(new LinearLayoutManager(this));
        rcy1.setHasFixedSize(true);
        mPoiListAdapter = new PoiListAdapter(R.layout.item_poi, poiInfos);
        mPoiListAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                String name = poiInfos.get(position).name;
                String province = poiInfos.get(position).province;
                String city = poiInfos.get(position).city;
                String area = poiInfos.get(position).area;
                LatLng location = poiInfos.get(position).location;
                L.show("選點(diǎn)列表click---" + poiInfos.get(position).toString());
                if (location == null) {
                    T.show("未獲取到該地點(diǎn)經(jīng)緯度信息徒仓,請重新輸入");
                    return;
                }
                Intent intent = new Intent();
                intent.putExtra("name", name);
                intent.putExtra("province", province);
                intent.putExtra("city", city);
                intent.putExtra("area", area);
                intent.putExtra("lat", location.latitude);
                intent.putExtra("lng", location.longitude);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        rcy1.setAdapter(mPoiListAdapter);
    }

    private void initContentView2() {
        //----------------------------poi搜索模塊設(shè)置璃岳,注冊搜索事件監(jiān)聽---------------------------//
        rcy2.setLayoutManager(new LinearLayoutManager(this));
        rcy2.setHasFixedSize(true);
        mPoiSearchAdapter = new PoiSearchAdapter(R.layout.item_poi_search, searchInfos);
        mPoiSearchAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                String name = searchInfos.get(position).name;
                String province = searchInfos.get(position).province;
                String city = searchInfos.get(position).city;
                String area = searchInfos.get(position).area;
                LatLng location = searchInfos.get(position).location;
                L.show("搜索列表click---" + searchInfos.get(position).toString());
                if (location == null) {
                    T.show("未獲取到該地點(diǎn)經(jīng)緯度信息涡尘,請重新輸入");
                    return;
                }
                Intent intent = new Intent();
                intent.putExtra("name", name);
                intent.putExtra("province", province);
                intent.putExtra("city", city);
                intent.putExtra("area", area);
                intent.putExtra("lat", location.latitude);
                intent.putExtra("lng", location.longitude);
                setResult(RESULT_OK, intent);
                finish();
            }
        });
        rcy2.setAdapter(mPoiSearchAdapter);

        PoiSearch mPoiSearch = PoiSearch.newInstance();
        //讓MainActivity實(shí)現(xiàn)OnGetPoiSearchResultListener趁冈,當(dāng)系統(tǒng)定位成功則調(diào)用onGetPoiResult()方法
        mPoiSearch.setOnGetPoiSearchResultListener(this);

        mSearchContent.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() <= 0) {
                    return;
                }
                //根據(jù)關(guān)鍵字搜索poi信息
                mPoiSearch.searchInCity((new PoiCitySearchOption())
                        .cityLimit(false)
                        .city(city)
                        .keyword(s.toString())
                        .pageCapacity(20));

            }
        });
    }

    // 輸入搜索回調(diào)
    @Override
    public void onGetPoiResult(PoiResult result) {
        //獲取POI檢索結(jié)果
        if (result == null || result.getAllPoi() == null || result.getAllPoi().size() == 0) {
            T.show(R.string.empty_data);
            return;
        }
        L.show("----------------------當(dāng)前搜索結(jié)果---------------------------");
        for (PoiInfo info : result.getAllPoi()) {
            L.show("當(dāng)前的搜索信息:" + info.name + " " + info.address);
        }
        searchInfos.clear();
        searchInfos.addAll(result.getAllPoi());
        mPoiSearchAdapter.notifyDataSetChanged();
    }

    @Override
    public void onMapStatusChangeFinish(MapStatus mapStatus) {
        // 獲取地圖最后狀態(tài)改變的中心點(diǎn)
        LatLng cenpt = mapStatus.target;
        L.show("最后停止點(diǎn):" + cenpt.latitude + "," + cenpt.longitude);
        //將中心點(diǎn)坐標(biāo)轉(zhuǎn)化為具體位置信息歼争,當(dāng)轉(zhuǎn)化成功后調(diào)用onGetReverseGeoCodeResult()方法
        geoCoder.reverseGeoCode(new ReverseGeoCodeOption().location(cenpt));

    }

    //經(jīng)緯度轉(zhuǎn)化地址
    @Override
    public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {
        L.show("中心點(diǎn)轉(zhuǎn)為地址-----" + reverseGeoCodeResult.toString());
        ReverseGeoCodeResult.AddressComponent detail = reverseGeoCodeResult.getAddressDetail();
        String province = detail.province;
        String city = detail.city;
        String area = detail.distance;

        List<PoiInfo> infos = reverseGeoCodeResult.getPoiList();
        for (int i = 0; i < poiInfos.size(); i++) {
            L.show("附近位置:" + poiInfos.get(i).name);
        }

        if (infos != null && !"".equals(infos)) {
            //創(chuàng)建poiAdapter 將獲取到的Poi數(shù)據(jù)傳入,更新UI
            poiInfos.clear();
            poiInfos.addAll(infos);
            mPoiListAdapter.notifyDataSetChanged();
        }
    }

    private void startLocation() {
        mBaiduMap.setMyLocationEnabled(true);
        // 定位圖層顯示方式
        MyLocationConfiguration.LocationMode mCurrentMode = MyLocationConfiguration.LocationMode.NORMAL;
        mBaiduMap.setMyLocationConfiguration(new MyLocationConfiguration(mCurrentMode, true, null));
        mLocClient = new LocationClient(this);
        //注冊LocationListener監(jiān)聽器
        mLocationListener = new MyLocationListener();
        mLocClient.registerLocationListener(mLocationListener);
        // 定位參數(shù)選項(xiàng)
        LocationClientOption option = new LocationClientOption();
        option.setCoorType("bd09ll");
        // 設(shè)置是否需要地址信息渗勘,默認(rèn)為無地址
        option.setIsNeedAddress(true);
        // 設(shè)置是否需要返回位置語義化信息沐绒,可以在BDLocation.getLocationDescribe()中得到數(shù)據(jù),ex:"在天安門附近"旺坠,
        // 可以用作地址信息的補(bǔ)充
        option.setIsNeedLocationDescribe(true);
        // 設(shè)置是否需要返回位置POI信息乔遮,可以在BDLocation.getPoiList()中得到數(shù)據(jù)
        option.setIsNeedLocationPoiList(true);
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        // 設(shè)置是否打開gps進(jìn)行定位
        option.setOpenGps(true);
        // 設(shè)置 LocationClientOption
        mLocClient.setLocOption(option);
        // 開始定位
        mLocClient.start();
    }

    class MyLocationListener extends BDAbstractLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            //mapView 銷毀后不在處理新接收的位置
            if (location == null || mapView == null) {
                return;
            }
            MyLocationData locData = new MyLocationData.Builder()
                    .accuracy(location.getRadius())
                    // 此處設(shè)置開發(fā)者獲取到的方向信息,順時(shí)針0-360
                    .direction(location.getDirection())
                    .latitude(location.getLatitude())
                    .longitude(location.getLongitude())
                    .build();
            mBaiduMap.setMyLocationData(locData);

            LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
            MapStatusUpdate msu = MapStatusUpdateFactory.newLatLngZoom(ll, 18);
            mBaiduMap.animateMapStatus(msu);
            LatLng locationLatLng = new LatLng(location.getLatitude(), location.getLongitude());
            // 獲取城市取刃,待會(huì)用于POISearch
            city = location.getCity();
            // 發(fā)起反地理編碼請求(經(jīng)緯度->地址信息)
            ReverseGeoCodeOption reverseGeoCodeOption = new ReverseGeoCodeOption();
            // 設(shè)置反地理編碼位置坐標(biāo)
            reverseGeoCodeOption.location(new LatLng(location.getLatitude(), location.getLongitude()));
            geoCoder.reverseGeoCode(reverseGeoCodeOption);

            //只需要打開該頁面時(shí)定位一次即可蹋肮,定位成功后關(guān)閉定位功能
            mLocClient.stop();
            mBaiduMap.setMyLocationEnabled(false);
        }
    }

    @OnClick({R.id.mAddressMapBack, R.id.mAddressMapSearch})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.mAddressMapBack:
                onBackPressed();
                break;
            case R.id.mAddressMapSearch:
                mContentLayout.setVisibility(View.GONE);
                rcy2.setVisibility(View.VISIBLE);
                break;
        }
    }

    @Override
    public void onBackPressed() {
        if (rcy2.getVisibility() == View.VISIBLE) {
            if (KeyboardUtils.isSoftInputVisible(this))
                KeyboardUtils.hideSoftInput(this);
            mContentLayout.setVisibility(View.VISIBLE);
            rcy2.setVisibility(View.GONE);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    protected void onDestroy() {
        mLocClient.unRegisterLocationListener(mLocationListener);
        mBaiduMap.setMyLocationEnabled(false);
        mBaiduMap.clear();
        mBaiduMap = null;
        mapView.onDestroy();
        mapView = null;
        super.onDestroy();

    }


    @Override
    public void onGetPoiDetailResult(PoiDetailResult poiDetailResult) {

    }

    @Override
    public void onGetPoiDetailResult(PoiDetailSearchResult poiDetailSearchResult) {

    }

    @Override
    public void onGetPoiIndoorResult(PoiIndoorResult poiIndoorResult) {

    }


    @Override
    public void onMapStatusChangeStart(MapStatus mapStatus) {

    }

    @Override
    public void onMapStatusChangeStart(MapStatus mapStatus, int i) {

    }

    @Override
    public void onMapStatusChange(MapStatus mapStatus) {

    }

    @Override
    public void onGetGeoCodeResult(GeoCodeResult geoCodeResult) {

    }
}

布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/white"
    android:clipToPadding="false"
    android:fitsSystemWindows="true"
    android:orientation="vertical">

    <android.support.v7.widget.Toolbar style="@style/AppToolbar">

        <ImageView
            android:id="@+id/mAddressMapBack"
            style="@style/AppBackImg"
            android:paddingRight="20dp" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="12dp"
            android:background="@color/white"
            android:gravity="center_vertical"
            android:orientation="horizontal">

            <ImageView
                android:id="@+id/iv_search"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_margin="6dp"
                android:src="@mipmap/ic_search" />

            <EditText
                android:id="@+id/mAddressMapSearch"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@android:color/transparent"
                android:hint="查找小區(qū)出刷,大廈,學(xué)校"
                android:singleLine="true"
                android:textColor="@color/text_color"
                android:textSize="14sp" />
        </LinearLayout>


    </android.support.v7.widget.Toolbar>

    <LinearLayout
        android:id="@+id/mAddressMapContentLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:clipToPadding="false"
        android:fitsSystemWindows="true"
        android:orientation="vertical">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:orientation="vertical"
            android:visibility="visible">

            <com.baidu.mapapi.map.MapView
                android:id="@+id/mAddressMapView"
                android:layout_width="match_parent"
                android:layout_height="match_parent" />

            <ImageView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerInParent="true"
                android:background="@android:color/transparent"
                android:src="@mipmap/icon_gcoding" />
        </RelativeLayout>

        <android.support.v7.widget.RecyclerView
            android:id="@+id/mAddressMapRcy1"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:cacheColorHint="#00000000"
            android:descendantFocusability="beforeDescendants"
            android:fastScrollEnabled="true"
            android:scrollbars="none" />
    </LinearLayout>


    <android.support.v7.widget.RecyclerView
        android:id="@+id/mAddressMapRcy2"
        android:layout_width="match_parent"
        android:background="@color/pager"
        android:layout_height="match_parent"
        android:visibility="gone" />

</LinearLayout>

附錄:

  1. 高仿百度外賣地址添加功能(百度地圖拖動(dòng)定位,poi搜索坯辩,設(shè)置配送范圍
  2. 百度地圖SDK
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末馁龟,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子濒翻,更是在濱河造成了極大的恐慌屁柏,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,858評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件有送,死亡現(xiàn)場離奇詭異淌喻,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)雀摘,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,372評論 3 395
  • 文/潘曉璐 我一進(jìn)店門裸删,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人阵赠,你說我怎么就攤上這事涯塔。” “怎么了清蚀?”我有些...
    開封第一講書人閱讀 165,282評論 0 356
  • 文/不壞的土叔 我叫張陵匕荸,是天一觀的道長。 經(jīng)常有香客問我枷邪,道長榛搔,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,842評論 1 295
  • 正文 為了忘掉前任东揣,我火速辦了婚禮践惑,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘嘶卧。我一直安慰自己尔觉,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,857評論 6 392
  • 文/花漫 我一把揭開白布芥吟。 她就那樣靜靜地躺著侦铜,像睡著了一般。 火紅的嫁衣襯著肌膚如雪钟鸵。 梳的紋絲不亂的頭發(fā)上泵额,一...
    開封第一講書人閱讀 51,679評論 1 305
  • 那天,我揣著相機(jī)與錄音携添,去河邊找鬼嫁盲。 笑死,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的羞秤。 我是一名探鬼主播缸托,決...
    沈念sama閱讀 40,406評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼瘾蛋!你這毒婦竟也來了俐镐?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,311評論 0 276
  • 序言:老撾萬榮一對情侶失蹤哺哼,失蹤者是張志新(化名)和其女友劉穎佩抹,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體取董,經(jīng)...
    沈念sama閱讀 45,767評論 1 315
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡棍苹,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了茵汰。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片枢里。...
    茶點(diǎn)故事閱讀 40,090評論 1 350
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡,死狀恐怖蹂午,靈堂內(nèi)的尸體忽然破棺而出栏豺,到底是詐尸還是另有隱情,我是刑警寧澤豆胸,帶...
    沈念sama閱讀 35,785評論 5 346
  • 正文 年R本政府宣布奥洼,位于F島的核電站,受9級特大地震影響晚胡,放射性物質(zhì)發(fā)生泄漏溉卓。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,420評論 3 331
  • 文/蒙蒙 一搬泥、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧伏尼,春花似錦忿檩、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,988評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至辨图,卻和暖如春班套,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背故河。 一陣腳步聲響...
    開封第一講書人閱讀 33,101評論 1 271
  • 我被黑心中介騙來泰國打工吱韭, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人鱼的。 一個(gè)月前我還...
    沈念sama閱讀 48,298評論 3 372
  • 正文 我出身青樓理盆,卻偏偏與公主長得像痘煤,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個(gè)殘疾皇子猿规,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,033評論 2 355

推薦閱讀更多精彩內(nèi)容