-
Notifications
You must be signed in to change notification settings - Fork 2
/
25_Snap.html
77 lines (70 loc) · 1.8 KB
/
25_Snap.html
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
<!doctype html>
<html lang="en">
<head>
<link rel="stylesheet" href="css/ol.css" type="text/css"/>
<link rel="stylesheet" href="css/sample.css" type="text/css"/>
<style>
body {
padding : 0;
margin : 0;
}
</style>
<title>OpenLayers 3 example 25 Snap</title>
</head>
<body>
<div id="map" class="map" style="width:640px;height:400px"></div>
<script src="js/jquery-2.1.4.min.js"></script>
<script src="js/ol-debug.js"></script>
<script type="text/javascript">
var rasterLayer = new ol.layer.Tile({
source: new ol.source.Stamen({layer: 'watercolor'})
});
// The modify interaction can only modify features, can't treat vectorSources or Layers.
var features = new ol.Collection();
var vectorSource = new ol.source.Vector({features:features});
var vectorLayer = new ol.layer.Vector({
source: vectorSource,
style: new ol.style.Style({
fill: new ol.style.Fill({
color: 'rgba(255,192,192,0.5)'
}),
stroke: new ol.style.Stroke({
color: 'red',
width: 2
}),
image: new ol.style.Circle({
radius: 7,
fill: new ol.style.Fill({
color: 'red'
})
})
})
});
var map = new ol.Map({
target: 'map',
layers: [rasterLayer, vectorLayer],
view: new ol.View({
center: ol.proj.transform([39.816667, 21.416667], 'EPSG:4326', 'EPSG:3857'),
zoom: 4
})
});
var drawInteraction = new ol.interaction.Draw({
source : vectorSource,
type : 'Polygon'
});
map.addInteraction(drawInteraction);
var modifyInteraction = new ol.interaction.Modify({
features: features,
deleteCondition : function (event) {
return ol.events.condition.shiftKeyOnly(event) && ol.events.condition.singleClick(event);
}
})
map.addInteraction(modifyInteraction);
// We must add Snap AFTER Draw and Modify.
var snapInteraction = new ol.interaction.Snap({
source: vectorSource
});
map.addInteraction(snapInteraction);
</script>
</body>
</html>