当前位置: 移动技术网 > IT编程>移动开发>Android > Android实现带列表的地图POI周边搜索功能

Android实现带列表的地图POI周边搜索功能

2019年07月24日  | 移动技术网IT编程  | 我要评论

九尾妖狐传txt,曾晨女神网,三环新城业主论坛

先看效果图:(以公司附近的国贸为中心点)

上面是地图,下面是地理位置列表,有的只有地理位置列表(qq动态的位置),这是个很常见的功能。它有个专门的叫法:poi周边搜索。

实现:

这个效果实现起来其实很简单,不过需要你先阅读下地图的api,这里使用的是高德地图的android sdk,sdk的配置这里不作讲解,文末会放一些链接供学习。

思路:

1、利用地图的定位功能,获取用户当前的位置
2、根据获得的位置信息调用poi搜索,获取位置列表
3、listview展示位置列表
4、用户拖动地图,获取地图中心坐标的位置信息,并执行2~3的步骤

代码:

layout:

<linearlayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  >

  <com.amap.api.maps2d.mapview
    android:id="@+id/map_local"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="2"/>

  <listview
    android:id="@+id/map_list"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="3"
    android:divider="@color/space"
    android:dividerheight="1dp"
    android:scrollbars="none"/>

</linearlayout>

activity:

public class new_localactivity extends activity implements locationsource,
    amaplocationlistener, amap.oncamerachangelistener, poisearch.onpoisearchlistener {

  @bindview(r.id.map_local)
  mapview mapview;
  @bindview(r.id.map_list)
  listview maplist;

  public static final string key_lat = "lat";
  public static final string key_lng = "lng";
  public static final string key_des = "des";


  private amaplocationclient mlocationclient;
  private locationsource.onlocationchangedlistener mlistener;
  private latlng latlng;
  private string city;
  private amap amap;
  private string deeptype = "";// poi搜索类型
  private poisearch.query query;// poi查询条件类
  private poisearch poisearch;
  private poiresult poiresult; // poi返回的结果
  private poioverlay poioverlay;// poi图层
  private list<poiitem> poiitems;// poi数据

  private poisearch_adapter adapter;


  @override
  protected void oncreate(bundle savedinstancestate) {
    super.oncreate(savedinstancestate);
    setcontentview(r.layout.activity_new__local);
    butterknife.bind(this);
    mapview.oncreate(savedinstancestate);
    init();
  }

  private void init() {
    if (amap == null) {
      amap = mapview.getmap();
      amap.setoncamerachangelistener(this);
      setupmap();
    }

    deeptype = "餐饮";//这里以餐饮为例
  }

  //-------- 定位 start ------

  private void setupmap() {
    if (mlocationclient == null) {
      mlocationclient = new amaplocationclient(getapplicationcontext());
      amaplocationclientoption mlocationoption = new amaplocationclientoption();
      //设置定位监听
      mlocationclient.setlocationlistener(this);
      //设置为高精度定位模式
      mlocationoption.setoncelocation(true);
      mlocationoption.setlocationmode(amaplocationclientoption.amaplocationmode.hight_accuracy);
      //设置定位参数
      mlocationclient.setlocationoption(mlocationoption);
      mlocationclient.startlocation();
    }
    // 自定义系统定位小蓝点
    mylocationstyle mylocationstyle = new mylocationstyle();
    mylocationstyle.mylocationicon(bitmapdescriptorfactory
        .fromresource(r.drawable.location_marker));// 设置小蓝点的图标
    mylocationstyle.strokecolor(color.black);// 设置圆形的边框颜色
    mylocationstyle.radiusfillcolor(color.argb(100, 0, 0, 180));// 设置圆形的填充颜色
    mylocationstyle.strokewidth(1.0f);// 设置圆形的边框粗细
    amap.setmylocationstyle(mylocationstyle);
    amap.setlocationsource(this);// 设置定位监听
    amap.getuisettings().setmylocationbuttonenabled(true);// 设置默认定位按钮是否显示
    amap.setmylocationenabled(true);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
  }

  /**
   * 开始进行poi搜索
   */
  protected void dosearchquery() {
    amap.setonmapclicklistener(null);// 进行poi搜索时清除掉地图点击事件
    int currentpage = 0;
    query = new poisearch.query("", deeptype, city);// 第一个参数表示搜索字符串,第二个参数表示poi搜索类型,第三个参数表示poi搜索区域(空字符串代表全国)
    query.setpagesize(20);// 设置每页最多返回多少条poiitem
    query.setpagenum(currentpage);// 设置查第一页
    latlonpoint lp = new latlonpoint(latlng.latitude, latlng.longitude);

    poisearch = new poisearch(this, query);
    poisearch.setonpoisearchlistener(this);
    poisearch.setbound(new poisearch.searchbound(lp, 5000, true));
    // 设置搜索区域为以lp点为圆心,其周围2000米范围
    poisearch.searchpoiasyn();// 异步搜索

  }

  @override
  public void onlocationchanged(amaplocation amaplocation) {
    if (mlistener != null && amaplocation != null) {
      if (amaplocation.geterrorcode() == 0) {
        // 显示我的位置
        mlistener.onlocationchanged(amaplocation);
        //设置第一次焦点中心
        latlng = new latlng(amaplocation.getlatitude(), amaplocation.getlongitude());
        amap.animatecamera(cameraupdatefactory.newlatlngzoom(latlng, 14), 1000, null);
        city = amaplocation.getprovince();
        dosearchquery();
      } else {
        string errtext = "定位失败," + amaplocation.geterrorcode() + ": " + amaplocation.geterrorinfo();
        log.e("amaperr", errtext);
      }
    }
  }

  @override
  public void activate(onlocationchangedlistener listener) {
    mlistener = listener;
    mlocationclient.startlocation();
  }

  @override
  public void deactivate() {
    mlistener = null;
    if (mlocationclient != null) {
      mlocationclient.stoplocation();
      mlocationclient.ondestroy();
    }
    mlocationclient = null;
  }

  @override
  public void oncamerachange(cameraposition cameraposition) {
  }

  @override
  public void oncamerachangefinish(cameraposition cameraposition) {
    latlng = cameraposition.target;
    amap.clear();
    amap.addmarker(new markeroptions().position(latlng));
    dosearchquery();
  }

  @override
  public void onpoisearched(poiresult result, int rcode) {
    if (rcode == 0) {
      if (result != null && result.getquery() != null) {// 搜索poi的结果
        if (result.getquery().equals(query)) {// 是否是同一条
          poiresult = result;
          poiitems = poiresult.getpois();// 取得第一页的poiitem数据,页数从数字0开始
          list<suggestioncity> suggestioncities = poiresult
              .getsearchsuggestioncitys();
          if (poiitems != null && poiitems.size() > 0) {
            adapter = new poisearch_adapter(this, poiitems);
            maplist.setadapter(adapter);
            maplist.setonitemclicklistener(new monitemclicklistener());

           }
          }
          else {
            logger.d("无结果");
          }
        }
      } else {
        logger.e("无结果");
      }
    } else if (rcode == 27) {
      logger.e("error_network");
    } else if (rcode == 32) {
      logger.e("error_key");
    } else {
      logger.e("error_other:" + rcode);
    }
  }

  @override
  public void onpoiitemsearched(poiitem poiitem, int i) {

  }

  //-------- 定位 end ------

  @override
  protected void onresume() {
    super.onresume();
    mlocationclient.startlocation();
  }

  @override
  protected void onpause() {
    super.onpause();
    mlocationclient.stoplocation();
  }

  @override
  protected void ondestroy() {
    mlocationclient.ondestroy();
    super.ondestroy();
  }

  private class monitemclicklistener implements adapterview.onitemclicklistener {
    @override
    public void onitemclick(adapterview<?> parent, view view, int position, long id) {
      intent intent = new intent();
      intent.putextra(key_lat, poiitems.get(position).getlatlonpoint().getlatitude());
      intent.putextra(key_lng, poiitems.get(position).getlatlonpoint().getlongitude());
      intent.putextra(key_des, poiitems.get(position).gettitle());
      setresult(result_ok, intent);
      finish();
    }
  }

示例中的activity是使用startactivityforresult方式启动的,最后点击位置之后会返回点选的位置信息。

总结:我第一次准备实现上述的效果时,也是不知所措,因为还没有对地图api有比较全面的认识,后来看了不少资料,自己便结合了一下地图的功能点,实现了设计图中的效果。

本文作者:他叫自己mr张

本文地址:

以上就是本文的全部内容,非常感谢作者的分享,希望对大家的学习有所帮助,大家共同进步。

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网