实现地图选择地址,附近poi检索等....

地图选点
/**
 * 地址-地图选点
 * 定位->经纬度->转地址信息PoiList
 * 移动地图->中心点经纬度->转地址信息PoiList
 * 关键字搜索->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 poiInfos = new ArrayList<>();
    private List 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);
        // 地图状态改变相关监听
        mBaiduMap.setOnMapStatusChangeListener(this);//让MainActivity实现OnMapStatusChangeListener

        // 创建GeoCoder实例对象
        geoCoder = GeoCoder.newInstance();
        // 设置查询结果监听者
        geoCoder.setOnGetGeoCodeResultListener(this);//让MainActivity实现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("选点列表click---" + poiInfos.get(position).toString());
                if (location == null) {
                    T.show("未获取到该地点经纬度信息,请重新输入");
                    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搜索模块设置,注册搜索事件监听---------------------------//
        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("未获取到该地点经纬度信息,请重新输入");
                    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实现OnGetPoiSearchResultListener,当系统定位成功则调用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;
                }
                //根据关键字搜索poi信息
                mPoiSearch.searchInCity((new PoiCitySearchOption())
                        .cityLimit(false)
                        .city(city)
                        .keyword(s.toString())
                        .pageCapacity(20));

            }
        });
    }

    // 输入搜索回调
    @Override
    public void onGetPoiResult(PoiResult result) {
        //获取POI检索结果
        if (result == null || result.getAllPoi() == null || result.getAllPoi().size() == 0) {
            T.show(R.string.empty_data);
            return;
        }
        L.show("----------------------当前搜索结果---------------------------");
        for (PoiInfo info : result.getAllPoi()) {
            L.show("当前的搜索信息:" + info.name + " " + info.address);
        }
        searchInfos.clear();
        searchInfos.addAll(result.getAllPoi());
        mPoiSearchAdapter.notifyDataSetChanged();
    }

    @Override
    public void onMapStatusChangeFinish(MapStatus mapStatus) {
        // 获取地图最后状态改变的中心点
        LatLng cenpt = mapStatus.target;
        L.show("最后停止点:" + cenpt.latitude + "," + cenpt.longitude);
        //将中心点坐标转化为具体位置信息,当转化成功后调用onGetReverseGeoCodeResult()方法
        geoCoder.reverseGeoCode(new ReverseGeoCodeOption().location(cenpt));

    }

    //经纬度转化地址
    @Override
    public void onGetReverseGeoCodeResult(ReverseGeoCodeResult reverseGeoCodeResult) {
        L.show("中心点转为地址-----" + reverseGeoCodeResult.toString());
        ReverseGeoCodeResult.AddressComponent detail = reverseGeoCodeResult.getAddressDetail();
        String province = detail.province;
        String city = detail.city;
        String area = detail.distance;

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

        if (infos != null && !"".equals(infos)) {
            //创建poiAdapter 将获取到的Poi数据传入,更新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监听器
        mLocationListener = new MyLocationListener();
        mLocClient.registerLocationListener(mLocationListener);
        // 定位参数选项
        LocationClientOption option = new LocationClientOption();
        option.setCoorType("bd09ll");
        // 设置是否需要地址信息,默认为无地址
        option.setIsNeedAddress(true);
        // 设置是否需要返回位置语义化信息,可以在BDLocation.getLocationDescribe()中得到数据,ex:"在天安门附近",
        // 可以用作地址信息的补充
        option.setIsNeedLocationDescribe(true);
        // 设置是否需要返回位置POI信息,可以在BDLocation.getPoiList()中得到数据
        option.setIsNeedLocationPoiList(true);
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);
        // 设置是否打开gps进行定位
        option.setOpenGps(true);
        // 设置 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())
                    // 此处设置开发者获取到的方向信息,顺时针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());
            // 获取城市,待会用于POISearch
            city = location.getCity();
            // 发起反地理编码请求(经纬度->地址信息)
            ReverseGeoCodeOption reverseGeoCodeOption = new ReverseGeoCodeOption();
            // 设置反地理编码位置坐标
            reverseGeoCodeOption.location(new LatLng(location.getLatitude(), location.getLongitude()));
            geoCoder.reverseGeoCode(reverseGeoCodeOption);

            //只需要打开该页面时定位一次即可,定位成功后关闭定位功能
            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) {

    }
}

布局文件:




    

        

        

            

            
        


    

    

        

            

            
        

        
    


    


附录:

  1. 高仿百度外卖地址添加功能(百度地图拖动定位,poi搜索,设置配送范围
  2. 百度地图SDK

你可能感兴趣的:(实现地图选择地址,附近poi检索等....)