快速接入设备位置到地图
更新时间:
复制 MD 格式
本文描述如何使用物联网数据分析中的二维数据可视化功能,快速接入设备位置到地图。一般在空气质量检测、基站分布、资产管理等场景,需要接入设备位置到地图上,实时展示设备的运行状态,查看并管理设备。
步骤一、创建产品
- 登录物联网平台控制台。
- 参考创建产品,创建带有位置属性的产品。
在创建产品(设备模型)页面中,填写产品名称,所属品类选择自定义品类,节点类型选择直连设备,连网方式选择WiFi,数据格式选择ICA 标准数据格式 (Alink JSON)。
- 参考单个创建设备,添加设备。
在添加设备对话框中,产品下拉框中选择已创建的产品,输入DeviceName,单击确认。
保存设备的设备证书(ProductKey、DeviceName、DeviceSecret),以备后续使用。
- 参考设备属性上报位置(Geolocation)内容,为设备所属产品添加地理位置功能,并上报设备位置到云端。
在添加标准功能对话框中,选择其他类型页签,搜索地理位置,从列表中选择适用的地理位置属性(标识符为GeoLocation),单击确定。
步骤二、模拟设备
使用Link Kit SDK提供的Java SDK Demo模拟设备的GPS模块。
- 下载Java SDK Demo并在本地解压。
- 在device_id.json文件中,填写已保存的设备证书(ProductKey、DeviceName、DeviceSecret)信息。
{ "productKey": "xxx", "deviceName": "xxx", "productSecret": "", "deviceSecret": "xxx", "subDevice": [ { "productKey": "", "deviceName": "" }, { "productKey": "", "deviceName": "" } ] } - 在test_case.json文件中,设置设备要上报的位置属性。
由于在物联网平台控制台,位置属性产品的页面中设置的GeoLocation属性定义是struct结构体,因此test_case.json文件中的value参数需要填结构体子属性的JSON串。可单击子属性名字后的查看获取其标识符。GeoLocation结构体包含经度、纬度、海拔和坐标系统4个子属性。
test_case.json文件中,填好结构体子属性的JSON后,文件内容如下所示。
{ "identifier": "GeoLocation", "value": "{\"Longitude\":116.400372,\"Latitude\":39.919316,\"Altitude\":0,\"CoordinateSystem\":1}" }说明 可使用高德开放平台经纬度拾取工具,设置设备经纬度(Longitude、Latitude)。 - Demo程序的入口在HelloWorld.java文件中,本Demo中我们只需要设备上报位置的属性,因此需简化HelloWorld.java代码。简化后的代码如下所示。
public class HelloWorld { private static final String TAG = "HelloWorld"; private String pk, dn; private ThingSample thingTestManager = null; public static void main(String[] args) { ALog.d(TAG, "Hello world!"); ALog.setLevel(ALog.LEVEL_DEBUG); HelloWorld manager = new HelloWorld(); ALog.d(TAG, "args=" + Arrays.toString(args)); System.out.println(System.getProperty("user.dir")); String diPath = System.getProperty("user.dir") + "/device_id.json"; String deviceInfo = FileUtils.readFile(diPath); if (deviceInfo == null) { ALog.e(TAG, "main - need device info path."); return; } Gson mGson = new Gson(); DeviceInfoData deviceInfoData = mGson.fromJson(deviceInfo, DeviceInfoData.class); if (deviceInfoData == null) { ALog.e(TAG, "main - deviceInfo format error."); return; } ALog.d(TAG, "测试一机一密和物模型"); manager.init(deviceInfoData); } public void init(final DeviceInfoData deviceInfoData) { this.pk = deviceInfoData.productKey; this.dn = deviceInfoData.deviceName; LinkKitInitParams params = new LinkKitInitParams(); /** * 设置Mqtt初始化参数 */ IoTMqttClientConfig config = new IoTMqttClientConfig(); config.productKey = deviceInfoData.productKey; config.deviceName = deviceInfoData.deviceName; config.deviceSecret = deviceInfoData.deviceSecret; config.channelHost = pk + ".iot-as-mqtt." + deviceInfoData.region + ".aliyuncs.com:1883"; /** * 是否接受离线消息 * 对应mqtt的cleanSession字段 */ config.receiveOfflineMsg = false; params.mqttClientConfig = config; /** * 设置初始化设备证书信息,用户传入 */ DeviceInfo deviceInfo = new DeviceInfo(); deviceInfo.productKey = pk; deviceInfo.deviceName = dn; deviceInfo.deviceSecret = deviceInfoData.deviceSecret; params.deviceInfo = deviceInfo; /** * 设置设备当前的初始状态值,属性需要和云端创建的物模型属性一致。 * 如果这里什么属性都不填,物模型就没有当前设备相关属性的初始值。 * 用户调用物模型上报接口之后,物模型会有相关数据缓存。 */ Map<String, ValueWrapper> propertyValues = new HashMap<String, ValueWrapper>(); // 示例 // propertyValues.put("LightSwitch", new ValueWrapper.BooleanValueWrapper(0)); params.propertyValues = propertyValues; thingTestManager = new ThingSample(pk, dn); LinkKit.getInstance().init(params, new ILinkKitConnectListener() { public void onError(AError aError) { ALog.e(TAG, "Init Error error=" + aError); } public void onInitDone(InitResult initResult) { ALog.i(TAG, "onInitDone result=" + initResult); thingTestManager.readData(System.getProperty("user.dir") + "/test_case.json"); thingTestManager.report(); } }); } } - 执行HelloWorld.java文件,使设备的位置生效。
步骤三、创建二维数据可视化场景
- 参考物联网平台设备展示,创建一个数据来源为物联网平台的场景。
在空间数据可视化页面的二维数据可视化页签下,单击创建场景。在弹出的对话框中,输入场景名称,并选择授权形式为只读(展示)或读写(远程控制)。
其中,目标产品或设备组选择产品,并从下拉框中选择已创建的位置属性产品。
- 进入二维可视化场景页面查看设备实时位置。
页面以地图形式展示设备实时位置,设备以圆形标记显示在地图上。顶部显示目标产品、授权形式和设备总数信息,右侧提供3D模型区、报警中心、地理围栏、历史轨迹和热力图等功能页签。
至此,您已经将您的物联网设备快速的接入到地图中了。
后续步骤
可根据分享场景内容,将已创建二维数据可视化场景分享给其他人。该文章对您有帮助吗?