搜索服务
【Navinfo Navigation SDK for Android】向用户提供总计包含千万级别的POI(Point of Interest,兴趣点)搜索服务,搜索服务同时在线和离线搜索功能。目前,提供的POI搜索方式包括:关键字搜索、周边搜索、类型搜索、沿路搜索等。
1. 关键字搜索方法为PoiSearch.searchWithKeyword;
2. 周边搜索方法为PoiSearch.forceNearBy(true),设置强制使用周边搜索,然后在调用PoiSearch.searchWithKeyword完成搜索;
3. 沿路搜索方法为PoiSearch.searchAlongRoute。使用搜索方法前需要初始化设置搜索引擎,初始化内容包括:关键字搜索使用的内存缓冲区的大小,返回的POI数量,周边搜索的覆盖范围等;
4. 沿路搜索需要有导航模块支持。
(完整代码详见相关资源下载中示例工程的NewSearchActivity.java)
/**
* 初始化需要使用的引擎 这里主要使用了POI搜索引擎和绘图引擎
*/
private void init () {
……
// 初始化POI搜索引擎
mPoiSearch = new PoiSearch();
// 设置搜索模式:
// onlineOnly在线搜索 offlineOnly离线搜索
mPoiSearch.setDataPreference(DataPreference.onlineOnly);
// 设置搜索城市id
mPoiSearch.setCity(cityName);
// 设置搜索中心点
mPoiSearch.setCenter(mCenter); // 注册搜索监听回调方法
mPoiSearch.setListener(new PoiSearch.Listener() {
@Override
public void onPoiSearch(int event) {
Log.d("search", event + "");
switch (event) {
case PoiSearch.Event.queryFinished:
Logger.i("luke finished event revieved");
break;
case PoiSearch.Event.pageLoaded:
try {
// 情况之前的气泡
clearAnnotation();
mPoiItems.clear();
// 例如搜索到3页,poisearch中包含1 - 3页的所有信息,所以需要计算出第三页的位置进行获取
boolean flag1 = mPoiSearch.getTotalPoiItemSum() <= (mPageIndex + 1) * mPoiSearch.getPageSize();
int maxIndex = flag1 ? mPoiSearch.getTotalPoiItemSum() : (mPageIndex + 1) * mPoiSearch.getPageSize();
// 获取当前load页的数量
int index = mPoiSearch.getCurrentPoiNum();
if (index > 0) {
int poiNumbers = mPoiSearch.getItemNum(BaseItem.ItemType.poi);
if (poiNumbers > 0) {
if (poiNumbers == 1) {
PoiItem poiItem = (PoiItem) mPoiSearch.getItemByIndex(BaseItem.ItemType.poi, 0);
// 如果是行政区则跳转到行政区
if (poiItem.type == PoiItem.ItemType.region) {
RegionPoiItem regionPoiItem = (RegionPoiItem) poiItem;
mDemoMapView.setCarPosition(regionPoiItem.center);
mPoiSearch.setCity(regionPoiItem.completeName);
mPoiSearch.setCenter(regionPoiItem.center);
return;
}
}
}
}
}
……
}
}
// 地图上打点
private void drawAnnotation() {
mAnnotations.clear();
Vector2DF vec = new Vector2DF(0.9f, 0.5f);
Point point = new Point();
int zlevel = 100;
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.gas);
for (int i = 0; i < mPoiItems.size(); i++) {
point.x = mPoiItems.get(i).position.x;
point.y = mPoiItems.get(i).position.y;
CustomAnnotation annot = new CustomAnnotation(zlevel, point, i, vec, icon);
annot.setTitle(mPoiItems.get(i).name);
mAnnotations.add(annot);
}
for (int i = 0; i < mAnnotations.size(); i++) {
mDemoMapView.getMapRenderer().addAnnotation(mAnnotations.get(i));
}
if (mPoiItems.size() > 0) {
// 移动地图中心
mDemoMapView.setWorldCenter(mPoiItems.get(0).position);
}
}
// 删除地图上打的点
private void clearAnnotation() {
for (int i = 0; i < mAnnotations.size(); i++) {
mDemoMapView.getMapRenderer().removeAnnotation(mAnnotations.get(i));
}
}
}
运行后搜索操作界面:
点击搜索按钮返回结果:
