{"version":"0.1.0","code":"0000","result":true,"message":"处理成功","errdetail":"","timestamp":1671508879230,"data":{"id":71712099,"title":"7.3 关键字搜索","slug":"ad6exg","format":"lake","bookId":26046811,"body":null,"body_draft":null,"body_html":"
关键字搜索有不同的查询类型,不同类型的query_type值不一样。
备注:关键字搜索、深度信息搜索都会下发POI深度信息,深度信息搜索接口主要用于加油站、充电站、停车场等特殊POI的深度信息下发,其余POI深度信息使用关键字搜索下发。具体请查看关键字搜索、深度信息搜索结果字段,按需请求。
用户在主图点击去这里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.SearchKeywordParam
String com.autonavi.gbl.search.model.SearchKeywordParam.query_type
使用 TQUERY 时,该参数必须设置为"TQUERY"。
Coord2DDouble com.autonavi.gbl.search.model.SearchKeywordParam.user_loc
Coord2DDouble com.autonavi.gbl.search.model.SearchKeywordParam.poi_loc
如果是选择的poi,那么赋值poi的经纬度,如果没选择poi,那么赋值地图中心点的经纬度。
Coord2DDouble com.autonavi.gbl.search.model.SearchKeywordParam.geoobj
geoobj参数(图面对角线坐标点),包含图面城市信息时,会提权,影响城市选择和排序。
String com.autonavi.gbl.search.model.SearchKeywordParam.keywords
支持多个关键字,在线搜索采用分隔符:|表示或,空格表示与,双引号表示不可分割;离线搜索不支持多个关键字。
String com.autonavi.gbl.search.model.SearchKeywordParam.query_type
使用 IDQ 时,该参数必须设置为"IDQ"。
String com.autonavi.gbl.search.model.SearchKeywordParam.id
String com.autonavi.gbl.search.model.SearchKeywordParam.query_type
使用 RQBXY 时,该参数必须设置为"RQBXY"。
Coord2DDouble com.autonavi.gbl.search.model.SearchKeywordParam.poi_loc
String com.autonavi.gbl.search.model.SearchKeywordParam.keywords
支持多个关键字,在线搜索采用分隔符:|表示或,空格表示与,双引号表示不可分割;离线搜索使用半角分号分隔。
String com.autonavi.gbl.search.model.SearchKeywordParam.range
在线搜索默认为3000米,离线搜索默认为20KM ,搜索范围有效取值(0, 50km](不包括0), 超出有效范围自动使用默认值
String com.autonavi.gbl.search.model.SearchKeywordParam.query_type
使用 SPQ 时,该参数必须设置为"SPQ"。
String com.autonavi.gbl.search.model.SearchKeywordParam.geoobj
String com.autonavi.gbl.search.model.SearchKeywordParam.keywords
支持多个关键字,在线搜索采用分隔符:|表示或,空格表示与,双引号表示不可分割;离线搜索使用半角分号分隔。
Coord2DDouble com.autonavi.gbl.search.model.SearchKeywordParam.user_loc
Coord2DDouble com.autonavi.gbl.search.model.SearchKeywordParam.poi_loc
离线搜时需要设置,表示搜索参考点。
String com.autonavi.gbl.search.model.SearchKeywordParam.query_type
使用 INDOOR 时,该参数必须设置为"INDOOR"。
String com.autonavi.gbl.search.model.SearchKeywordParam.id
String com.autonavi.gbl.search.model.SearchKeywordParam.floorId
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.IGSearchKeyWordObserver
com.autonavi.gbl.search.model.SearchKeywordResult
pstResult关键信息如下所示,更多返回信息可参考SearchKeywordResult结构的成员描述。
int com.autonavi.gbl.search.model.SearchKeywordResult.iPoiType
int com.autonavi.gbl.search.model.SearchKeywordResult.total
com.autonavi.gbl.search.model.SearchPoiLocRes
如搜“北京”,返回北京POI点信息,上层可以根据该信息将地图显示切换到北京。
com.autonavi.gbl.search.model.SearchPoiClassify
当搜索参数 boolean com.autonavi.gbl.search.model.SearchKeywordParam.is_classify 为 true 时才会下发该字段数据,该字段数据返回一系列筛选参数,比如:搜索“美食”,将下发范围筛选、美食分类筛选、排序筛选数据。详情参考文档:关键字搜索结果筛选
ArrayList<SearchPoi> com.autonavi.gbl.search.model.SearchKeywordResult.poiList
com.autonavi.gbl.search.model.SearchPoiBase
com.autonavi.gbl.search.model.SearchPoi.mTempDataMap 该结果字段是一个HashMap,AutoSDK部分数据以键值对的形式存放在这个字段中下发,上层可以使用poi模板id(PoiTemplateConstant)作为key获取相应的信息,参考文档:模板数据获取。
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.SearchService.keyWordSearch(SearchKeywordParam param, IGSearchKeyWordObserver observer, @SearchMode.SearchMode1 int mode, int taskid)
关键字搜索,包含根据ID搜索(IDQ)、根据关键字搜索(TQUERY),根据经纬度周边搜索(RQBXY),框选搜索(SPQ)。
备注:taskid为搜索发起请求的唯一任务id,由客户端分配,用于标识发起请求,客户端可根据此id调用中断接口,中断搜索请求,详见中断请求描述。
void com.autonavi.gbl.search.observer.IGSearchKeyWordObserver.onGetKeyWordResult(int taskid, int euRet, SearchKeywordResult pstResult)
备注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}
// 建议定义成static类型\nstatic class SearchKeyWordObserver implements IGSearchKeyWordObserver {\n @Override\n public void onGetKeyWordResult(int taskid, int euRet, SearchKeywordResult 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, " result = " + stResult.result);\n Log.i(TAG, " message = " + stResult.message);\n Log.i(TAG, " version = " + stResult.version);\n Log.i(TAG, " timestamp = " + stResult.timestamp);\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].poi.address);\n Log.i(TAG, " poiid = " + stResult.poiList[0].poi.poiid);\n Log.i(TAG, " name = " + stResult.poiList[0].poi.name);\n }\n }\n }\n }\n}\n\n// 回调观察者,这边需要上层管理,不要每次发起搜索都new一个,定义成成员变量管理一个即可\nSearchKeyWordObserver searchKeywordObserver = new SearchKeyWordObserver();
注意:观察者需要上层以内部类形式实现,建议定义成static类型,回调观察者的初始化需要上层管理,不要每次发起搜索都new一个,定义成成员变量管理一个即可。
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 SearchKeywordParam param = new SearchKeywordParam();\n param.query_type = "TQUERY";\n param.keywords = "天安门"; // 替换为输入框输入的搜索关键字\n param.adcode = adcode;\n param.city = ""+adcode;\n /* 搜索中心点坐标 */\n param.poi_loc = poiLoc;\n /* 自车位坐标 */\n param.user_loc = carPos;\n param.search_operate = 1; // 框搜\n param.geoobj = getGeoObj(poiLoc);\n param.pagesize = 10;\n\t\tparam.offlineResultMaxCount = 200;\n param.is_classify = true;\n \n // 5. 生成唯一的任务id\n taskId = 1;\n \n // 发起关键字搜索\n\t\tint iret = SearchService.keyWordSearch(param, searchKeywordObserver, SEARCH_MODE_ONLINE_ADVANCED, taskId);\n }\n}
public class SearchRequest\n{\n // 请求函数\n\tpublic void doIDQKeywordSearch()\n {\n // 1. 构建参数\n SearchKeywordParam param = new SearchKeywordParam();\n param.query_type = "IDQ";\n param.id = "B000A60DA1"; // 替换为需要搜索的POI的POIID\n // 获取自车位所在经纬度\n param.user_loc = getCarLocation();\n // 通过点击POI扎标进行IDQ时,param.poi_loc 的值可以通过获取 com.autonavi.gbl.map.model.MapLabelItem 的p20坐标,再将其转换为经纬度\n param.poi_loc.lon = 116.397604;\n param.poi_loc.lat = 39.907694;\n \n // 2. 生成唯一的任务id\n taskId = 1;\n \n // 3. 发起关键字搜索\n\t\tint iret = SearchService.keyWordSearch(param, searchKeywordObserver, SEARCH_MODE_ONLINE_ADVANCED, taskId);\n }\n}
public class SearchRequest\n{\n // 请求函数\n\tpublic void doRQBXYKeywordSearch()\n {\n // 1. 构建参数\n SearchKeywordParam param = new SearchKeywordParam();\n param.query_type = "RQBXY";\n // 设置周边搜索的中心经纬度\n param.poi_loc.lon = 116.397604;\n param.poi_loc.lat = 39.907694;\n \n param.keywords = "美食"; // 替换为输入框输入的搜索关键字\n \n // 通过经纬度获取城市编码\n MapDataService mapDataService = getMapDataService(); // 获取地图数据服务,com.autonavi.gbl.data.MapDataService\n param.adcode = mapDataService.getAdcodeByLonLat(carPos.lon, carPos.lat);\n param.city = String("")+adcode;\n\n param.search_operate = 2;\n \n // 2. 生成唯一的任务id\n taskId = 1;\n \n // 3. 发起关键字搜索\n\t\tint iret = SearchService.keyWordSearch(param, searchKeywordObserver, SEARCH_MODE_ONLINE_ADVANCED, taskId);\n }\n}
public class SearchRequest\n{\n // 请求函数\n\tpublic void doSPQKeywordSearch()\n {\n // 1. 构建参数\n SearchKeywordParam param = new SearchKeywordParam();\n param.query_type = "SPQ";\n param.user_loc = getCarLocation();\n param.geoobj = getGeoObj4SPQ();\n param.keywords = "美食"; // 替换为输入框输入的搜索关键字\n \n // 通过经纬度获取城市编码\n MapDataService mapDataService = getMapDataService(); // 获取地图数据服务,com.autonavi.gbl.data.MapDataService\n param.adcode = mapDataService.getAdcodeByLonLat(carPos.lon, carPos.lat);\n param.city = ""+adcode;\n\n param.search_operate = 1;\n\n // 2. 生成唯一的任务id\n taskId = 1;\n \n // 3. 发起关键字搜索\n\t\tint iret = SearchService.keyWordSearch(param, searchKeywordObserver, SEARCH_MODE_ONLINE_ADVANCED, taskId);\n }\n}
public class SearchRequest\n{\n // 请求函数\n\tpublic void doSPQKeywordSearch()\n {\n // 1. 构建参数\n SearchKeywordParam param = new SearchKeywordParam();\n param.query_type = "INDOOR";\n param.user_loc = getCarLocation();\n param.id = ""; // 楼层所属建筑的POIID\n param.floorId = "F1;F2"; // 输入楼层ID\n \n // 2. 生成唯一的任务id\n taskId = 1;\n \n // 3. 发起关键字搜索\n\t\tint iret = SearchService.keyWordSearch(param, searchKeywordObserver, SEARCH_MODE_ONLINE_ONLY, taskId);\n }\n}
public class PoiItem\n{\n\tpublic String tag;\t\t\t// 在线tag信息\n public String distance;\t\t// 与当前车位之间的距离\n};\n\nPoiItem getDistance(HashMap<Integer, SearchCommonTemplate> tempDataMap)\n{\n PoiItem item;\n\t\n for (Integer key : tempDataMap.keySet()) {\n\t\tSearchCommonTemplate template = tempDataMap.get(key);\n switch (template.typeId)\n {\n case PoiItemlateTypeId.POI_TEMPLATE_TYPE_TEXT:\n {\n SearchTextTemplate textTemplate = (SearchTextTemplate)template;\n switch (textTemplate.id)\n {\n case PoiTemplateConstant.LIST_DISTANCE:\t// 距离\n {\n item.distance = textTemplate.value;\n break;\n }\n }\n break;\n }\n case PoiItemlateTypeId.POI_TEMPLATE_TYPE_HTML:\n {\n \tSearchHtmlTemplate htmlTemplate = (SearchHtmlTemplate)template;\n switch (htmlTemplate.id)\n {\n case PoiTemplateConstant.LIST_TAG:\t// 标签\n {\n item.tag = htmlTemplate.value; // 返回的值时html标签需要二次解析\n break;\n }\n }\n \tbreak;\n }\n }\n }\n}
// 获得搜索服务实例\ntaskId = 1;\nSearchService.abort(taskId);