forked from SuperMap/iClient-JavaScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path02_editFeatures.html
More file actions
274 lines (249 loc) · 9.38 KB
/
02_editFeatures.html
File metadata and controls
274 lines (249 loc) · 9.38 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>地物编辑</title>
<script type="text/javascript" include="bootstrap,widgets.alert" src="../js/include-web.js"></script>
<script type="text/javascript" src="../../dist/include-mapboxgl.js"></script>
<style>
.editPane {
position: absolute;
right: 10px;
top: 10px;
text-align: center;
background: #FFF;
z-index: 1000;
}
</style>
</head>
<body style=" margin: 0;overflow: hidden;background: #fff;width: 100%;height:100%;position: absolute;top: 0;">
<div id="map" style="margin:0 auto;width: 100%;height: 100%"></div>
<div>
<div class="panel panel-primary editPane" id="editPane">
<div class='panel-heading'>
<h5 class='panel-title text-center'>编辑单个要素</h5></div>
<div class='panel-body content'>
<input type='button' class='btn btn-default' value='添加地物' onclick='addMarker()'/>
<input type='button' class='btn btn-default' value='撤销添加' onclick='revocationMarker()'/>
<input type='button' class='btn btn-default' value='提交添加' onclick='commit()'/>
<input type='button' class='btn btn-default' value='清除结果' onclick='clearLayer()'/>
</div>
</div>
</div>
<script>
var map, id = [], pointFeature, alertDiv, featureService,
baseUrl = (window.isLocal ? window.server : "http://support.supermap.com.cn:8090") + "/iserver/services/map-world/rest/maps/World",
mapUrl = baseUrl + "/zxyTileImage.png?z={z}&x={x}&y={y}",
dataUrl = (window.isLocal ? window.server : "http://support.supermap.com.cn:8090") + "/iserver/services/data-world/rest/data";
var attribution = "<a href='https://www.mapbox.com/about/maps/' target='_blank'>© Mapbox </a>" +
"| Map Data <span>© <a href='http://support.supermap.com.cn/product/iServer.aspx' target='_blank'>SuperMap iServer</a></span> " +
" with <span>© <a href='http://iclient.supermap.io' target='_blank'>SuperMap iClient</a></span>";
map = new mapboxgl.Map({
container: 'map',
style: {
"version": 8,
"sources": {
"raster-tiles": {
"attribution": attribution,
"type": "raster",
"tiles": [mapUrl],
"tileSize": 256
}
},
"layers": [{
"id": "simple-tiles",
"type": "raster",
"source": "raster-tiles",
}]
},
center: [0, 0],
zoom: 2
});
map.addControl(new mapboxgl.NavigationControl(), 'top-left');
map.addControl(new mapboxgl.supermap.LogoControl(), 'bottom-right');
featureService = new mapboxgl.supermap.FeatureService(dataUrl);
var sourceFeatures = {},
addPointFeaturesData = {
"type": "FeatureCollection",
"features": []
};
map.loadImage('../img/marker-icon.png', function (error, image) {
if (error) throw error;
map.addImage('positionPoint', image);
});
initFeature();
function initFeature() {
var polygon = {
"type": "Polygon",
"coordinates": [[[118, 20], [120, 20], [120, 50], [-120, 50], [118, 20]]]
};
var getFeatureParams = new SuperMap.GetFeaturesByGeometryParameters({
toIndex: -1,
datasetNames: ["World:Capitals"],
geometry: polygon,
spatialQueryMode: "INTERSECT"
});
featureService.getFeaturesByGeometry(getFeatureParams, function (serviceResult) {
sourceFeatures = serviceResult.result.features;
if (!map.getSource("queryDatas")) {
map.addSource("queryDatas", {
"type": "geojson",
"data": sourceFeatures
});
} else {
map.getSource("queryDatas").setData(sourceFeatures);
}
map.addLayer({
"id": "queryDatasLayer",
"type": "symbol",
"source": "queryDatas",
"layout": {
"icon-image": "positionPoint",
"icon-size": 0.8,
"icon-offset": [0, -15] //设置偏移量
},
});
var popup = new mapboxgl.Popup({
anchor: 'bottom',
closeButton: false,
offset: {
'bottom': [0, -20],
}
});
map.on('mousemove', "queryDatasLayer", function (e) {
popup.setLngLat(e.lngLat).setHTML(e.features[0].properties.CAPITAL).addTo(map);
map.getCanvas().style.cursor = 'pointer';
});
map.on('mouseout', "queryDatasLayer", function () {
map.getCanvas().style.cursor = '';
popup.remove();
})
});
}
//添加地物
function addMarker() {
closeClearListener();
widgets.alert.clearAlert();
addPointFeaturesData.features = [];
var xmax = 120, xmin = 100, ymax = 50, ymin = 20;
var addPoint = [Math.floor(Math.random() * (xmax - xmin + 1) + xmin), Math.floor(Math.random() * (ymax - ymin + 1) + ymin)];
pointFeature = {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": addPoint
},
"properties": {POP: 1, CAPITAL: 'test'}
};
addPointFeaturesData.features.push(pointFeature);
if (!map.getLayer("addPoint")) {
map.addSource("addPointsSource", {
"type": "geojson",
"data": addPointFeaturesData
});
map.addLayer({
"id": "addPoint",
"type": "circle",
"source": "addPointsSource",
'paint': {
'circle-radius': 8,
'circle-color': 'rgba(255, 0, 0, 0.2)',
"circle-stroke-color": 'red',
"circle-stroke-width": 2,
// "circle-translate": [0, 13] //设置偏移量
},
});
} else {
map.getSource("addPointsSource").setData(addPointFeaturesData);
}
map.flyTo({
center: addPoint,
zoom: 5,
bearing: 0,
speed: 1.2,
curve: 1,
easing: function (t) {
return t;
}
});
}
function revocationMarker() {
closeClearListener();
if (pointFeature) {
addPointFeaturesData.features = [];
map.getSource("addPointsSource").setData(addPointFeaturesData);
pointFeature = null;
} else {
widgets.alert.showAlert("没有可撤回的要素。", false);
}
}
//提交添加
function commit() {
closeClearListener();
widgets.alert.clearAlert();
if (!pointFeature) {
widgets.alert.showAlert("没有可提交的新要素,请先添加新要素。", false);
return;
}
//更新添加的source
var addFeatureParams = new SuperMap.EditFeaturesParameters({
features: pointFeature,
dataSourceName: "World",
dataSetName: "Capitals",
editType: "add",
returnContent: true
});
featureService.editFeatures(addFeatureParams, function (serviceResult) {
if (serviceResult.result.succeed) {
addPointFeaturesData.features = [];
map.getSource("addPointsSource").setData(addPointFeaturesData);
pointFeature = null;
map.removeLayer("queryDatasLayer");
widgets.alert.showAlert("提交成功", true);
pointFeature = null;
initFeature();
}
});
}
var popup = new mapboxgl.Popup({
anchor: 'left',
closeButton: false,
offset: {'left': [10, 10]}
});
function clearLayer() {
closeClearListener();
widgets.alert.clearAlert();
map.on('mousemove', mousemoveListener);
map.on('click', 'queryDatasLayer', dataClickListener);
}
function mousemoveListener(e) {
popup.setLngLat(e.lngLat).setHTML("请选择需要删除的点").addTo(map);
}
function dataClickListener(e) {
var deleteParams = new SuperMap.EditFeaturesParameters({
dataSourceName: "World",
dataSetName: "Capitals",
IDs: [e.features[0].properties.ID],
editType: "delete"
});
featureService.editFeatures(deleteParams, function (serviceResult) {
if (serviceResult.result.succeed) {
map.removeLayer("queryDatasLayer");
//重载数据默认集
initFeature();
map.getCanvas().style.cursor = '';
closeClearListener();
widgets.alert.showAlert("删除成功", true);
} else {
widgets.alert.showAlert("删除失败", false);
}
});
}
function closeClearListener() {
popup.remove();
map.off('mousemove', mousemoveListener);
map.off('click', 'queryDatasLayer', dataClickListener);
}
</script>
</body>
</html>