1. 程式人生 > >高德地圖教程_poi搜尋以及顯示

高德地圖教程_poi搜尋以及顯示

最近打算高仿深圳通的應用,UI已經做好了,我看過APP的查詢介面,斷了網也可以查詢這就表明資料是儲存在資料庫,或者就是第一次聯網,就在網站將資料全部下好了。我就想幹脆用地圖提供的查詢吧。

以前是接觸的百度地圖,不過百度地圖,不怎麼好用,我也就用高德地圖了關於工程建立 可以看官方文件,

到後面的專案,說不是很清楚,要看官方demo才行。我就乾脆自己寫一個。 看UI吧 我再把UI程式碼貼出來

Edittext輸入資訊 button將資料提交查詢 listview 顯示查詢回撥的資料 

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        tools:ignore="UselessLeaf,UselessParent" >

        <EditText
            android:id="@+id/et_keyword"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:hint="請輸入關鍵字" />

        <Button
            android:id="@+id/btn_search"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="搜尋" />
    </LinearLayout>

    <ListView
        android:id="@+id/lv_result"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>


第一步:初始化控制元件

	// 控制元件相關
	private EditText mEtKeyword;
	private Button mBtnSearch;
	private ListView mLvResult;

	private void initView() {
		mEtKeyword = (EditText) findViewById(R.id.et_keyword);

		mBtnSearch = (Button) findViewById(R.id.btn_search);

		mLvResult = (ListView) findViewById(R.id.lv_result);
		mBtnSearch.setOnClickListener(this);
		mLvResult.setOnItemClickListener(this);
	}

第二部 看button點選事件處理
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_search:
			//1.獲得使用者輸入資料
			String keyword = mEtKeyword.getText().toString();
			//2.判斷使用者是否輸入為空
			if (keyword.trim().length() == 0) {
				Toast.makeText(this, "請輸入查詢條件", Toast.LENGTH_LONG).show();
			} else {
			//3.不為空進行搜尋	
				search(keyword);
			}
			break;
		default:
			break;
		}
	}

第三步 看搜尋資料以及返回

這時候我們看看官方文件是怎麼說的

關鍵字搜尋

1.搜尋條件設定

您需要通過 PoiSearch.Query(String query, String ctgr, String city) 設定搜尋條件。引數“query”為搜尋的關鍵字,“ctgr”為搜尋型別(型別參照表從相關下載處獲取)、“city”為搜尋城市。關鍵字、型別至少輸入一個,搜尋城市必須輸入。

通過 Query.setPageSize(int) 設定查詢每頁的結果數目;

通過 Query.setPageNum(int) 設定查詢第幾頁。

2.傳送請求和接收資料。

使用 PoiSearch.searchPOIAsyn() 搜尋 POI。使用 PoiSearch.setOnPoiSearchListener() 方法設定監聽器,在 PoiSearch.OnPoiSearchListener 介面回撥方法 onPoiSearched(PoiResult poiResult,int rCode)中處理返回結果。當指定搜尋城市時,若沒有返回 POI 結果,則會返回包含關鍵字的建議城市名稱。當關鍵字搜尋無結果時,則會返回搜尋建議關鍵字。

SO  我們先定義

    // 興趣點查詢先關
    private PoiSearch search;
    //會返回查詢資料的類
    private PoiSearch.Query query;

因為我在深圳 所以 我會搜深圳的東西

	private void search(String keyword) {
		// 初始化查詢條件
		query = new Query(keyword, null, "深圳");
		query.setPageSize(10);
		query.setPageNum(1);

		// 查詢興趣點
		search = new PoiSearch(this, query);
		// 非同步搜尋
		search.searchPOIAsyn();
		search.setOnPoiSearchListener(this);
	}

第四步 看回調資料  因為我們自己設定了 返回10條資料  用listview 顯示
	/* -------------------- 興趣點查詢回撥 -------------------- */
	@Override
	public void onPoiItemDetailSearched(PoiItemDetail arg0, int arg1) {

	}

	@Override
	public void onPoiSearched(PoiResult poiResult, int rCode) {
		List<String> strs = new ArrayList<String>();
		items = poiResult.getPois();
		if (items != null && items.size() > 0) {
			PoiItem item = null;
			for (int i = 0, count = items.size(); i < count; i++) {
				item = items.get(i);
				strs.add(item.getTitle());
			}
			// 給ListView賦值,顯示結果
			ArrayAdapter<String> array = new ArrayAdapter<String>(this,
					android.R.layout.simple_list_item_1, strs);
			mLvResult.setAdapter(array);
		}
	}

	/* -------------------- 興趣點查詢回撥 -------------------- */