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

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

概述先看效果图:(以公司附近的国贸为中心点)上面是地图,下面是地理位置列表,有的只有地理位置列表(QQ动态的位置),这是个很常见的功能。它有个专门的叫法: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,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

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

总结

以上是内存溢出为你收集整理的Android实现带列表的地图POI周边搜索功能全部内容,希望文章能够帮你解决Android实现带列表的地图POI周边搜索功能所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1149184.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-31
下一篇2022-05-31

发表评论

登录后才能评论

评论列表(0条)

    保存