-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathgraphicfeature.html
143 lines (105 loc) · 4.24 KB
/
graphicfeature.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
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
<!DOCTYPE html>
<html>
<title>Graphic features</title>
<link rel="stylesheet" type="text/css" href="examples.css"/>
<script type="text/javascript" src="https://api.giscloud.com/1/api.js"></script>
<script type="text/javascript" src="examples.js"></script>
<script type="text/javascript">
var viewer, polygonGraphic, $ = giscloud.exposeJQuery();
function randomPointsInsideBounds(bounds, n) {
var i, points = [];
for (i = 0; i < n; i++) {
points.push(new giscloud.geometry.Point(
bounds.left + Math.random() * bounds.width(),
bounds.bottom + Math.random() * bounds.height()
));
}
return points;
}
function center(points) {
var i, n = points.length, sx = points[0].x, sy = points[0].y;
for (i = 1; i < n - 1; i++) { sx += points[i].x; }
for (i = 1; i < n - 1; i++) { sy += points[i].y; }
return new giscloud.geometry.Point(sx / n, sy / n);
}
function clockwise(points) {
var c = center(points);
return points.sort(function (a, b) {
var dxa = a.x - c.x, dya = a.y - c.y,
dxb = b.x - c.x, dyb = b.y - c.y,
pi2 = 2 * Math.PI,
phia = (Math.atan2(dya, dxa) + pi2) % pi2,
phib = (Math.atan2(dyb, dxb) + pi2) % pi2;
if (phia < phib) return 1;
if (phia > phib) return -1;
return 0;
});
}
function randomPoly() {
var bounds, points, n, geometry, style;
// remove existing graphic feature
if (polygonGraphic) {
viewer.graphic.remove(polygonGraphic);
}
// get current map bounds
bounds = viewer.bounds();
// get n random points inside current bounds
n = 4 + Math.ceil(Math.random() * 10);
points = clockwise(randomPointsInsideBounds(bounds, n));
// create polygon geometry
geometry = new giscloud.geometry.Polygon([new giscloud.geometry.Line(points)]);
// create a style
style = new giscloud.GraphicStyle("polygon");
style.color = giscloud.Color.randomHue(100, 60);
// create the graphic feature
polygonGraphic = new giscloud.GraphicFeature(geometry, style);
// add the feature to the map
viewer.graphic.add(polygonGraphic);
}
giscloud.ready(function () {
// create a viewer
viewer = new giscloud.Viewer("mapViewer", mapId, { slider: true });
// add button handlers after the viewer has loaded
viewer.loading.done(function () {
$("#addRandomPolygon")
.on("click", randomPoly)
.prop("disabled", false);
});
});
</script>
<style type="text/css">
#geom {
border: thin solid #ccc;
padding: 0.3em;
width: 100%;
margin-top: 0.5em;
}
</style>
<body>
<h1>Graphic features</h1>
<p>
The viewer's graphic object allows us to add custom graphic elements to the map.
</p>
<p>
Pressing the button below will take some random points inside the current bounds, </br>
create a polygon, and put the graphic feature to the map.
</p>
<p>
<button id="addRandomPolygon" disabled="disabled">Random polygon</button>
</p>
<div id="mapViewer"></div>
<p>
The code:
</p>
<pre>// create polygon geometry
geometry = new giscloud.geometry.Polygon([new giscloud.geometry.Line(points)]);
// create a style
style = new giscloud.GraphicStyle("polygon");
style.color = giscloud.Color.randomHue(100, 60);
// create the graphic feature
polygonGraphic = new giscloud.GraphicFeature(geometry, style);
// add the feature to the map
viewer.graphic.add(polygonGraphic);
</pre>
</body>
</html>