高德地图,仿微信周边位置poi搜索

高德地图,仿微信周边位置poi搜索

高德地图,仿微信周边位置poi搜索

首先要申请一个高德地图开发者账号,建立你的项目,得到key


高德地图官方文档都有介绍,具体可去配置 

http://lbs.amap.com/api/android-sdk/guide/create-project/dev-attention


在AndroidManifest中添加一些权限,

高德地图,仿微信周边位置poi搜索


把你申请的项目的key添加到value中来

高德地图,仿微信周边位置poi搜索



在libs文件夹下添加需要的jar包


高德地图,仿微信周边位置poi搜索


上代码,参照高德的poi文档进行实现,

http://lbs.amap.com/api/android-sdk/guide/map-data/poi/



主代码,Activity

public class New_LocalActivity extends Activity implements LocationSource,
        AMapLocationListener, AMap.OnCameraChangeListener, PoiSearch.OnPoiSearchListener {

    MapView mapView;
    ListView mapList;

    public static final String KEY_LAT = "lat";
    public static final String KEY_LNG = "lng";
    public static final String KEY_DES = "des";
    public static final String KEY_SNI = "snippet";

    private AMapLocationClient mLocationClient;
    private LocationSource.OnLocationChangedListener mListener;
    private LatLng latlng;
    private String city;
    private AMap aMap;
    private String deepType = "";// poi搜索类型
    private String keyWord = "";// 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;
    private TextView searchButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new__local);
        mapView = (MapView) findViewById(R.id.map_local);
        mapList = (ListView) findViewById(R.id.map_list);

        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();
        }
        aMap.setLocationSource(this);// 设置定位监听
        aMap.getUiSettings().setMyLocationButtonEnabled(true);// 设置默认定位按钮是否显示
        aMap.setMyLocationEnabled(true);// 设置为true表示显示定位层并可触发定位,false表示隐藏定位层并不可触发定位,默认是false
    }

    /**
     * 开始进行poi搜索
     */
    protected void doSearchQuery() {
        aMap.setOnMapClickListener(null);// 进行poi搜索时清除掉地图点击事件
        int currentPage = 0;
// 第一个参数表示搜索字符串keyWord,第二个参数表示poi搜索类型,第三个参数表示poi搜索区域(空字符串代表全国)
query = new PoiSearch.Query("", deepType, "广州"); 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(); } ArrayList<LocationBean> data = new ArrayList<LocationBean>(); @Override public void onPoiSearched(PoiResult result, int rCode) { if (rCode == 1000) { if (result != null && result.getQuery() != null) {// 搜索poi的结果 if (result.getQuery().equals(query)) {// 是否是同一条 poiResult = result; poiItems = poiResult.getPois();// 取得第一页的poiitem数据,页数从数字0开始 data.clear(); for (PoiItem item : poiItems) { //获取经纬度对象 LatLonPoint llp = item.getLatLonPoint(); double lon = llp.getLongitude(); double lat = llp.getLatitude(); //获取标题 String title = item.getTitle(); //获取内容 String text = item.getSnippet(); data.add(new LocationBean(lon, lat, title, text)); } List<SuggestionCity> suggestionCities = poiResult .getSearchSuggestionCitys(); if (data != null && data.size() > 0) { adapter = new PoiSearch_adapter(this, data); mapList.setAdapter(adapter); mapList.setOnItemClickListener(new mOnItemClickListener()); } } } } } @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()); intent.putExtra(KEY_SNI, poiItems.get(position).getSnippet()); setResult(RESULT_OK, intent); finish(); } }}


布局


<?xml version="1.0" encoding="utf-8"?>
<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="200dp"/>


    <ListView
        android:id="@+id/map_list"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="3"
        android:divider="#000"
        android:dividerHeight="1dp"
        android:scrollbars="none"/>

</LinearLayout>

Bean类

public class LocationBean implements Serializable{

    private double lon;
    private double lat;
    private String title;
    private String content;

    public LocationBean(double lon,double lat,String title,String content){
        this.lon = lon;
        this.lat = lat;
        this.title = title;
        this.content = content;
    }


    public double getLon() {
        return lon;
    }

    public double getLat() {
        return lat;
    }

    public String getTitle() {
        return title;
    }

    public String getContent() {
        return content;
    }
}

Adapter


public class PoiSearch_adapter extends BaseAdapter {
    private Context ctx;
    private List<LocationBean> list;

    public PoiSearch_adapter(Context context, List<LocationBean> poiList) {
        this.ctx = context;
        this.list = poiList;
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
            holder = new ViewHolder();
            convertView = View.inflate(ctx, R.layout.poisearch_item, null);
            holder.poititle = (TextView) convertView.findViewById(R.id.poititle);
            holder.poititle2 = (TextView) convertView.findViewById(R.id.poititle2);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolder) convertView.getTag();
        }
        LocationBean item = (LocationBean) getItem(position);
        holder.poititle.setText(item.getTitle());
        holder.poititle2.setText(item.getContent());
        return convertView;
    }

    private class ViewHolder {
        TextView poititle;
        TextView poititle2;
    }
}

item布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:background="#d1cece"
              android:orientation="vertical"
              android:padding="8dp"

    >

    <TextView
        android:id="@+id/poititle"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:layout_marginLeft="5dp"
        android:gravity="center_vertical"
        android:singleLine="true"
        android:textColor="#000"
        android:text=""
        android:textSize="16sp"/>

    <TextView
        android:id="@+id/poititle2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:layout_marginLeft="5dp"
        android:gravity="center_vertical"
        android:text=""
        android:singleLine="true"
        android:textColor="#000"
        android:textSize="16sp"/>

</LinearLayout>