码迷,mamicode.com
首页 > 其他好文 > 详细

高德地图兴趣点搜索(PoiSearch)小例子

时间:2015-04-14 16:48:57      阅读:266      评论:0      收藏:0      [点我收藏+]

标签:android应用   高德地图   兴趣点搜索   

   目前我们项目上在做一个兴趣点搜索的小功能(搜索附近的电影院),用的是高德地图,为了便于记忆,就写下来。功能及页面都很简单,就是在输入框中输入内容,然后就会搜索出附近相关的位置,然后在ListView中展示出来。项目中使用的是分页加载,为了写文章方便,就把加载去掉了,直接用ListView展示出来。

----------------------------------界面布局---------------------------------------

<?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:background="@color/all_activity_layout_bg_color"
    android:orientation="vertical" >

    <RelativeLayout
        android:id="@+id/ll_search"
        android:layout_width="fill_parent"
        android:layout_height="40dp"
        android:layout_marginBottom="8dp"
        android:layout_marginLeft="10dp"
        android:layout_marginTop="8dp"
        android:orientation="horizontal" >

        <RelativeLayout
            android:id="@+id/rl_search_plane"
            android:layout_width="fill_parent"
            android:layout_height="40dp"
            android:layout_marginRight="10dp"
            android:layout_toLeftOf="@+id/tv_search_btn"
            android:background="@drawable/seabar_input" >

            <ImageView
                android:id="@+id/iv_search_img"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="10dp"
                android:src="@drawable/search_img" />

            <EditText
                android:id="@+id/et_search_edt_text"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_marginLeft="10dp"
                android:layout_marginRight="75dp"
                android:layout_toRightOf="@+id/iv_search_img"
                android:background="@null"
                android:gravity="center_vertical"
                android:hint="输入要搜索KTV地址"
                android:maxLength="32"
                android:singleLine="true"
                android:textColor="#333333"
                android:textColorHint="#aaaaaa"
                android:textSize="16sp" >
            </EditText>
        </RelativeLayout>

        <TextView
            android:id="@+id/tv_search_btn"
            android:layout_width="59dp"
            android:layout_height="40dp"
            android:layout_alignParentRight="true"
            android:layout_marginLeft="10dp"
            android:gravity="center"
            android:text="取消"
            android:textColor="#333333"
            android:textSize="16sp"
            android:visibility="gone" >
        </TextView>
    </RelativeLayout>

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_marginBottom="15dp"
        android:cacheColorHint="#0000"
        android:divider="@null"
        android:fadingEdge="none"
        android:fastScrollEnabled="false"
        android:footerDividersEnabled="false"
        android:headerDividersEnabled="false"
        android:listSelector="#00000000"
        android:scrollbars="none"
        android:windowSoftInputMode="adjustResize" />

</LinearLayout>

--------------------------Activity类-----------------------------------------

public class AmapPoisListActivity extends BaseAct implements OnItemClickListener {
private ListView listview;
private AmapAdapter mAdapter;
private EditText search_edt;
private TextView search_Tv;
private String keyString = "KTV";
private ArrayList<AmapBean> dataList;
private PoiSearchUtil mPoiSearchUtil;

@Override
public void setRootView() {//onCreate()方法
setContentView(R.layout.activity_myconcernlist_layout);
mPoiSearchUtil = new PoiSearchUtil(this);

                 initWidget();
}

private void initWidget() {
super.initWidget();
listview = (ListView) findViewById(R.id.listview);
search_edt = (EditText) findViewById(R.id.et_search_edt_text);
search_edt.addTextChangedListener(searchTextWatcher);
search_Tv = (TextView) findViewById(R.id.tv_search_btn);
search_Tv.setOnClickListener(this);
search_Tv.setVisibility(View.GONE);
mPoiSearchUtil.setPoiResultCallback(mPoiResultCallback);
mPoiSearchUtil.startSearchPoi(keyString);
initAdapter();
}

PoiResultCallback mPoiResultCallback = new PoiResultCallback() {

@Override
public void onSuccess(List<PoiItem> resultList, int pageNum, int totalPage) {
dataList = new ArrayList<AmapBean>();
for (PoiItem poiItem : resultList) {
AmapBean amapInfoBean = new AmapBean(poiItem);
dataList.add(amapInfoBean);
}
}
@Override
public void onError(String errorReason) {
dismissLoadingDialog();
showToastMsg(errorReason);
}


};

@Override
public void widgetClick(View v) {
// TODO Auto-generated method stub
super.widgetClick(v);
switch (v.getId()) {
case R.id.tv_search_btn:
search_edt.setText("");
break;
default:
break;
}
}

/** 初始化适配器和URL */
protected void initAdapter() {
mAdapter = new AmapAdapter(this, dataList);
listview.setAdapter(mAdapter);
listview.setOnItemClickListener(this);
}

@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
int realPosition = position - listview.getHeaderViewsCount();
Toast.makeText(this, "你点击的是:" + dataList.get(realPosition).getDistance(), Toast.LENGTH_SHORT).show();
}

private TextWatcher searchTextWatcher = new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (mAdapter == null) return;
if (s.length() > 0) {
mPoiSearchUtil.startSearchPoi(keyString);
search_Tv.setVisibility(View.VISIBLE);
}
else {
mPoiSearchUtil.startSearchPoi(keyString);
search_Tv.setVisibility(View.GONE);
}
}

@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
};


}

--------------数据适配类--------------------------------------

public class AmapAdapter extends BaseAdapter {
private Context context;
private ArrayList<AmapBean> dataList;

public AmapAdapter(Context context, ArrayList<AmapBean> dataList) {
this.context = context;
this.dataList = dataList;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if (convertView == null) {
convertView = View.inflate(context, R.layout.item_amap_list, null);
holder = new ViewHolder(convertView);
convertView.setTag(holder);
}
else {
holder = (ViewHolder) convertView.getTag();
}
setData(holder, position);
return convertView;
}

private void setData(ViewHolder holder, int position) {
holder.distancetv.setText(DistanceTools.distanceUnitConversion(dataList.get(position).getDistance()) + "");
holder.addresTv.setText(dataList.get(position).getPoiCompileAddr());
}

private static class ViewHolder {
public ViewHolder(View convertView) {
titleName = (TextView) convertView.findViewById(R.id.titleName);
distancetv = (TextView) convertView.findViewById(R.id.distancetv);
addresTv = (TextView) convertView.findViewById(R.id.addresTv);
}

/** 标题 */
TextView titleName;
/** 距离 */
TextView distancetv;
/** 详细地址 */
TextView addresTv;

}

@Override
public int getCount() {
// TODO Auto-generated method stub
return dataList.size();
}

@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return dataList.get(position);
}

@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}

}

--------------高德PoiSearch工具类----------------------------

public class PoiSearchUtil implements OnPoiSearchListener {
/**Poi查询条件类*/
private Query query;
private PoiSearch poiSearch;
private int currentPage = 0;
private LatLonPoint lp = new LatLonPoint(22.538249, 113.943487);// 默认深圳
private Context context;
/**poi返回的结果*/
protected PoiResult poiResult;
/**定义一个搜索回调接口*/
public static interface PoiResultCallback {
/**成功处理*/
void onSuccess(List<PoiItem> resultList, int pageNum, int totalPage);
/**失败处理*/
void onError(String errorReason);
}

private PoiResultCallback poiResultCallback;

public PoiResultCallback getPoiResultCallback() {
return poiResultCallback;
}

public void setPoiResultCallback(PoiResultCallback poiResultCallback) {
this.poiResultCallback = poiResultCallback;
}

public PoiSearchUtil(Context context) {
this.context = context;
}

public void setLatLonPoint(double latitude, double longitude) {
if ((latitude != 0) && (longitude != 0)) {
lp.setLatitude(latitude);
lp.setLongitude(longitude);
}
}

public void startSearchPoi(String key) {
startSearchPoi(key, PreferencesHelper.getCurrentLatitude(), PreferencesHelper.getCurrentLongitude(), 10);
}


public void startSearchPoi(String key, double latitude, double longitude, int pageSize) {
currentPage = 0;
/**第一个参数表示搜索字符串,第二个参数表示POI搜索类型(参考 《高德地图API POI类别对照表》),二选其一
* 第三个参数表示POI搜索区域(空字符串代表全国,参考《高德地图API 城市编码对照表》)
*/
setLatLonPoint(latitude, longitude);
try {
/**第三个直接填空,在setBound中设置范围*/
query = new Query(key, "", "");
/**下面两项所有POI*/
query.setLimitDiscount(false);
query.setLimitGroupbuy(false);
/**设置每页最多返回多少条poiitem*/
query.setPageSize(pageSize);
/**设置查第一页*/
query.setPageNum(0);
poiSearch = new PoiSearch(context, query);
/**搜索监听*/
poiSearch.setOnPoiSearchListener(this);
/**设置查询矩形*/
poiSearch.setBound(new SearchBound(lp, 8 * 1000, true));//
/**异步搜索*/
poiSearch.searchPOIAsyn();
}
catch (Exception e) {
e.printStackTrace();
if (null != poiResultCallback) poiResultCallback.onError("高德地图初始化失败");
}

}


@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;
ArrayList<PoiItem> poiItems = poiResult.getPois();
if (poiItems != null && poiItems.size() > 0) {
/**保存数据*/
if (null != poiResultCallback) poiResultCallback.onSuccess(poiItems, currentPage, result.getPageCount());
}
else {
if (null != poiResultCallback) poiResultCallback.onError("没有数据");
}
}
}
}
else if (rCode == 27) {
Log.v("ldm", "搜索失败,请检查网络连接");
if (null != poiResultCallback) poiResultCallback.onError("搜索失败,请检查网络连接");
}
else if (rCode == 32) {
Log.v("ldm", "key验证无效!");
if (null != poiResultCallback) poiResultCallback.onError("key验证无效!");
}
else {
Log.v("ldm", "未知错误,请稍后重试!错误码为" + rCode);
if (null != poiResultCallback) poiResultCallback.onError("未知错误,请稍后重试!错误码为" + rCode);
}
}

@Override
public void onPoiItemDetailSearched(PoiItemDetail arg0, int arg1) {
}

}

------------备注-------------------

1,要从高德地图开发者服务中获取正确的KEY值,

2,ListView的适配器中布局文件没有贴出来,就是几个TextView

3,时间原因,代码写得杂乱,有不正确的地方,请多多包涵。

高德地图兴趣点搜索(PoiSearch)小例子

标签:android应用   高德地图   兴趣点搜索   

原文地址:http://blog.csdn.net/true100/article/details/45042449

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!