轻松学习android百度地图开发(二)

实现android百度地图搜索功能

本篇主要实现基于当前位置实现周围2000米的搜索,将在上次的代码基础上,实现过程如下图所示:

这里写图片描述

  • 先在mapView上面添加一个editText搜索框,实现搜索监听事件
  • 在mapView上面添加PoiSearch图层,主要用于搜索结果描绘
  • 对查找出来的地点图标mark添加监听事件,弹出其具体信息窗口展示

1、 MainActivit重写onCreate

MainActivity实现接口OnEditorActionListener, OnGetPoiSearchResultListener,在上次
代码的基础上,在onCreate方法里添加以下代码

// searchs为搜索框、mPoiSearch为PoiSearch图层
searchs.setOnEditorActionListener(this);
mPoiSearch = PoiSearch.newInstance();
mPoiSearch.setOnGetPoiSearchResultListener(this);
mMapView = (MapView) findViewById(R.id.bmapView);
mMapView.showZoomControls(false);
baiduMap = mMapView.getMap();
baiduMap.setMapStatus(MapStatusUpdateFactory.zoomTo(18));

// 开启定位图层
baiduMap.setMyLocationEnabled(true);
LocationClientOption option = new LocationClientOption();
option.setOpenGps(true);
option.setAddrType("detail");
option.setCoorType("bd09ll");
// 定位到当前位置
locClient = new LocationClient(this);
locClient.setLocOption(option);
locClient.registerLocationListener(this);
locClient.start();

自定义搜索方法

// 查询周围2000米的某类建筑物
public void poiSearch(String keyWords) {
    // 得到当前所在位置坐标
    BDLocation location = locClient.getLastKnownLocation();
    LatLng ll = new LatLng(location.getLatitude(), location.getLongitude());
    // 成功执行后将回调OnGetPoiSearchResultListener的方法
    mPoiSearch.searchNearby(new PoiNearbySearchOption().location(ll)
        .keyword(keyWords).radius(2000));
}

2、 editText搜索监听回调方法

@Override
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
    // 点击搜索后,自动隐藏软键盘
    InputMethodManager imm = (InputMethodManager)  
        getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(arg0.getWindowToken(), 0);
    // 调用搜索方法
    poiSearch(arg0.getText().toString());
    return true;
}

3、OnGetPoiSearchResultListener回调方法

@Override
public void onGetPoiDetailResult(PoiDetailResult result) {
    if (result.error != SearchResult.ERRORNO.NO_ERROR) {
        // 详情检索失败
        // result.error请参考SearchResult.ERRORNO
    } else {
        // 检索成功
    }
}

@Override
public void onGetPoiResult(PoiResult result) {
    if (result == null
            || result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
        return;
    }
    if (result.error == SearchResult.ERRORNO.NO_ERROR) {
        baiduMap.clear();
        // 自定义MyPoiOverlay继承自PoiOverlay
        PoiOverlay overlay = new MyPoiOverlay(baiduMap);
        // 设置overlay可以处理标注点击事件
        baiduMap.setOnMarkerClickListener(overlay);
        // 设置PoiOverlay数据
        overlay.setData(result);
        overlay.addToMap();
        overlay.zoomToSpan();
        return;
    }
}

自定义的MyPoiOverlay

private class MyPoiOverlay extends PoiOverlay {
    // 用来存储位置信息的类
    PoiInfo poi;

    public MyPoiOverlay(BaiduMap baiduMap) {
        super(baiduMap);
    }

    // 点击marker触发的事件
    @Override
    public boolean onPoiClick(int index) {
        poi = getPoiResult().getAllPoi().get(index);
        AlertDialog.Builder alertDialog = new AlertDialog.Builder(
                    MainActivity.this);
        // 设置Title的内容
        alertDialog.setTitle("位置信息");
        // 设置显示信息
        alertDialog.setMessage(poi.name + "\n" + poi.address);
        // 设置一个NegativeButton
        alertDialog.setNegativeButton("确定",new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        });
        alertDialog.show();
        return true;
    }
}

4、注意事项

以下方法必须重写

@Override
public void onDestroy() {
    locClient.stop();
    baiduMap.setMyLocationEnabled(false);
    mMapView.onDestroy();
    mPoiSearch.destroy();
    mMapView = null;
    Log.d("-d", "mapview");
    super.onDestroy();
}

@Override
public void onResume() {
    // 在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
    mMapView.onResume();
    super.onDestroy();
}

@Override
public void onPause() {
    // 在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
    mMapView.onPause();
    super.onPause();
}

下篇将实现百度地图路径规划

代码下载

如果感觉理解起来比较困难,可以参考百度地图开发指南

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值