百度POI检索searchNearby中free keyword search不支持多个怎么解决

下次自动登录
现在的位置:
& 综合 & 正文
Android 百度地图POI检索之显示单次搜索的所有结果
转载请注明出处:
最近在研究百度地图API,通过POI搜索时发现每次只能显示10个覆盖物,强迫症不能忍啊有木有,官方demo并不能满足我的需求,研究了一段时间实现了显示所有覆盖物的功能。
来,看图(= =这回要犯密集恐惧症了)
下面来看看是如何实现的
map.xml 很简单,就一个MapView控件
&?xml version="1.0" encoding="utf-8"?&
&RelativeLayout xmlns:android="/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" &
&com.baidu.mapapi.map.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true" /&
&/RelativeLayout&
* 演示poi搜索功能
public class PoiSearchDemo extends Activity {
/** 百度地图 */
private MapView mapV
/** 定义 BaiduMap 地图对象的操作方法与接口 */
private BaiduMap baiduM
private PoiSearch poiS
/** 搜索关键词 */
private final String keyword = "餐厅";
/** 每页容量50 */
private final int pageCapacity = 50;
/** 第一页 */
private final int pageNum = 0;
/** 搜索半径10km */
private final int radius = 10000;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
initView();
initData();
private void initView() {
mapView = (MapView) findViewById(R.id.mapView);
// 是否显示自带缩放控件
mapView.showZoomControls(false);
private void initData() {
baiduMap = mapView.getMap();
// 初始化搜索模块
poiSearch = PoiSearch.newInstance();
// 注册搜索事件监听
.setOnGetPoiSearchResultListener(new PoiSearchResultListener());
// 搜索该坐标附近的餐厅
poiSearch.searchNearby(new PoiNearbySearchOption().keyword(keyword)
.location(new LatLng(39.6.327764))
.pageCapacity(pageCapacity).pageNum(pageNum).radius(radius));
protected void onPause() {
mapView.onPause();
super.onPause();
protected void onResume() {
mapView.onResume();
super.onResume();
protected void onDestroy() {
// 销毁搜索模块
poiSearch.destroy();
// 关闭定位图层
baiduMap.setMyLocationEnabled(false);
mapView.onDestroy();
super.onDestroy();
private class PoiSearchResultListener implements
OnGetPoiSearchResultListener {
public void onGetPoiDetailResult(PoiDetailResult result) {
if (result.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(getApplicationContext(),
result.getName() + ": " + result.getAddress(), 0)
public void onGetPoiResult(PoiResult result) {
if ((result == null)
|| (result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND)) {
if (result.error == SearchResult.ERRORNO.NO_ERROR) {
baiduMap.clear();
&span style="background-color: rgb(255, 255, 153);"&MyPoiOverlay overlay = new MyPoiOverlay(baiduMap);&/span&
baiduMap.setOnMarkerClickListener(overlay);
overlay.setData(result);
overlay.addToMap();
// 缩放地图,使所有Overlay都在合适的视野内
overlay.zoomToSpan();
if (result.error == SearchResult.ERRORNO.AMBIGUOUS_KEYWORD) {
&/pre&&pre name="code" class="java"&&span style="white-space:pre"& &/span&private class MyPoiOverlay extends OverlayManager {...}
这里与官方的demo很相似,不同的是MyPoiOverlay这个类
MyPoiOverlay
private class MyPoiOverlay extends OverlayManager {
private PoiResult poiResult =
public MyPoiOverlay(BaiduMap baiduMap) {
super(baiduMap);
public void setData(PoiResult poiResult) {
this.poiResult = poiR
public boolean onMarkerClick(Marker marker) {
if (marker.getExtraInfo() != null) {
int index = marker.getExtraInfo().getInt("index");
PoiInfo poi = poiResult.getAllPoi().get(index);
// 详情搜索
poiSearch.searchPoiDetail((new PoiDetailSearchOption())
.poiUid(poi.uid));
public List&OverlayOptions& getOverlayOptions() {
if ((this.poiResult == null)
|| (this.poiResult.getAllPoi() == null))
ArrayList&OverlayOptions& arrayList = new ArrayList&OverlayOptions&();
for (int i = 1; i & this.poiResult.getAllPoi().size(); i++) {
if (this.poiResult.getAllPoi().get(i).location == null)
// 给marker加上标签
Bundle bundle = new Bundle();
bundle.putInt("index", i);
arrayList.add(new MarkerOptions()
.icon(BitmapDescriptorFactory
.fromBitmap(setNumToIcon(i))).extraInfo(bundle)
.position(this.poiResult.getAllPoi().get(i).location));
return arrayL
* 往图片添加数字
private Bitmap setNumToIcon(int num) {
BitmapDrawable bd = (BitmapDrawable) getResources().getDrawable(
R.drawable.icon_gcoding);
Bitmap bitmap = bd.getBitmap().copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setAntiAlias(true);
int widthX;
int heightY = 0;
if (num & 10) {
paint.setTextSize(30);
widthX = 8;
heightY = 6;
paint.setTextSize(20);
widthX = 11;
canvas.drawText(String.valueOf(num),
((bitmap.getWidth() / 2) - widthX),
((bitmap.getHeight() / 2) + heightY), paint);
思路如下:
这里通过继承OverlayManager这个类,改写了getOverlayOptions()和onMarkerClick()这两个方法
通过官方demo中给出的icon_gconding,在代码中给图片加上数字,达到需要的效果
每次搜索最多只能设置50,另外Bitmap过多会导致OOM。
最后说下,第一次写博客(怎么装成经常写的样子?= =),有什么说的不对的地方请告诉我。
&&&&推荐文章:
【上篇】【下篇】最新百度地图V3.4开发(5)
前面学习百度地图的一些基本的用法,这次我们一起来看一看百度地图的检索功能吧&
poi检索api的基本用法
百度地图的POI类中共有如下几个方法
POI范围内检索参数
poi城市内检索参数
详情检索结果
poi 详情检索参数
附近检索参数
poi搜索结果。
POI检索接口
我们现在先看下中的内容
释放检索对象
创建PoiSearch实例
范围内检索
城市内检索
POI 详情检索
(&listener)
设置poi检索监听者
从api中我们不难看出,百度的poi检索主要有范围检索(指定坐标范围)、城市检索(指定某一城市)周边检索(根据自身位置周边)和poi详情检索
2.指定坐标范围搜索
无论是什么搜索基本步骤都是不变的
&span style=&color:#ff6666;&& // 第一步,创建POI检索实例&/span&
poiSearch = PoiSearch.newInstance();
&span style=&color:#ff6666;&& // 第二步,创建POI检索监听者;&/span&
OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener() {
public void onGetPoiResult(PoiResult result) {
// 获取POI检索结果
if (result == null || result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
Toast.makeText(POISearchMapActivity.this, &未找到结果&, Toast.LENGTH_LONG).show();
if (result.error == SearchResult.ERRORNO.NO_ERROR) {
List&PoiInfo& allPoi = result.getAllPoi();
totalpages = result.getTotalPageNum();
String[] datas = new String[10];
for (int i = 0; i & 10; i++) {
datas[i] = allPoi.get(i).
adapter = new ArrayAdapter&String&(POISearchMapActivity.this, android.R.layout.simple_list_item_1, datas);
listView.setAdapter(adapter);
if (result.error == SearchResult.ERRORNO.AMBIGUOUS_KEYWORD) {
// 当输入关键字在本市没有找到,但在其他城市找到时,返回包含该关键字信息的城市列表
String strInfo = &在&;
for (CityInfo cityInfo : result.getSuggestCityList()) {
strInfo += cityInfo.
strInfo += &,&;
strInfo += &找到结果&;
Toast.makeText(POISearchMapActivity.this, strInfo, Toast.LENGTH_LONG).show();
public void onGetPoiDetailResult(PoiDetailResult result) {
// 获取Place详情页检索结果
&span style=&color:#ff6666;&&// 第三步,设置POI检索监听者;&/span&
poiSearch.setOnGetPoiSearchResultListener(poiListener);
// 这是范围
LatLngBounds.Builder b = new LatLngBounds.Builder();
b.include(new LatLng(39.6.320331));
b.include(new LatLng(39.6.32838));
b.include(new LatLng(39.6.491081));
b.include(new LatLng(39.6.475558));
LatLngBounds build = b.build();&pre name=&code& class=&java&&获得LatLngBounds // 第四步,发起检索请求; poiSearch.searchInBound(new PoiBoundSearchOption().bound(build).keyword(&美食&).pageNum(currentPage));
下面使用一个简单的例子来进行说明
里面只有一个简单的listview
&RelativeLayout xmlns:android=&/apk/res/android&
xmlns:tools=&/tools&
android:layout_width=&match_parent&
android:layout_height=&match_parent&
tools:context=&.IndexActivity& &
android:id=&@+id/mylistView&
android:layout_width=&wrap_content&
android:layout_height=&wrap_content& /&
&/RelativeLayout&
在activity中实现对数据的获取和展示工作,在例子中为了方便演示,监听了onKeyDown方法,修改了meun按键的相应操作,修改为按下menu键显示数据再次点击显示下页数据。这里仅展示“美食”搜索结果仅展示“美食名”。
package com.flyou.
import java.util.L
import android.app.A
import android.os.B
import android.view.KeyE
import android.widget.ArrayA
import android.widget.ListV
import android.widget.T
import com.baidu.mapapi.SDKI
import com.baidu.mapapi.model.LatL
import com.baidu.mapapi.model.LatLngB
import com.baidu.mapapi.search.core.CityI
import com.baidu.mapapi.search.core.PoiI
import com.baidu.mapapi.search.core.SearchR
import com.baidu.mapapi.search.poi.OnGetPoiSearchResultL
import com.baidu.mapapi.search.poi.PoiBoundSearchO
import com.baidu.mapapi.search.poi.PoiDetailR
import com.baidu.mapapi.search.poi.PoiR
import com.baidu.mapapi.search.poi.PoiS
public class POISearchMapActivity extends Activity {
private int totalpages = 0;
private ListView listView =
private PoiSearch poiS
private int currentPage = 0;
ArrayAdapter&String& adapter =
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 在使用SDK各组件之前初始化context信息,传入ApplicationContext
// 注意该方法要再setContentView方法之前实现
SDKInitializer.initialize(getApplicationContext());
setContentView(R.layout.activity_index);
listView = (ListView) findViewById(R.id.mylistView);
// 第一步,创建POI检索实例
poiSearch = PoiSearch.newInstance();
// 第三步,设置POI检索监听者;
poiSearch.setOnGetPoiSearchResultListener(poiListener);
// 第二步,创建POI检索监听者;
OnGetPoiSearchResultListener poiListener = new OnGetPoiSearchResultListener() {
public void onGetPoiResult(PoiResult result) {
// 获取POI检索结果
if (result == null || result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND) {
Toast.makeText(POISearchMapActivity.this, &未找到结果&, Toast.LENGTH_LONG).show();
if (result.error == SearchResult.ERRORNO.NO_ERROR) {
List&PoiInfo& allPoi = result.getAllPoi();
totalpages = result.getTotalPageNum();
String[] datas = new String[10];
for (int i = 0; i & 10; i++) {
datas[i] = allPoi.get(i).
Toast.makeText(POISearchMapActivity.this, &当前第&+currentPage+&页,共&+result.getTotalPageNum()+&页&, 0).show();
adapter = new ArrayAdapter&String&(POISearchMapActivity.this, android.R.layout.simple_list_item_1, datas);
listView.setAdapter(adapter);
if (result.error == SearchResult.ERRORNO.AMBIGUOUS_KEYWORD) {
// 当输入关键字在本市没有找到,但在其他城市找到时,返回包含该关键字信息的城市列表
String strInfo = &在&;
for (CityInfo cityInfo : result.getSuggestCityList()) {
strInfo += cityInfo.
strInfo += &,&;
strInfo += &找到结果&;
Toast.makeText(POISearchMapActivity.this, strInfo, Toast.LENGTH_LONG).show();
public void onGetPoiDetailResult(PoiDetailResult result) {
// 获取Place详情页检索结果
protected void onDestroy() {
super.onDestroy();
// 在activity执行onDestroy时执行mMapView.onDestroy(),实现地图生命周期管理
poiSearch.destroy();
protected void onResume() {
super.onResume();
// 在activity执行onResume时执行mMapView. onResume (),实现地图生命周期管理
protected void onPause() {
super.onPause();
// 在activity执行onPause时执行mMapView. onPause (),实现地图生命周期管理
public boolean onKeyDown(int keyCode, KeyEvent event) {
switch (keyCode) {
case KeyEvent.KEYCODE_MENU:
// 这是范围
LatLngBounds.Builder b = new LatLngBounds.Builder();
b.include(new LatLng(39.6.320331));
b.include(new LatLng(39.6.32838));
b.include(new LatLng(39.6.491081));
b.include(new LatLng(39.6.475558));
LatLngBounds build = b.build();
// 第四步,发起检索请求;
poiSearch.searchInBound(new PoiBoundSearchOption().bound(build).keyword(&美食&).pageNum(currentPage));
// poiSearch.searchInCity((new
// PoiCitySearchOption()).city(&开封&).keyword(&美食&).pageNum(currentPage));
currentPage++;
if (currentPage == totalpages) {
currentPage = 0;
return super.onKeyDown(keyCode, event);
3.城市搜索
poiSearch.searchInCity((new
PoiCitySearchOption()).city(&开封&).keyword(&美食&).pageNum(currentPage));
4.周边搜索
(java.lang.String&key)
检索关键字
(&location)
(int&pageCapacity)
设置每页容量,默认为每页10条
(int&pageNum)
(int&radius)
设置检索的半径范围----------单位为米
poiSearch.searchNearby(new PoiNearbySearchOption()
.keyword(&大学&)
.location(new LatLng(34.496))
.radius(3000)
);//可以为定位的坐标也可自己制定
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:83948次
积分:1992
积分:1992
排名:第13840名
原创:112篇
转载:24篇
评论:37条
(1)(2)(1)(6)(6)(12)(20)(1)(5)(8)(26)(7)(13)(11)(17)百度地图检索功能参数无效,求指教_百度地图api吧_百度贴吧
&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&签到排名:今日本吧第个签到,本吧因你更精彩,明天继续来努力!
本吧签到人数:0成为超级会员,使用一键签到本月漏签0次!成为超级会员,赠送8张补签卡连续签到:天&&累计签到:天超级会员单次开通12个月以上,赠送连续签到卡3张
关注:5,239贴子:
百度地图检索功能参数无效,求指教收藏
mPoiSearch.searchNearby((new PoiNearbySearchOption()).keyword(&大厦&).radius(10)
.location(ll).pageNum(1).pageCapacity(15));这样的确能搜索到,但是无论我怎么设置半径,搜索到的数据都是一样的
登录百度帐号推荐应用
为兴趣而生,贴吧更懂你。或Android(1)
Android 百度地图
转载请注明出处:
最近在研究百度地图API,通过POI搜索时发现每次只能显示10个覆盖物,强迫症不能忍啊有木有,官方demo并不能满足我的需求,研究了一段时间实现了显示所有覆盖物的功能。
来,看图(= =这回要犯密集恐惧症了)
下面来看看代码是如何实现的
map.xml 很简单,就一个MapView控件
&?xml version=&1.0& encoding=&utf-8&?&
&RelativeLayout xmlns:android=&/apk/res/android&
android:layout_width=&match_parent&
android:layout_height=&match_parent& &
&com.baidu.mapapi.map.MapView
android:id=&@+id/mapView&
android:layout_width=&fill_parent&
android:layout_height=&fill_parent&
android:clickable=&true& /&
&/RelativeLayout&
* 演示poi搜索功能
public class PoiSearchDemo extends Activity {
/** 百度地图 */
private MapView mapV
/** 定义 BaiduMap 地图对象的操作方法与接口 */
private BaiduMap baiduM
private PoiSearch poiS
/** 搜索关键词 */
private final String keyword = &餐厅&;
/** 每页容量50 */
private final int pageCapacity = 50;
/** 第一页 */
private final int pageNum = 0;
/** 搜索半径10km */
private final int radius = 10000;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
initView();
initData();
private void initView() {
mapView = (MapView) findViewById(R.id.mapView);
// 是否显示自带缩放控件
mapView.showZoomControls(false);
private void initData() {
baiduMap = mapView.getMap();
// 初始化搜索模块
poiSearch = PoiSearch.newInstance();
// 注册搜索事件监听
.setOnGetPoiSearchResultListener(new PoiSearchResultListener());
// 搜索该坐标附近的餐厅
poiSearch.searchNearby(new PoiNearbySearchOption().keyword(keyword)
.location(new LatLng(39.6.327764))
.pageCapacity(pageCapacity).pageNum(pageNum).radius(radius));
protected void onPause() {
mapView.onPause();
super.onPause();
protected void onResume() {
mapView.onResume();
super.onResume();
protected void onDestroy() {
// 销毁搜索模块
poiSearch.destroy();
// 关闭定位图层
baiduMap.setMyLocationEnabled(false);
mapView.onDestroy();
super.onDestroy();
private class PoiSearchResultListener implements
OnGetPoiSearchResultListener {
public void onGetPoiDetailResult(PoiDetailResult result) {
if (result.error != SearchResult.ERRORNO.NO_ERROR) {
Toast.makeText(getApplicationContext(),
result.getName() + &: & + result.getAddress(), 0)
public void onGetPoiResult(PoiResult result) {
if ((result == null)
|| (result.error == SearchResult.ERRORNO.RESULT_NOT_FOUND)) {
if (result.error == SearchResult.ERRORNO.NO_ERROR) {
baiduMap.clear();
&span style=&background-color: rgb(255, 255, 153);&&MyPoiOverlay overlay = new MyPoiOverlay(baiduMap);&/span&
baiduMap.setOnMarkerClickListener(overlay);
overlay.setData(result);
overlay.addToMap();
// 缩放地图,使所有Overlay都在合适的视野内
overlay.zoomToSpan();
if (result.error == SearchResult.ERRORNO.AMBIGUOUS_KEYWORD) {
}&/pre&&pre name=&code& class=&java&&&span style=&white-space:pre&& &/span&private class MyPoiOverlay extends OverlayManager {...}
这里与官方的demo很相似,不同的是MyPoiOverlay这个类
MyPoiOverlay
private class MyPoiOverlay extends OverlayManager {
private PoiResult poiResult =
public MyPoiOverlay(BaiduMap baiduMap) {
super(baiduMap);
public void setData(PoiResult poiResult) {
this.poiResult = poiR
public boolean onMarkerClick(Marker marker) {
if (marker.getExtraInfo() != null) {
int index = marker.getExtraInfo().getInt(&index&);
PoiInfo poi = poiResult.getAllPoi().get(index);
// 详情搜索
poiSearch.searchPoiDetail((new PoiDetailSearchOption())
.poiUid(poi.uid));
public List&OverlayOptions& getOverlayOptions() {
if ((this.poiResult == null)
|| (this.poiResult.getAllPoi() == null))
ArrayList&OverlayOptions& arrayList = new ArrayList&OverlayOptions&();
for (int i = 1; i & this.poiResult.getAllPoi().size(); i++) {
if (this.poiResult.getAllPoi().get(i).location == null)
// 给marker加上标签
Bundle bundle = new Bundle();
bundle.putInt(&index&, i);
arrayList.add(new MarkerOptions()
.icon(BitmapDescriptorFactory
.fromBitmap(setNumToIcon(i))).extraInfo(bundle)
.position(this.poiResult.getAllPoi().get(i).location));
return arrayL
* 往图片添加数字
private Bitmap setNumToIcon(int num) {
BitmapDrawable bd = (BitmapDrawable) getResources().getDrawable(
R.drawable.icon_gcoding);
Bitmap bitmap = bd.getBitmap().copy(Bitmap.Config.ARGB_8888, true);
Canvas canvas = new Canvas(bitmap);
Paint paint = new Paint();
paint.setColor(Color.WHITE);
paint.setAntiAlias(true);
int widthX;
int heightY = 0;
if (num & 10) {
paint.setTextSize(30);
widthX = 8;
heightY = 6;
paint.setTextSize(20);
widthX = 11;
canvas.drawText(String.valueOf(num),
((bitmap.getWidth() / 2) - widthX),
((bitmap.getHeight() / 2) + heightY), paint);
思路如下:
这里通过继承OverlayManager这个类,改写了getOverlayOptions()和onMarkerClick()这两个方法通过官方demo中给出的icon_gconding,在代码中给图片加上数字,达到需要的效果每次搜索最多只能设置50,另外Bitmap过多会导致OOM。
最后说下,第一次写博客(怎么装成经常写的样子?= =),有什么说的不对的地方请告诉我。
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:1698次
排名:千里之外}

我要回帖

更多关于 高德 nearbysearch 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信