-
Notifications
You must be signed in to change notification settings - Fork 116
/
addZone.html
296 lines (250 loc) · 14 KB
/
addZone.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
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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Geotab SDK | Add Zone Example</title>
<link rel="icon" href="../../favicon.ico">
<!-- CSS used for the map (Leaflet) -->
<link href="js/leaflet/leaflet.css" rel="stylesheet" type="text/css" />
<!-- Updated CSS file to reflect the UI refresh -->
<link href="css/JavaScriptExamples.css" rel="stylesheet" type="text/css" />
<!-- Materialize icons -->
<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">
</head>
<body>
<!-- Contains all of the elements within the nav bar (logo, sample-pill, sign-out button) -->
<header>
<nav>
<!-- MyGeotab SDK Logo -->
<a href="https://geotab.github.io/sdk/software/introduction/" target="_blank">
<img src="../../assets/images/pillar.svg" width="35" height="35" alt="">
<h1>MyGeotab SDK</h1>
</a>
<!-- Sample tool tip (text within the class "sample-tooltip-text" is displayed when hovered upon) -->
<div class="sample-tooltip">
<h1>DEVELOPER SAMPLE</h1>
<div class="sample-tooltip-text">
<p>This tool is provided as an example and is available on an As-Is basis. You must assume all the risks and costs associated with the use of the sample tool, including, without limitation, any damage to any equipment, software, information or data. In addition, we are not obligated to provide any maintenance, technical or other support for the sample tool. In order to improve our products, we may at any time, and without warning, change the design, presentation, or functionality of the software.</p>
</div>
</div>
<!-- Sign out button using the CSS class "primary-btn" to apply the primary button stylings -->
<button class="primary-btn sign-out-btn" id="signout">Sign out</button>
</nav>
</header>
<div class="container">
<!-- Div used to append the sign in components too within loginUpdated.js -->
<div id="signin-content"></div>
<!-- Header used to display the name of the example and more information about the example -->
<div class="example-header">
<!-- Name of the example-->
<div class="title">Add Zone Example</div>
<!-- Skill level tool tip (text within the class "skill-text" is displayed when hovered upon) -->
<div class="skill-level">Skill level:
<div class="skill-tooltip">
<img src="images/intermediate-pill.svg">
<div class="skill-text">
<p>This example demonstrates a tool that can be built with an intermediate-level knowledge of the MyGeotab system. It is suitable for those who have a good understanding of the Geotab ecosystem and are comfortable with the MyGeotab SDK and leveraging the MyGeotab APIs within their projects.</p>
</div>
</div>
</div>
<!-- Description section of the example -->
<div class="description">
This JavaScript example guides you through the process of creating zones and geocoding addresses.
Using an address such as "CN Tower", or "102 Main Street East, Toronto, ON" an API request is made to retrieve the latitude and longitude coordinates for that address. Then, a box is created around that point consisting of four corners. The distance apart is variable using the slider provided. To finish creating the zone, add a name and click Add zone and the points you see will be saved to your account.
</div>
<!-- Methods/objects used section containing the links to the specific SDK references used -->
<div class="api-references">In this example
<details open>
<summary>API Methods:</summary>
<a href="https://geotab.github.io/sdk/software/api/reference/#Add1" target="_blank">Add</a>
<a href="https://geotab.github.io/sdk/software/api/reference/#GetCoordinates" target="_blank">GetCoordinates</a>
</details>
<details open>
<summary>API Entities:</summary>
<a href="https://geotab.github.io/sdk/software/api/reference/#Zone" target="_blank">Zone</a>
</details>
</div>
</div>
<!-- Components which make up the example in this case input fields to select a location -->
<div id="example-content">
<form>
<p>
<label for="address">Street Address</label>
<input id="address" type="text" placeholder="Example: 125 John Street, Mycity">
</p>
<p>
<label id="sizeLabel" for="size">Size (500m)</label>
<input id="size" type="range" value="500" min="100" max="2500">
</p>
<p>
<input id="shape-square" type="radio" name="shape" value="Square" checked="checked">
<label for="shape-square">Square</label>
<input id="shape-circle" type="radio" name="shape" value="Circle">
<label for="shape-circle">Circle</label>
</p>
<p>
<button class="primary-btn" id="search">Search</button>
</p>
<div id="example-content-details">
<p>
<label for="zoneName">Zone Name</label>
<input id="zoneName" type="text" placeholder="Example: My office" maxlength="255">
</p>
<p>
<input id="zoneDisplayed" type="checkbox" checked="checked">
<label for="zoneDisplayed">Show on MyGeotab by default</label>
</p>
<p>
<button class="primary-btn" id="addZone">Add zone</button>
</p>
</div>
</form>
</div>
<!-- Div which consists of the leaflet map -->
<div class="map-container">
<div id="map"></div>
</div>
</div>
<!-- JavaScript file containing the map functionality -->
<script src="js/leaflet/leaflet.js"></script>
<!-- JavaScript file containing the api functionality -->
<script src="js/api.js"></script>
<!-- JavaScript file containing the login functionality (creating the sign-in modal) -->
<script src="js/login.js"></script>
<!-- The JavaScript used for the example is present in the same HTML file here -->
<script type="text/javascript">
var map;
var layerGroup;
var zonePoints;
document.addEventListener("DOMContentLoaded", function () {
// Set up Leaflet map (https://leafletjs.com)
map = new L.Map("map").setView([43.437111,-79.712756], 13);
L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a>',
subdomains: ['a','b','c']
}).addTo(map);
layerGroup = L.layerGroup().addTo(map);
document.getElementById("example-content-details").style.display = "none";
document.getElementById("search").addEventListener("click", function (event) {
event.preventDefault();
var address = document.getElementById("address").value;
// Use Geotab API to reverse geocode by street address
if (address == "") {
alert("Address is required");
} else {
api.call("GetCoordinates", {
addresses: [address]
}, function (result) {
if (result[0] != null) {
drawZone(result[0]);
document.getElementById("example-content-details").style.display = "block";
} else {
alert("Could not find coordinates for that address");
}
}, function (error) {
alert(error);
});
}
});
document.getElementById("addZone").addEventListener("click", function (event) {
event.preventDefault();
var zoneName = document.getElementById("zoneName").value;
var zoneDisplayed = document.getElementById("zoneDisplayed").checked;
if (zoneName == "") {
alert("Zone name is required");
return;
}
api.call("Add", {
typeName: "Zone",
entity: {
name: zoneName,
externalReference: "",
mustIdentifyStops: true,
displayed: zoneDisplayed,
activeFrom: "1986-01-01T00:00:00.000Z",
activeTo: "2050-01-01T00:00:00.000Z",
zoneTypes: ["ZoneTypeCustomerId"],
groups: [{
id: "GroupCompanyId"
}],
points: zonePoints,
fillColor: {
r: 233,
g: 150,
b: 122,
a: 255
}
}
}, function (result) {
alert("Successfully created zone " + zoneName);
}, function (error) {
alert(error);
});
});
document.getElementById("size").addEventListener("change", function(event) {
if (event.target.value < 1000) {
document.getElementById("sizeLabel").innerHTML = "Size (" + event.target.value + "m)";
} else {
document.getElementById("sizeLabel").innerHTML = "Size (" + (event.target.value / 1000.0).toFixed(2) + "km)";
}
});
});
function drawZone(coordinate) {
var center = new L.LatLng(coordinate.y, coordinate.x);
var radius = document.getElementById("size").value;
if (document.getElementById("shape-square").checked) {
generateSquareZonePoints(coordinate, radius);
} else {
generateCircleZonePoints(coordinate, radius);
}
var polygon = [];
for (var i = 0; i < zonePoints.length; i++) {
polygon.push([
zonePoints[i].y, zonePoints[i].x
]);
}
var shape = L.polygon(polygon);
layerGroup.clearLayers().addLayer(shape);
map.setView(center, 13);
}
function generateSquareZonePoints(coordinates, radius) {
var earthRadius = 6371000;
var x1 = (coordinates.x - toDegrees(radius / earthRadius / Math.cos(toRadians(coordinates.y))));
var x2 = (coordinates.x + toDegrees(radius / earthRadius / Math.cos(toRadians(coordinates.y))));
var y1 = (coordinates.y + toDegrees(radius / earthRadius / Math.sin(toRadians(coordinates.x))));
var y2 = (coordinates.y - toDegrees(radius / earthRadius / Math.sin(toRadians(coordinates.x))));
zonePoints = [
{ "x": x1, "y": y1 },
{ "x": x2, "y": y1 },
{ "x": x2, "y": y2 },
{ "x": x1, "y": y2 }
];
}
function generateCircleZonePoints(coordinate, radius) {
var earthRadius = 6371000;
var latitude = toRadians(coordinate.y);
var longitude = toRadians(coordinate.x);
var angularDistance = radius / earthRadius;
zonePoints = [];
for (var i = 0; i <= 360; i++) {
var radians = toRadians(i);
var latitudeRadians = Math.asin(Math.sin(latitude) * Math.cos(angularDistance) + Math.cos(latitude) * Math.sin(angularDistance) * Math.cos(radians));
var longitudeRadians = longitude + Math.atan2(Math.sin(radians) * Math.sin(angularDistance) * Math.cos(latitude), Math.cos(angularDistance) - Math.sin(latitude) * Math.sin(latitudeRadians));
zonePoints.push({
"x": toDegrees(longitudeRadians),
"y": toDegrees(latitudeRadians)
});
}
}
function toDegrees(radians) {
return radians * 180.0 / Math.PI;
}
function toRadians(degrees) {
return degrees * (Math.PI / 180.0);
}
function differenceRadians(degrees1, degrees2) {
return toRadians(degrees1) - toRadians(degrees2);
}
</script>
</body>
</html>