-
Notifications
You must be signed in to change notification settings - Fork 0
/
interactions.js
93 lines (87 loc) · 2.68 KB
/
interactions.js
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
// some interactions
var typeSelect = document.getElementById('type');
// 全局变量. 记录上一次addInteraction用户选择的绘图控件
var draw;
function addInteraction() {
var value = typeSelect.value;
if (value !== 'None') {
var geometryFunction, maxPoints;
if (value === 'Square') {
value = 'Circle';
geometryFunction = ol.interaction.Draw.createRegularPolygon(4);
} else if (value === 'Box') {
value = 'LineString';
maxPoints = 2;
geometryFunction = function(coordinates, geometry) {
if (!geometry) {
geometry = new ol.geom.Polygon(null);
}
var start = coordinates[0];
var end = coordinates[1];
geometry.setCoordinates([
[start, [start[0], end[1]], end, [end[0], start[1]], start]
]);
return geometry;
};
}
draw = new ol.interaction.Draw({
//source: drawsource,
features:features,
type: /** @type {ol.geom.GeometryType} */ (value),
geometryFunction: geometryFunction,
maxPoints: maxPoints
});
map.addInteraction(draw);
}
}
typeSelect.onchange = function(e) {
map.removeInteraction(draw);
addInteraction();
};
addInteraction();
var modify = new ol.interaction.Modify({
features:features,
// the SHIFT key must be pressed to delete vertices, so
// that new vertices can be drawn at the same position
// of existing vertices
deleteCondition: function(event) {
return ol.events.condition.shiftKeyOnly(event) &&
ol.events.condition.singleClick(event);
}
});
//map.addInteraction(modify);
var removebtn = document.getElementById("removedraw");
removebtn.onclick = function(){
map.removeInteraction(draw);
console.log("draw removed");
}
var removedrawed = document.getElementById("removedrawed");
removedrawed.onclick = function(){
drawsource.clear();
console.log("remove all features in user layer!");
}
var features2wgs = function(features){
var fs = [];
for(f in features)
{
gclone = features[f].getGeometry().clone().transform("EPSG:3857","EPSG:4326");
var fclone = new ol.Feature(gclone);
fclone.setProperties(features[f].getProperties());
fs.push(fclone);
}
return fs;
}
var exportkml = document.getElementById("exportkml");
exportkml.onclick = function(){
var kmlformatter = new ol.format.KML();
var kmlstr = kmlformatter.writeFeatures(features2wgs(selectedFeatures.array_));
console.log(kmlstr);
selinfobox.innerHTML = kmlstr;
}
var exportgeojson = document.getElementById("exportgeojson");
exportgeojson.onclick = function(){
var formatter = new ol.format.EsriJSON();
var jsonstr = formatter.writeFeatures(features2wgs(selectedFeatures.array_));
console.log(jsonstr);
selinfobox.innerHTML = jsonstr;
}