码迷,mamicode.com
首页 > 移动开发 > 详细

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

时间:2015-10-23 11:57:41      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:

1.相关接口及方法:

   ①  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.附上我做的简单封装代码,直接放进配置好的工程里可以直接调用

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

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

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

标签:

原文地址:http://www.cnblogs.com/ZhangXiangQian/p/4903748.html

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