Skip to content

Commit 0f520f0

Browse files
committed
优化DataFlowLayer
1 parent 0893522 commit 0f520f0

File tree

3 files changed

+102
-14
lines changed

3 files changed

+102
-14
lines changed

dist/iclient9-leaflet.js

Lines changed: 51 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17539,21 +17539,23 @@ var DataFlowLayer = exports.DataFlowLayer = _leaflet2.default.GeoJSON.extend({
1753917539
options: {
1754017540
geometry: null,
1754117541
prjCoordSys: null,
17542-
excludeField: null
17542+
excludeField: null,
17543+
idField: "id"
1754317544
},
1754417545

1754517546
initialize: function initialize(url, options) {
1754617547
options = options || {};
1754717548
var me = this;
1754817549
if (options.style && !options.pointToLayer) {
17549-
me.options.pointToLayer = function (geojson, latlng) {
17550-
return _leaflet2.default.circleMarker(latlng, me.options.style);
17550+
options.pointToLayer = function (geojson, latlng) {
17551+
return _leaflet2.default.circleMarker(latlng, options.style());
1755117552
};
1755217553
}
1755317554
_leaflet2.default.Util.setOptions(me, options);
1755417555
me._layers = {};
1755517556
_leaflet2.default.stamp(me);
1755617557
me.url = url;
17558+
this.idCache = {};
1755717559
},
1755817560

1755917561
/**
@@ -17622,9 +17624,52 @@ var DataFlowLayer = exports.DataFlowLayer = _leaflet2.default.GeoJSON.extend({
1762217624
},
1762317625

1762417626
_onMessageSuccessed: function _onMessageSuccessed(msg) {
17625-
this.clearLayers();
17626-
this.addData(msg.featureResult);
17627-
this.fire("dataUpdated", { layer: this, data: msg.featureResult });
17627+
var geojson = msg.featureResult;
17628+
var geoID = msg.featureResult.properties[this.options.idField];
17629+
var layer = null;
17630+
if (geoID !== undefined && this.idCache[geoID]) {
17631+
layer = this.getLayer(this.idCache[geoID]);
17632+
this._updateLayerData(layer, geojson);
17633+
} else {
17634+
layer = _leaflet2.default.GeoJSON.geometryToLayer(geojson, this.options);
17635+
layer.feature = _leaflet2.default.GeoJSON.asFeature(geojson);
17636+
this.addLayer(layer);
17637+
if (geoID !== undefined) {
17638+
this.idCache[geoID] = this.getLayerId(layer);
17639+
}
17640+
}
17641+
if (this.options.onEachFeature) {
17642+
this.options.onEachFeature(geojson, layer);
17643+
}
17644+
this.fire("dataUpdated", { layer: this, updateLayer: layer, data: msg.featureResult });
17645+
},
17646+
_updateLayerData: function _updateLayerData(layer, geojson) {
17647+
if (geojson.properties) {
17648+
layer.feature.properties = geojson.properties;
17649+
}
17650+
var latlngs = [];
17651+
switch (geojson.geometry.type) {
17652+
case 'Point':
17653+
latlngs = _leaflet2.default.GeoJSON.coordsToLatLng(geojson.geometry.coordinates);
17654+
layer.setLatLng(latlngs);
17655+
break;
17656+
case 'LineString':
17657+
latlngs = _leaflet2.default.GeoJSON.coordsToLatLngs(geojson.geometry.coordinates, 0);
17658+
layer.setLatLngs(latlngs);
17659+
break;
17660+
case 'MultiLineString':
17661+
latlngs = _leaflet2.default.GeoJSON.coordsToLatLngs(geojson.geometry.coordinates, 1);
17662+
layer.setLatLngs(latlngs);
17663+
break;
17664+
case 'Polygon':
17665+
latlngs = _leaflet2.default.GeoJSON.coordsToLatLngs(geojson.geometry.coordinates, 1);
17666+
layer.setLatLngs(latlngs);
17667+
break;
17668+
case 'MultiPolygon':
17669+
latlngs = _leaflet2.default.GeoJSON.coordsToLatLngs(geojson.geometry.coordinates, 2);
17670+
layer.setLatLngs(latlngs);
17671+
break;
17672+
}
1762817673
}
1762917674
});
1763017675
var dataFlowLayer = exports.dataFlowLayer = function dataFlowLayer(url, options) {

dist/iclient9-leaflet.min.js

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/leaflet/overlay/DataFlowLayer.js

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,21 +18,22 @@ export var DataFlowLayer = L.GeoJSON.extend({
1818
geometry: null,
1919
prjCoordSys: null,
2020
excludeField: null,
21+
idField: "id"
2122
},
2223

2324
initialize: function (url, options) {
2425
options = options || {};
2526
var me = this;
2627
if (options.style && !options.pointToLayer) {
27-
me.options.pointToLayer = function (geojson, latlng) {
28-
return L.circleMarker(latlng, me.options.style);
28+
options.pointToLayer = function (geojson, latlng) {
29+
return L.circleMarker(latlng, options.style());
2930
}
3031
}
3132
L.Util.setOptions(me, options);
3233
me._layers = {};
3334
L.stamp(me);
3435
me.url = url;
35-
36+
this.idCache = {};
3637
},
3738

3839
/**
@@ -101,10 +102,52 @@ export var DataFlowLayer = L.GeoJSON.extend({
101102
},
102103

103104
_onMessageSuccessed: function (msg) {
104-
this.clearLayers();
105-
this.addData(msg.featureResult);
106-
this.fire("dataUpdated", {layer: this, data: msg.featureResult});
107-
105+
var geojson = msg.featureResult;
106+
var geoID = msg.featureResult.properties[this.options.idField];
107+
var layer = null;
108+
if (geoID !== undefined && this.idCache[geoID]) {
109+
layer = this.getLayer(this.idCache[geoID]);
110+
this._updateLayerData(layer, geojson);
111+
} else {
112+
layer = L.GeoJSON.geometryToLayer(geojson, this.options);
113+
layer.feature = L.GeoJSON.asFeature(geojson);
114+
this.addLayer(layer);
115+
if (geoID !== undefined) {
116+
this.idCache[geoID] = this.getLayerId(layer);
117+
}
118+
}
119+
if (this.options.onEachFeature) {
120+
this.options.onEachFeature(geojson, layer);
121+
}
122+
this.fire("dataUpdated", {layer: this, updateLayer: layer, data: msg.featureResult});
123+
},
124+
_updateLayerData: function (layer, geojson) {
125+
if (geojson.properties) {
126+
layer.feature.properties = geojson.properties;
127+
}
128+
var latlngs = [];
129+
switch (geojson.geometry.type) {
130+
case 'Point':
131+
latlngs = L.GeoJSON.coordsToLatLng(geojson.geometry.coordinates);
132+
layer.setLatLng(latlngs);
133+
break;
134+
case 'LineString':
135+
latlngs = L.GeoJSON.coordsToLatLngs(geojson.geometry.coordinates, 0);
136+
layer.setLatLngs(latlngs);
137+
break;
138+
case 'MultiLineString':
139+
latlngs = L.GeoJSON.coordsToLatLngs(geojson.geometry.coordinates, 1);
140+
layer.setLatLngs(latlngs);
141+
break;
142+
case 'Polygon':
143+
latlngs = L.GeoJSON.coordsToLatLngs(geojson.geometry.coordinates, 1);
144+
layer.setLatLngs(latlngs);
145+
break;
146+
case 'MultiPolygon':
147+
latlngs = L.GeoJSON.coordsToLatLngs(geojson.geometry.coordinates, 2);
148+
layer.setLatLngs(latlngs);
149+
break;
150+
}
108151
}
109152
});
110153
export var dataFlowLayer = function (url, options) {

0 commit comments

Comments
 (0)