更新时间:2026/11/30 15:37
在线场景识别由服务端实时完成。开发者传入当前坐标和关注的场景标签,SDK 自动采集设备周边 WiFi 信息,与坐标一起上报服务端,服务端返回命中的场景及候选 POI。
一、使用前提
已完成 Android Studio 配置 和 隐私合规与初始化。
AK 已开通场景识别服务权限,见 获取密钥。
设备可以联网。
已获得 WiFi 扫描相关权限,且设备 WiFi 开关已打开,见下文"权限要求"。
权限要求
在线识别依赖周边 WiFi 列表,SDK 在发起请求前会检查权限。不满足时不会发送网络请求,直接回调错误。
Android 6.0 以下 SDK 不做运行时权限检查。
只授予 ACCESS_COARSE_LOCATION(模糊位置)不满足要求,必须授予精确位置。
部分机型还需打开系统"位置信息"开关才能返回 WiFi 扫描结果,否则会回调 ERROR_NO_WIFI_DATA。
只授予 ACCESS_COARSE_LOCATION(模糊位置)不满足要求,必须授予精确位置。
部分机型还需打开系统"位置信息"开关才能返回 WiFi 扫描结果,否则会回调 ERROR_NO_WIFI_DATA。
动态申请权限示例:
Java
private static final int REQUEST_CODE_WIFI = 1001;
private void requestWifiPermission(Activity activity) { List<String> permissions = new ArrayList<>(); permissions.add(Manifest.permission.ACCESS_FINE_LOCATION); permissions.add(Manifest.permission.ACCESS_COARSE_LOCATION); if (Build.VERSION.SDK_INT >= 33) { permissions.add("android.permission.NEARBY_WIFI_DEVICES"); } List<String> toRequest = new ArrayList<>(); for (String p : permissions) { if (ContextCompat.checkSelfPermission(activity, p) != PackageManager.PERMISSION_GRANTED) { toRequest.add(p); } } if (!toRequest.isEmpty()) { ActivityCompat.requestPermissions(activity, toRequest.toArray(new String[0]), REQUEST_CODE_WIFI); }}二、接口说明
identify
Java
public boolean identify(LatLng location, List<Tag> tags, String coordType, IIdentifyResult listener)所属类:com.baidu.placesemantic.PlaceSemantic
coordType 可选值:bd09ll(默认)、gcj02ll、wgs84ll。SDK 不做转换,原样传给服务端,返回结果中的坐标(response.getLocation()、poi.getLocation())也使用同一坐标系。
Tag
com.baidu.placesemantic.data.smartscene.Tag,场景标签。
Java
List<Tag> tags = new ArrayList<>();tags.add(new Tag().setName("医疗:综合医院").setRadius(2000));tags.add(new Tag().setName("交通设施:停车场").setRadius(2000));可用的标签列表请向对接人获取。
IIdentifyResult
com.baidu.placesemantic.listener.IIdentifyResult
Java
void onResult(int errorCode, String msg, IdentifyResponse response);三、返回结果说明
结果类:com.baidu.placesemantic.data.smartscene.IdentifyResponse,结构如下:
代码
IdentifyResponse├── status / message / logId / location└── results: List<IdentifyResult> // 每个命中的场景标签一项 ├── matchedTag / searchRadius └── pois: List<IdentifyPoi> // 该标签下的候选 POI ├── uid / name / source / matchType / distance / ... └── coveringAois: List<CoveringAoi>IdentifyResponse
IdentifyResult
IdentifyPoi
CoveringAoi
四、完整示例
Java
import com.baidu.mapapi.model.LatLng;import com.baidu.placesemantic.PlaceSemantic;import com.baidu.placesemantic.data.smartscene.IdentifyResponse;import com.baidu.placesemantic.data.smartscene.Tag;import com.baidu.placesemantic.listener.IIdentifyResult;
public class OnlineSceneActivity extends AppCompatActivity {
private static final String TAG = "OnlineScene";
private void doIdentify(double lat, double lng) { // 1. 请求坐标(BD09LL) LatLng location = new LatLng(lat, lng);
// 2. 关注的场景标签 List<Tag> tags = new ArrayList<>(); tags.add(new Tag().setName("医疗:综合医院").setRadius(2000)); tags.add(new Tag().setName("交通设施:停车场").setRadius(2000));
// 3. 发起识别 boolean ret = PlaceSemantic.getInstance().identify(location, tags, "bd09ll", new IIdentifyResult() { @Override public void onResult(int errorCode, String msg, IdentifyResponse response) { // 回调在 SDK 工作线程,更新 UI 需切换到主线程 runOnUiThread(() -> handleResult(errorCode, msg, response)); } }); if (!ret) { Log.e(TAG, "identify 提交失败,检查参数"); } }
private void handleResult(int errorCode, String msg, IdentifyResponse response) { switch (errorCode) { case 0: break; case IIdentifyResult.ERROR_NO_WIFI_PERMISSION: // 申请权限 return; case IIdentifyResult.ERROR_NO_WIFI_DATA: // 提示用户打开 WiFi return; case IIdentifyResult.ERROR_SERVER: Log.e(TAG, "服务端错误 status=" + response.getStatus() + " message=" + response.getMessage() + " logId=" + response.getLogId()); return; default: Log.e(TAG, "识别失败 errorCode=" + errorCode + " msg=" + msg); return; }
List<IdentifyResponse.IdentifyResult> results = response.getResults(); if (results == null || results.isEmpty()) { Log.i(TAG, "当前位置未识别到场景"); return; } for (IdentifyResponse.IdentifyResult result : results) { Log.i(TAG, "命中场景:" + result.getMatchedTag() + " 半径:" + result.getSearchRadius()); if (result.getPois() == null) { continue; } for (IdentifyResponse.IdentifyPoi poi : result.getPois()) { Log.i(TAG, " POI:" + poi.getName() + " 距离:" + poi.getDistance() + "m" + " 得分:" + poi.getScore()); } } }}五、注意事项
每次调用都会发起网络请求。如需高频识别(如持续跟踪),建议控制调用频率,或改用 离线场景识别。
WiFi 列表的新鲜度取决于系统扫描。SDK 读取的是系统最近一次扫描结果,不会主动触发扫描。
返回结果中的坐标与入参使用同一坐标系,在地图上展示时注意坐标系一致。
排查问题时请提供 response.getLogId() 和开启 enableLog(true, true) 后的本地日志。
本篇文章对您是否有帮助?