Android 必知必会 - 带列表的地图 POI 周边搜索

2,311 阅读3分钟

如果移动端访问不佳,请尝试–> Github版

背景

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

2016-05-30_007D8133A7BD3AF58D7994F64FDDB8D9.gif

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

实现

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

思路:

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

代码:

Layout:



    

    

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 = "";
    private PoiSearch.Query query;
    private PoiSearch poiSearch;
    private PoiResult poiResult; 
    private PoiOverlay poiOverlay;
    private List poiItems;

    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 = "餐饮";
    }

    

    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);
    }

    /**
     * 开始进行poi搜索
     */
    protected void doSearchQuery() {
        aMap.setOnMapClickListener(null);
        int currentPage = 0;
        query = new PoiSearch.Query("", deepType, city);
        query.setPageSize(20);
        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));
        
        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) {
                if (result.getQuery().equals(query)) {
                    poiResult = result;
                    poiItems = poiResult.getPois();
                    List 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) {

    }

    

    @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有比较全面的认识,后来看了不少资料,自己便结合了一下地图的功能点,实现了设计图中的效果。

下面是一些资料,初学者务必先学习基础API的应用:

如果你有什么问题,可以在博客上留言。

PS:

你可以关注的我GithubCSDN微博