实时数据

随着实时数据量的迅速增长,人们对数据能够即时显示在手机、计算机上的需求越来越强烈。客户端与服务器之间对实时数据传输一般采用 WebSocket 协议、TCP 协议、HTTP 协议以及 Kafka 专用通讯协议等,可传输的数据格式包括 CSV、JSON 、GeoJSON等。

本节以查询一个线数据为例,每两秒将一个点通过 dataFlowService 传输给服务器,用来模拟实时数据。

var map,urlQuery ="http://support.supermap.com.cn:8090/iserver/services/map-china400/rest/maps/China_4326",
urlDataFlow ="http://support.supermap.com.cn:8090/iserver/services/dataflowTest/dataflow";


var popup = L.popup({offset: L.point(0, 0), autoPan: true});
//创建DataFlowLayer,创建DataFlowLayer订阅DataFlow并将结果加载到地图上
var dataFlowLayer = L.supermap.dataFlowLayer(urlDataFlow, {
style: function (feature) {
return {
    fillColor: "red",
    fillOpacity: 1,
    radius: 6,
    weight: 0
};
},
onEachFeature: function (feature, layer) {
popup.setLatLng(L.GeoJSON.coordsToLatLng(feature.geometry.coordinates))
    .setContent(feature.properties.time);
if (!popup.isOpen()) {
    popup.addTo(map);
}
},
});
dataFlowLayer.addTo(map);

//查询一个线数据,每两秒将一个点通过dataFlowService广播给iSevrer的dataflow服务,模拟实时数据
//可通过dataFlowService将其他实时数据广播给SuperMap iServer
function query() {
var param = new SuperMap.QueryBySQLParameters({
queryParams: {
name: "[email protected]#1",
attributeFilter: "SMID = 1755"
}
});
L.supermap
.queryService(urlQuery)
.queryBySQL(param, function (serviceResult) {
featureResult = serviceResult;
dataFlowService = L.supermap.dataFlowService(urlDataFlow).initBroadcast();
dataFlowService.on('broadcastSocketConnected', function (e) {
    timer = window.setInterval("broadcast()", 2000);
})
});
}

var count = 200;

function broadcast() {
if (count >= featureResult.result.recordsets[0].features.features[0].geometry.coordinates.length) {
window.clearInterval(timer);
return;
}
var point = featureResult.result.recordsets[0].features.features[0].geometry.coordinates[count];
var feature = {
geometry: {coordinates: [point[0], point[1]], type: "Point"},
id: 1,
type: "Feature",
properties: {id: 1, time: new Date()}
};
dataFlowService.broadcast(feature);
count += 3;
}

查看完整示例代码

results matching ""

    No results matching ""