Android 百度地图 热点搜索(二)

1.OnGetPoiSearchResultListener 接口及方法:

   ①  OnGetPoiSearchResultListener接口  搜索结果监听

OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener(){  
    public void onGetPoiResult(PoiResult result){  
    //获取POI检索结果  
    }  
    public void onGetPoiDetailResult(PoiDetailResult result){  
    //获取Place详情页检索结果  
    }  
};

   ②创建POI搜索实例

  PoiSearch    mPoiSearch  = PoiSearch.newInstance();    //创建POI搜索实例

   ③设置POI搜索监听

  mPoiSearch.setOnGetPoiSearchResultListener(poiListener);

   ④发起搜索

   mPoiSearch.searchInCity(new PoiCitySearchOption().city(city)          //搜索所在城市
                                                    .keyword(keyWord)    //关键字
                                                    .pageNum(pageNum));   //页数

   ⑤POI详情搜索

mPoiSearch.searchPoiDetail((new PoiDetailSearchOption()).poiUid(UID));  //调用searchInCity返回的列表中每一项均包含UID,将这个值取出来
//OnGetPoiSearchResultListener 接口的第二个方法onGetPoiDetailResult
//会将搜索结果返回到这里

   ⑥释放POI搜索实例

   mPoiSearch.destroy();    //搜索结束之后一定要释放

 2.OnGetSuggestionResultListener 接口及方法,这个接口提供在线建议查询功能,根据用户输入的关键字提供在线建议词,SDK从3.5版本,开发查询结果的经纬度信息及对应POI信息的UID信息

 注意:1) 在线建议检索的本质是根据部分关键是检索出来可能的完整关键词名称,如果需要这些关键词对应的POI的具体信息,请使用POI检索来完成;

    2)  在线检索结果的第一条可能存在没有经纬度信息的情况,该条结果为文字联想出来的关键词结果,并不对应任何确切POI点。例如输入“肯”,第一条结果为“肯德基”,这条结果是一个泛指的名称,不会带有经纬度等信息。

  ①OnGetSuggestionResultListener 的实现

OnGetSuggestionResultListener listener = new OnGetSuggestionResultListener() {  
    public void onGetSuggestionResult(SuggestionResult res) {  
        if (res == null || res.getAllSuggestions() == null) {  
            return;  
            //未找到相关结果  
        }  
    //获取在线建议检索结果  
    }  
};

  

 3.附上我做的简单封装代码,直接放进配置好的工程里可以直接调用

package com.example.baidumapdemo;

import java.util.List;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.baidu.mapapi.search.core.CityInfo;
import com.baidu.mapapi.search.core.SearchResult;
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultListener;
import com.baidu.mapapi.search.poi.PoiCitySearchOption;
import com.baidu.mapapi.search.poi.PoiDetailResult;
import com.baidu.mapapi.search.poi.PoiResult;
import com.baidu.mapapi.search.poi.PoiSearch;
import com.baidu.mapapi.search.sug.OnGetSuggestionResultListener;
import com.baidu.mapapi.search.sug.SuggestionResult;
import com.baidu.mapapi.search.sug.SuggestionResult.SuggestionInfo;
import com.baidu.mapapi.search.sug.SuggestionSearch;

/**
 * 
 * @author Administrator
 * OnGetPoiSearchResultListener    POI搜索监听
 */
public class BaiduMapPoiSearchUtils implements OnGetPoiSearchResultListener,OnGetSuggestionResultListener{
    public PoiSearch                         mPoiSearch;
    private SuggestionSearch                mSuggestionSearch;
    //private InfoWindow                    mInfoWindow;
    
    private static BaiduMapPoiSearchUtils            mPoiSearchUtils;
    private Context                            context;
    
    private OnPoiSearchResultCallback        mOnPoiSearchResultCallback;
    private OnSuggestionResultCallback        mOnSuggestionResultCallback;
    private OnGetPoiDetailResultCallback    mOnGetPoiDetailResultCallback;
    private OnSearchErrorCallback            onSearchErrorCallback;
    
    private BaiduMapPoiSearchUtils(Context context){
        init(context);
    }
    
    public static BaiduMapPoiSearchUtils newInstance(Context context){
        if(mPoiSearchUtils == null){
            mPoiSearchUtils = new BaiduMapPoiSearchUtils(context);
        }
        return mPoiSearchUtils;
    }
    
    private void init(Context context){
        this.context      = context;
        mPoiSearch           = PoiSearch.newInstance();    //创建POI搜索实例
        mSuggestionSearch = SuggestionSearch.newInstance();
        mPoiSearch.setOnGetPoiSearchResultListener(this);
        mSuggestionSearch.setOnGetSuggestionResultListener(this);
    }
    
    /**
     * 热点搜索结果
     * @param mOnPoiSearchResultCallback
     */
    public void setOnPoiSearchResultCallback(OnPoiSearchResultCallback mOnPoiSearchResultCallback){
        this.mOnPoiSearchResultCallback = mOnPoiSearchResultCallback;
    }
    
    /*
     * 搜索建议 
     */
    public void setOnSuggestionResultCallback(OnSuggestionResultCallback mOnSuggestionResultCallback){
        this.mOnSuggestionResultCallback = mOnSuggestionResultCallback;
    }
    
    /**
     * 搜搜详情
     * @param mOnGetPoiDetailResultCallback
     */
    public void setOnGetPoiDetailResultCallback(OnGetPoiDetailResultCallback mOnGetPoiDetailResultCallback){
        this.mOnGetPoiDetailResultCallback = mOnGetPoiDetailResultCallback;
    }
    /**
     * 搜索出错
     * @param onSearchErrorCallback
     */
    public void setOnSearchErrorCallback(OnSearchErrorCallback onSearchErrorCallback) {
        this.onSearchErrorCallback = onSearchErrorCallback;
    }

    public void startSearch(String city,String keyWord,int pageNum){
        mPoiSearch.searchInCity(new PoiCitySearchOption().city(city)
                                                         .keyword(keyWord)
                                                         .pageNum(pageNum));
    }
    
    @Override
    public void onGetSuggestionResult(SuggestionResult result) {
        if(result == null || result.error != SearchResult.ERRORNO.NO_ERROR){
            if(onSearchErrorCallback != null){
                onSearchErrorCallback.onGetPoiDetailResult();
            }
            return;
        }
        if(mOnSuggestionResultCallback != null){
            mOnSuggestionResultCallback.onSuggestionResult(result.getAllSuggestions());
        }
    }

    @Override
    public void onGetPoiDetailResult(PoiDetailResult result) {
        if(result == null || result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND){
            Toast.makeText(context, "未找到结果", Toast.LENGTH_SHORT).show();
            return;
        }
        if(mOnGetPoiDetailResultCallback != null){
            mOnGetPoiDetailResultCallback.onGetPoiDetailResult(result);
        }
    }

    @Override
    public void onGetPoiResult(PoiResult result) {
        if(result == null || result.error != SearchResult.ERRORNO.NO_ERROR){
            Toast.makeText(context, "未找到结果", Toast.LENGTH_SHORT).show();
            return;
        }
        if(mOnPoiSearchResultCallback != null){
            mOnPoiSearchResultCallback.onPoiSearchResult(result);
        }
        if(result.error == SearchResult.ERRORNO.AMBIGUOUS_KEYWORD){
            String strInfo = "在";
            for(CityInfo c:result.getSuggestCityList()){
                strInfo += c.city;
                strInfo += ",";
            }
            strInfo += "找到结果";
            Log.e("TAG", strInfo);
        }
    }
    
    public interface OnPoiSearchResultCallback{
        public void onPoiSearchResult(PoiResult result);
    }
    public interface OnSuggestionResultCallback{
        public void onSuggestionResult(List<SuggestionInfo> list);
    }
    public interface OnGetPoiDetailResultCallback{
        public void onGetPoiDetailResult(PoiDetailResult result);
    }
    public interface OnSearchErrorCallback{
        public void onGetPoiDetailResult();
    }
    public void onDestroy(){
        mPoiSearch.destroy();
        mSuggestionSearch.destroy();
    }
}

 百度地图就先介绍到这,有时间会附上地图上位置标记。

posted @ 2015-10-23 11:09  还没好好感受年轻  阅读(2872)  评论(0编辑  收藏  举报