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

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

上面是地图,下面是地理位置列表,有的只有地理位置列表(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张

本文地址:http://blog.csdn.net/ys743276112/article/details/51519223

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

(0)

相关推荐

  • android二级listview列表实现代码

    今天来实现以下大众点评客户端的横向listview二级列表,先看一下样式.  这种横向的listview二级列表在手机软件上还不太常见,但是使用过平板的都应该知道,在平板上市比较常见的.可能是因为平板屏幕比较大,而且也能展现更多的内容. 下面来看一下我的实现步骤. 首先自定义一个listview,代码如下: 复制代码 代码如下: public class MyListView extends ListView implements Runnable { private float mLastDo

  • android 支持的语言列表(汇总)

    Arabic, Egypt (ar_EG) -----------------------------阿拉伯语,埃及Arabic, Israel (ar_IL) -------------------------------阿拉伯语,以色列Bulgarian, Bulgaria (bg_BG) ---------------------保加利亚语,保加利亚Catalan, Spain (ca_ES) ---------------------------加泰隆语,西班牙Czech, Czech

  • Android实现获取应用程序相关信息列表的方法

    本文所述为Androdi获取手机应用列表的方法,比如获取到Android应用的软件属性.大小和应用程序路径.应用名称等,获取所有已安装的Android应用列表,包括那些卸载了的,但没有清除数据的应用程序,同时在获取到应用信息的时候,判断是不是系统的应用程序,这是一个应用管理器所必需具备的功能. 具体实现代码如下: //AppInfoProvider.java package com.xh.ui; import java.util.ArrayList; import java.util.List;

  • Android用ListView显示SDCard文件列表的小例子

    复制代码 代码如下: filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/ADASiteMaps/SigRecord";        File file=new File(filePath);        File[] files = file.listFiles(); 构造Adapter, 复制代码 代码如下: for(File mCurrentFile:files){       

  • Android ExpandableListView展开列表控件使用实例

    你是否觉得手机QQ上的好友列表那个控件非常棒? 不是..... 那也没关系,学多一点知识对自己也有益无害. 那么我们就开始吧. 展开型列表控件, 原名ExpandableListView 是普通的列表控件进阶版, 可以自由的把列表进行收缩, 非常的方便兼好看. 首先看看我完成的截图, 虽然界面不漂亮, 但大家可以自己去修改界面. 该控件需要一个主界面XML 一个标题界面XML及一个列表内容界面XML 首先我们来看看 mian.xml 主界面 复制代码 代码如下: //该界面非常简单, 只要一个E

  • Android uses-permission权限列表中文注释版

    android同时也限定了系统资源的使用,像网络设备,SD卡,录音设备等.如果你的应用希望去使用任何系统资源,我们必须去申请Android的权限.这就是<uses-permission>元素的作用. 一个权限通常有以下格式,用一个名字为name 的字符串去指导我们希望使用的权限. 复制代码 代码如下: <uses-permission android:name="string"/> 例如:想要获得networking APIs的使用权限,我们指定如下的元素作为

  • Android实现三级联动下拉框 下拉列表spinner的实例代码

    主要实现办法:动态加载各级下拉值的适配器 在监听本级下拉框,当本级下拉框的选中值改变时,随之修改下级的适配器的绑定值              XML布局: 复制代码 代码如下: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_w

  • android开发教程之使用listview显示qq联系人列表

    首先还是xml布局文件,在其中添加ListView控件: 主布局layout_main.xml 复制代码 代码如下: <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"

  • ListView实现聊天列表之处理不同数据项

    通常我们用惯的ListView每一项的布局都是相同的,只是控件所绑定的数据不同.但单单只是如此并不能满足我们某些特殊需求,比如我们常见的QQ.微信的聊天列表,除了有左右之分外,内容更是有很大区别,有文字.语音.图片.视频等等,他们真的是ListView可以实现的吗?答案是肯定的,只要我们做一下类型区别即可. 实现效果如下所示: 大家不要在意布局,这里为了方便就随意了.大家可以看到,这里有两种布局,一种头像在左,一种头像在右,虽然这是一种简单的情况,但我们只需要了解其中的原理,再复杂的情况都可以迎

  • Android通过LIstView显示文件列表的两种方法介绍

    在Android中通过ListView显示SD卡中的文件列表一共有两种方法,一是:通过继承ListActivity显示;二是:利用BaseAdapter显示.BaseAdapter是一个公共基类适配器,用于对ListView和Spinner等 一些控件提供显示数据.下面是利用BaseAdapter类来实现通过LIstView显示SD卡的步骤: 1.main.xml界面设计,如下图 复制代码 代码如下: <?xml version="1.0" encoding="utf-

随机推荐