{"version":"0.1.0","code":"0000","result":true,"message":"处理成功","errdetail":"","timestamp":1671509003292,"data":{"id":71714048,"title":"18.3 关键字搜索","slug":"uxb0cw","format":"lake","bookId":26046811,"body":null,"body_draft":null,"body_html":"
关键字搜索有5种不同的查询类型,每种查询分别对应一个独立的API,具体如下:
用户在主图点击去这里button, 进入搜索页后,在顶部搜索框输入搜索关键字进行——框搜索。分为在线和离线2种状态。
1)用户在主图点击去这里button,进入搜索首页,点击搜索框进入搜索输入页,输入关键字,发起搜索。
2)在搜索首页为输入状态下,搜索框内有文案提示“搜索目的地”。
3) 点击搜索框,如果还未输入关键字,搜索框内有文案提示“请输入目的地”,弹出键盘。
4)支持搜索类型:中文、首拼、拼音、英文、数字、poi、交叉道路,道路、地址、品牌、类别、tag、电话号码、错别字、同义词、城市名等。
5) 搜索框输入城市区域名,是直接跳转到地图主页,切换到该城市中心,图面切换之后,toast提示“已切换至XXX”。搜索省份时,切换至省会城市图面中心。
离线场景下,可通过城市列表选择搜索区域,支持选择区域为全省或者城市,选择之后将在选中的省份或者城市范围内进行搜索。
1) 支持输入简体中文、拼音或者首字母,搜索省份或者城市名。
2) 展示顺序:常用城市->附近城市->直辖市->省份。
3) 常用城市,最多显示4个,当前用户无搜索历史记录时,北上广深四个城市填充。
4) 附近城市,最多显示4个,取用户最近切换城市,不显示定位所在城市。
5) 省份及城市按首字母排序。
6) 附近城市、直辖市、省份内城市,默认全部收起,点击右侧下拉按钮,展开显示其中内容,再点击收起,只能点击展现其中一个。
7) 点击城市切换按钮,切换城市后,底部地图不需要随之切换到相应城市。
com.autonavi.gbl.search.model.KeywordSearchTQueryParam
Coord2DDouble com.autonavi.gbl.search.model.KeywordSearchTQueryParam.userLoc
Coord2DDouble com.autonavi.gbl.search.model.KeywordSearchTQueryParam.poiLoc
如果是选择的poi,那么赋值poi的经纬度,如果没选择poi,那么赋值地图中心点的经纬度。
Coord2DDouble com.autonavi.gbl.search.model.SearchKeywordParam.geoobj
geoobj参数(图面对角线坐标点),包含图面城市信息时,会提权,影响城市选择和排序。
String com.autonavi.gbl.search.model.KeywordSearchTQueryParam.keywords
支持多个关键字,在线搜索采用分隔符:|表示或,空格表示与,双引号表示不可分割;离线搜索不支持多个关键字。
com.autonavi.gbl.search.model.KeywordSearchIdqParam
String com.autonavi.gbl.search.model.KeywordSearchIdqParam.id
com.autonavi.gbl.search.model.KeywordSearchRqbxyParam
Coord2DDouble com.autonavi.gbl.search.model.KeywordSearchRqbxyParam.poiLoc
String com.autonavi.gbl.search.model.KeywordSearchRqbxyParam.keywords
支持多个关键字,在线搜索采用分隔符:|表示或,空格表示与,双引号表示不可分割;离线搜索使用半角分号分隔。
String com.autonavi.gbl.search.model.KeywordSearchRqbxyParam.geoObj
com.autonavi.gbl.search.model.KeywordSearchSpqParam
String com.autonavi.gbl.search.model.KeywordSearchSpqParam.geoObj
String com.autonavi.gbl.search.model.KeywordSearchSpqParam.keywords
支持多个关键字,在线搜索采用分隔符:|表示或,空格表示与,双引号表示不可分割;离线搜索使用半角分号分隔。
Coord2DDouble com.autonavi.gbl.search.model.KeywordSearchSpqParam.userLoc
Coord2DDouble com.autonavi.gbl.search.model.KeywordSearchSpqParam.poiLoc
离线搜时需要设置,表示搜索参考点。
com.autonavi.gbl.search.model.SearchMode.SearchMode1
取值 | 描述 | 备注 |
SEARCH_MODE_ONLINE_ADVANCED | 在线优先 | 当在线搜索出现异常,会自动转离线搜索 |
SEARCH_MODE_OFFLINE_ADVANCED | 离线优先 | 若当前网络是wifi,会切到在线优先方式 |
SEARCH_MODE_ONLINE_ONLY | 仅在线 | |
SEARCH_MODE_OFFLINE_ONLY | 仅离线 |
taskid外部需自己保证全局唯一
com.autonavi.gbl.search.observer.IKeyWordSearchObserverV2
com.autonavi.gbl.search.model.KeywordSearchResultV2
pstResult关键信息如下所示,更多返回信息可参考SearchKeywordResult结构的成员描述。
int com.autonavi.gbl.search.model.KeywordSearchResultV2.poiType
int com.autonavi.gbl.search.model.KeywordSearchResultV2.total
com.autonavi.gbl.search.model.SearchPoiLocRes
如搜“北京”,返回北京POI点信息,上层可以根据该信息将地图显示切换到北京。
com.autonavi.gbl.search.model.SearchClassifyInfo
该字段数据返回一系列筛选参数,比如:搜索“美食”,将下发范围筛选、美食分类筛选、排序筛选数据。详情参考文档:关键字搜索结果筛选
ArrayList<SearchPoiInfo> com.autonavi.gbl.search.model.KeywordSearchResultV2.poiList
com.autonavi.gbl.search.model.SearchPoiInfo.basicInfo
com.autonavi.gbl.search.model.SearchPoiInfo.rankInfo
com.autonavi.gbl.search.model.SearchPoiInfo.parkingInfo
com.autonavi.gbl.search.model.SearchPoiInfo.gasInfo
com.autonavi.gbl.search.model.SearchPoiInfo.chargingStationInfo
com.autonavi.gbl.search.model.SearchPoiInfo.hotelInfo
com.autonavi.gbl.search.model.SearchPoiInfo.indoorInfo
com.autonavi.gbl.search.model.SearchPoiInfo.childInfoList
com.autonavi.gbl.search.model.SearchPoiInfo.buslineInfo
com.autonavi.gbl.search.model.SearchPoiInfo.floorList
com.autonavi.gbl.search.model.SearchPoiInfo.hospitalTel
com.autonavi.gbl.search.model.SearchPoiInfo.eventInfoList
com.autonavi.gbl.search.model.SearchPoiInfo.offlineInfo
com.autonavi.gbl.search.model.SearchPoiInfo.photoInfo
int
euRet为通用的返回错误码,可详见:
com.autonavi.gbl.util.errorcode.common.Service,
com.autonavi.gbl.util.errorcode.common.ThirdParty,
com.autonavi.gbl.util.errorcode.search.Online,
com.autonavi.gbl.util.errorcode.search.Offline 类型。
int com.autonavi.gbl.search.SearchServiceV2.keyWordSearchTQuery(KeywordSearchTQueryParam param, IKeyWordSearchObserverV2 observer, @SearchMode.SearchMode1 int mode, int taskid)
int com.autonavi.gbl.search.SearchServiceV2.keyWordSearchIdq(KeywordSearchIdqParam param, IKeyWordSearchObserverV2 observer, @SearchMode.SearchMode1 int mode, int taskid)
int com.autonavi.gbl.search.SearchServiceV2.keyWordSearchRqbxy(KeywordSearchRqbxyParam param, IKeyWordSearchObserverV2 observer, @SearchMode.SearchMode1 int mode, int taskid
int com.autonavi.gbl.search.SearchServiceV2.keyWordSearchSpq(KeywordSearchSpqParam param, IKeyWordSearchObserverV2 observer, @SearchMode.SearchMode1 int mode, int taskid)
备注:taskid为搜索发起请求的唯一任务id,由客户端分配,用于标识发起请求,客户端可根据此id调用中断接口,中断搜索请求,详见中断请求描述。
void com.autonavi.gbl.search.observer.IKeyWordSearchObserverV2.onGetKeyWordResult(int taskid, int euRet, KeywordSearchResultV2pstResult)
备注1:发起请求为异步接口,结果通过pstResult返回,返回状态参见euRet。
备注2:请注意observer的生命周期,搜索完成或者手动取消之前,请确保观察者不被释放。
说明:函数详情,请复制函数名称到在线API搜索。
// 获取自车位当前经纬度\nCoord2DDouble getCarLocation()\n{\n Coord2DDouble carPos; // 自车位所在2D经度、纬度\n \n MapService mapService = ServiceMgr.getBLService(SingleServiceID.MapSingleServiceID);\n MapView mapView = mapService.getMapView(MapEngineID.MapEngineIdMain);\n BizControlService bizControlService = ServiceMgr.getBLService(SingleServiceID.BizControlSingleServiceID);\n BizCarControl bizCarControl = bizControlService.getBizCarControl(mapView); // 获取车标类图层控制 com.autonavi.gbl.layer.BizCarControl\n \n CarLoc carLoc = bizCarControl.getCarPosition();\n if (carLoc.vecPathMatchInfo.size() > 0)\n {\n carPos.lon = vecPathMatchInfo.get(0).longitude;\n carPos.lat = vecPathMatchInfo.get(0).latitude;\n }\n\n return carPos;\n}
/* 获取地图中心点坐标 */\nCoord2DDouble getMapCenter()\n{\n Coord2DDouble mapCenter;\n\n MapService mapService = ServiceMgr.getBLService(SingleServiceID.MapSingleServiceID);\n MapView mapView = mapService.getMapView(MapEngineID.MapEngineIdMain);\n OperatorPosture operatorPosture = mapView.getOperatorPosture(); // 获取地图姿态操作接口, com.autonavi.gbl.map.OperatorPosture\n Coord3DDouble mapCenter3D;\n mapCenter3D = operatorPosture.getMapCenterByLonLat();\n\n mapCenter.lon = mapCenter3D.lon;\n mapCenter.lat = mapCenter3D.lat;\n\n return mapCenter;\n}
/* 获取对角线坐标字符串 */\nString getGeoObj(Coord2DDouble carPos)\n{\n // 1. 获取POI点的搜索区域范围\n /* 自车位经纬度转地图P20坐标 */\n\tMapService mapService = ServiceMgr.getBLService(SingleServiceID.MapSingleServiceID);\n MapView mapView = mapService.getMapView(MapEngineID.MapEngineIdMain);\n OperatorPosture operatorPosture = mapView.getOperatorPosture(); // 获取地图姿态操作接口, com.autonavi.gbl.map.OperatorPosture\n \n double mapX = OperatorPosture.lonLatToMap(carPos.lon, carPos.lat).lon;\n double mapY = OperatorPosture.lonLatToMap(carPos.lon, carPos.lat).lat;\n\n /* 获取当前显示的地图范围 */\n MapBound mapBound = operatorPosture.getMapBound();\n int width = mapBound.width / 2;\n int height = mapBound.height / 2;\n\n RectInt mapRect = new RectInt();\n mapRect.left = mapX - width;\n mapRect.right = mapX + width;\n mapRect.top = mapY - height;\n mapRect.bottom = mapY + height;\n\n // 2. 将显示区域转换为对角线坐标字符串\n Coord2DDouble leftTop = operatorPosture.mapToLonLat(mapRect.left, mapRect.top);\n Coord2DDouble bottomRight = operatorPosture.mapToLonLat(mapRect.right, mapRect.bottom);\n\n String geoobj = "";\n geoobj += leftTop.lon + "|" + leftTop.lat + "|" + bottomRight.lon + "|" + bottomRight.lat;\n\n return geoobj;\n}
String getGeoObj4SPQ()\n{\n MapService mapService = ServiceMgr.getBLService(SingleServiceID.MapSingleServiceID);\n MapView mapView = mapService.getMapView(MapEngineID.MapEngineIdMain);\n OperatorPosture operatorPosture = mapView.getOperatorPosture();\n \n // 使用 MapBound com.autonavi.gbl.map.OperatorPosture.getMapBound() 获取地图范围\n\tMapBound mapBound = operatorPosture.getMapBound();\n \n // 2. 将显示区域转换为对角线坐标字符串\n double geoblLeft = mapBound.left;\n double geoblTop = mapBound.top;\n double geoblRight = mapBound.left + mapBound.width;\n double geoblBottom = mapBound.top + mapBound.height;\n \n String geoobj = String("");\n geoobj += geoblLeft + "|" + geoblTop + "|" + geoblRight + "|" + geoblBottom;\n\n return geoobj;\n}
class SearchKeyWordObserver implements IKeyWordSearchObserverV2 {\n @Override\n public void onGetKeyWordResult(int taskid, int euRet, KeywordSearchResultV2 pstResult) {\n\n Log.i(TAG, "onGetKeyWordResult: ");\n Log.i(TAG, " taskid = " + taskid);\n Log.i(TAG, " euRet = " + euRet);\n if (stResult != null) {\n Log.i(TAG, " message = " + stResult.message);\n Log.i(TAG, " keyword = " + stResult.keyword);\n\n if (stResult.poiList != null && stResult.poiList.length > 0) {\n if (stResult.poiList[0].poi != null) {\n Log.i(TAG, " address = " + stResult.poiList[0].basicInfo.address);\n Log.i(TAG, " poiid = " + stResult.poiList[0].basicInfo.poiId);\n Log.i(TAG, " name = " + stResult.poiList[0].basicInfo.name);\n }\n }\n }\n }\n}
public class SearchRequest\n{\n // 请求函数\n\tpublic void doTQUERYKeywordSearch()\n {\n // 1. 获取自车位所在经纬度\n Coord2DDouble carPos = getCarLocation();\n \n // 2. 通过经纬度获取自车位所在城市编码\n MapDataService mapDataService = getMapDataService(); // 获取地图数据服务,com.autonavi.gbl.data.MapDataService\n int adCodeCarLoc = mapDataService.getAdcodeByLonLat(carPos.lon, carPos.lat);\n \n // 3. 计算搜索中心poiLoc以及搜索城市编码adCode\n Coord2DDouble poiLoc;\n int adCode;\n // 获取当前地图中心点坐标\n Coord2DDouble mapCenter = getMapCenter();\n \n int adCodeMapCenter;\n if (false == IsNetConnected) // 是否联网\n {\n /**< 离线时从当前搜索城市信息中获取adcode,例如选择在北京市离线搜索 */\n adCodeMapCenter = getOfflineSearchAdcode();\n }\n else\n {\n adCodeMapCenter = mapDataService.getAdcodeByLonLat(mapCenter.lon, mapCenter.lat);\n }\n\n if ((adCodeCarLoc == adCodeMapCenter) || (adCodeMapCenter<0))\n {\n /**< 自车位与图面中心点所在城市为同一城市或无法获取地图中心点adcode时, 框搜以自车位为搜索中心点 */\n poiLoc = carLoc;\n adCode = adCodeCarLoc\n }\n else\n {\n /**< 自车位与图面中心点所在城市不为同一城市, 在图面中心点发起周边搜 */\n poiLoc = mapCenter;\n adCode = adCodeMapCenter;\n }\n \n // 4. 构建参数\n KeywordSearchTQueryParam param = new KeywordSearchTQueryParam();\n param.keywords = "天安门"; // 替换为输入框输入的搜索关键字\n param.offlineParam.adcode = adcode;\n param.city = ""+adcode;\n /* 搜索中心点坐标 */\n param.poiLoc = poiLoc;\n /* 自车位坐标 */\n param.userLoc = carPos;\n param.searchOperate = 1; // 框搜\n param.geoObj = getGeoObj(poiLoc);\n param.pageParam.pageSize = 10;\n\t\tparam.offlineParam.resultMaxCount = 200;\n \n // 5. 回调观察者\n SearchKeyWordObserver searchKeywordObserver = new SearchKeyWordObserver();\n \n // 6. 生成唯一的任务id\n taskId = 1;\n \n // 发起关键字搜索\n\t\tint iret = SearchServiceV2.keyWordSearchTQuery(param, searchKeywordObserver, SEARCH_MODE_ONLINE_ADVANCED, taskId);\n }\n}
public class SearchRequest\n{\n // 请求函数\n\tpublic void doIDQKeywordSearch()\n {\n // 1. 构建参数\n KeywordSearchIdqParam param = new KeywordSearchIdqParam();\n param.id = "B000A60DA1"; // 替换为需要搜索的POI的POIID\n // 获取自车位所在经纬度\n param.userLoc = getCarLocation();\n // 通过点击POI扎标进行IDQ时,param.poi_loc 的值可以通过获取 com.autonavi.gbl.map.model.MapLabelItem 的p20坐标,再将其转换为经纬度\n param.poiLoc.lon = 116.397604;\n param.poiLoc.lat = 39.907694;\n \n // 2. 回调观察者\n SearchKeyWordObserver searchKeywordObserver = new SearchKeyWordObserver();\n \n // 3. 生成唯一的任务id\n taskId = 1;\n \n // 4. 发起关键字搜索\n\t\tint iret = SearchServiceV2.keyWordSearchIdq(param, searchKeywordObserver, SEARCH_MODE_ONLINE_ADVANCED, taskId);\n }\n}
public class SearchRequest\n{\n // 请求函数\n\tpublic void doRQBXYKeywordSearch()\n {\n // 1. 构建参数\n KeywordSearchRqbxyParam param = new KeywordSearchRqbxyParam();\n // 设置周边搜索的中心经纬度\n param.poiLoc.lon = 116.397604;\n param.poiLoc.lat = 39.907694;\n \n param.keywords = "美食"; // 替换为输入框输入的搜索关键字\n \n // 通过经纬度获取城市编码\n MapDataService mapDataService = getMapDataService(); // 获取地图数据服务,com.autonavi.gbl.data.MapDataService\n param.offlineParam.adcode = mapDataService.getAdcodeByLonLat(carPos.lon, carPos.lat);\n param.city = String("")+adcode;\n\n param.searchOperate = 2;\n \n // 2. 回调观察者\n SearchKeyWordObserver searchKeywordObserver = new SearchKeyWordObserver();\n \n // 3. 生成唯一的任务id\n taskId = 1;\n \n // 4. 发起关键字搜索\n\t\tint iret = SearchServiceV2.keyWordSearchRqbxy(param, searchKeywordObserver, SEARCH_MODE_ONLINE_ADVANCED, taskId);\n }\n}
public class SearchRequest\n{\n // 请求函数\n\tpublic void doSPQKeywordSearch()\n {\n // 1. 构建参数\n KeywordSearchSpqParam param = new KeywordSearchSpqParam();\n param.userLoc = getCarLocation();\n param.geoObj = getGeoObj4SPQ();\n param.keywords = "美食"; // 替换为输入框输入的搜索关键字\n \n // 通过经纬度获取城市编码\n MapDataService mapDataService = getMapDataService(); // 获取地图数据服务,com.autonavi.gbl.data.MapDataService\n param.offlineParam.adcode = mapDataService.getAdcodeByLonLat(carPos.lon, carPos.lat);\n param.city = ""+adcode;\n\n param.searchOperate = 1;\n \n // 2. 回调观察者\n SearchKeyWordObserver searchKeywordObserver = new SearchKeyWordObserver();\n \n // 3. 生成唯一的任务id\n taskId = 1;\n \n // 4. 发起关键字搜索\n\t\tint iret = SearchServiceV2.keyWordSearchSpq(param, searchKeywordObserver, SEARCH_MODE_ONLINE_ADVANCED, taskId);\n }\n}
// 获得搜索服务实例\ntaskId = 1;\nSearchService.abort(taskId);