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
212 lines (194 loc) · 7.32 KB
/
02_editFeatures.html
File metadata and controls
212 lines (194 loc) · 7.32 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
<!DOCTYPE html>
<html>
<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-leaflet.js"></script>
<style>
.leaflet-tooltip, .leaflet-tooltip:before {
color: white;
border: none;
background: rgba(0, 0, 0, 0.5);
}
.tooltip-inner {
width: 80px;
background-color: transparent;
color: #515151;
}
</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>
<script type="text/javascript">
var host = window.isLocal ? window.server : "http://support.supermap.com.cn:8090";
var map, marker, featureGroup, resultLayer, featureService,
baseUrl = host + "/iserver/services/map-world/rest/maps/World",
url = host + "/iserver/services/data-world/rest/data";
map = L.map('map', {
preferCanvas: true,
crs: L.CRS.EPSG4326,
center: {lon: 0, lat: 0},
maxZoom: 18,
zoom: 2
});
L.supermap.tiledMapLayer(baseUrl).addTo(map);
featureGroup = L.featureGroup().addTo(map);
featureService = L.supermap.featureService(url);
initFeature();
initEditView();
function initFeature() {
var polygon = L.polygon([[20, 118], [20, 120], [50, 120], [50, -120], [20, 118]]);
var getFeatureParams = new SuperMap.GetFeaturesByGeometryParameters({
toIndex: -1,
datasetNames: ["World:Capitals"],
geometry: polygon
});
featureService.getFeaturesByGeometry(getFeatureParams, function (serviceResult) {
resultLayer = L.geoJSON(serviceResult.result.features).addTo(map);
resultLayer.on("mousemove", function (e) {
e.layer.bindPopup("首都:" + e.layer.feature.properties.CAPITAL).openPopup();
});
resultLayer.on("mouseout", function (e) {
e.layer.closePopup();
});
});
}
function initEditView() {
var infoView = L.control({position: 'topright'});
infoView.onAdd = function () {
var me = this;
me._div = L.DomUtil.create('div', 'panel panel-primary');
me._div.innerHTML = "<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>";
handleMapEvent(me._div, me._map);
return me._div;
};
infoView.addTo(map);
}
function handleMapEvent(div, map) {
if (!div || !map) {
return;
}
div.addEventListener('mouseover', function () {
map.dragging.disable();
map.scrollWheelZoom.disable();
map.doubleClickZoom.disable();
});
div.addEventListener('mouseout', function () {
map.dragging.enable();
map.scrollWheelZoom.enable();
map.doubleClickZoom.enable();
});
}
//添加鼠标滑动事件
var tooltip = L.tooltip({
direction: 'right'
});
var pointerMoveHandler = function (evt) {
tooltip.setContent("选择并删除要素。");
tooltip.setLatLng(evt.latlng);
tooltip.addTo(map);
};
function addMarker() {
widgets.alert.clearAlert();
closeClearListener();
var xmax = 120, xmin = 100, ymax = 50, ymin = 20,
point = [];
if (!featureGroup.hasLayer(marker)) {
point = [
Math.floor(Math.random() * (ymax - ymin + 1) + ymin),
Math.floor(Math.random() * (xmax - xmin + 1) + xmin)
];
marker = L.circleMarker(point, {color: "red"});
featureGroup.addLayer(marker);
featureGroup.addTo(map);
map.flyTo(point, 5);
} else {
featureGroup.clearLayers();
point = [
Math.floor(Math.random() * (ymax - ymin + 1) + ymin),
Math.floor(Math.random() * (xmax - xmin + 1) + xmin)
];
marker = L.circleMarker(point, {color: "red"});
featureGroup.addLayer(marker);
featureGroup.addTo(map);
map.flyTo(point, 5);
}
}
function revocationMarker() {
closeClearListener();
if (featureGroup.hasLayer(marker)) {
featureGroup.clearLayers();
marker = null;
} else {
widgets.alert.showAlert("没有可撤回的要素。",false);
}
}
function commit() {
widgets.alert.clearAlert();
closeClearListener();
if (featureGroup.hasLayer(marker)) {
marker = marker.toGeoJSON();
marker.properties = {POP: 1, CAPITAL: 'test'};
var addFeatureParams = new SuperMap.EditFeaturesParameters({
dataSourceName: "World",
dataSetName: "Capitals",
features: marker,
editType: "add",
returnContent: true
});
featureService.editFeatures(addFeatureParams, function (serviceResult) {
if (serviceResult.result.succeed) {
featureGroup.clearLayers();
marker = null;
map.removeLayer(resultLayer);
resultLayer = null;
initFeature();
widgets.alert.showAlert("提交成功",true);
}
});
} else {
widgets.alert.showAlert("请先添加地物",false);
}
}
function clearLayer() {
map.on('mousemove', pointerMoveHandler);
resultLayer.on("click", layerClickListener);
widgets.alert.clearAlert();
}
function layerClickListener(e) {
var deleteParams = new SuperMap.EditFeaturesParameters({
dataSourceName: "World",
dataSetName: "Capitals",
IDs: [e.layer.feature.properties.ID],
editType: "delete"
});
featureService.editFeatures(deleteParams, function (serviceResult) {
if (serviceResult.result.succeed) {
map.removeLayer(resultLayer);
resultLayer = null;
initFeature();
widgets.alert.showAlert("删除成功",true);
closeClearListener();
} else {
widgets.alert.showAlert("删除失败",false);
}
});
}
function closeClearListener() {
if (map.hasLayer(tooltip)) {
tooltip.removeFrom(map);
}
if (resultLayer) {
resultLayer.off("click", layerClickListener);
}
map.off('mousemove', pointerMoveHandler);
}
</script>
</body>
</html>