-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.html
More file actions
460 lines (384 loc) · 17.5 KB
/
Copy pathindex.html
File metadata and controls
460 lines (384 loc) · 17.5 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
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
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
<!--
~ Copyright (C) 2016-2020, Stichting Mapcode Foundation (http://www.mapcode.com)
~
~ Licensed under the Apache License, Version 2.0 (the "License");
~ you may not use this file except in compliance with the License.
~ You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing, software
~ distributed under the License is distributed on an "AS IS" BASIS,
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
~ See the License for the specific language governing permissions and
~ limitations under the License.
-->
<html>
<head>
<title>Mapcode REST API Example</title>
<style type="text/css">
body {
margin: 5px 10px 5px 10px;
font-family: verdana, geneva, arial, helvetica, sans-serif;
font-size: 12px;
background-color: #eeeeee;
text-decoration: none;
font-weight: normal;
line-height: normal;
}
</style>
</head>
<body onload="onLoad()">
<h1>Sample HTML/Javascript using the mapcode REST API</h1>
This page is an example of how the mapcode REST API can be used from HTML/Javascript.
You can have a look at the source code of this page to see how the REST API is called,
or you can try it out by simply entering some information in the dialog boxes and
pressing the conversion/request buttons.
<p></p>
<b>Copyright (C) 2016, Stichting Mapcode Foundation</b>, <a href="http://www.mapcode.com">http://www.mapcode.com</a>
<hr/>
<h2>REST API Version</h2>
<p>
<tt>HTTP GET /mapcode/version</tt>
</p>
<b><u>REST API Version:</u></b>
<tt>
<div id="version">(waiting...)</div>
</tt>
<hr/>
<!--
~ --------------------------------------------------------------------------------
~ UTILITY FUNCTIONS
~ --------------------------------------------------------------------------------
-->
<script type="text/javascript">
/* This is the base URL of the mapcode REST API. */
var apiBaseURL = "https://api.mapcode.com/mapcode";
// For local testing, use:
// var apiBaseURL = "http://localhost:8080/mapcode";
function onLoad() {
var url = apiBaseURL + "/version";
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState == 4) {
versionResponseReady(httpRequest.responseText, httpRequest.status);
}
};
httpRequest.open("GET", url, true);
httpRequest.setRequestHeader('Accept', 'application/json');
httpRequest.send();
}
function versionResponseReady(response, status) {
if ((status == 200) && response) {
var result = JSON.parse(response);
document.getElementById("version").innerHTML = result.version;
}
}
function createResponseLine(status, result) {
return "<small>" + ((status == 200) ? "<font color=\"#228800\">" : "<font color=\"cc2222\">") +
"<b>Response:<" + "/b><br><tt>" + result + "<" + "/tt><" + "/font><" + "/small><" + "/font><br/><br/>";
}
function createRequestLine(request) {
return "<font color=\"#228800\"><small><b>Request:<" + "/b><br><tt>HTTP GET " + request + "<" + "/tt><" + "/small><" + "/font><br/><br/>";
}
function createTerritoryLine(territory) {
var line = "<b>" + territory.alphaCode + "<" + "/b> - <u>" + territory.fullName + "<" + "/u>";
if (territory.fullNameAliases) {
for (j = 0; j < territory.fullNameAliases.length; j++) {
line += ", " + territory.fullNameAliases[j];
}
}
if (territory.aliases) {
line += ", also known as: ";
for (j = 0; j < territory.aliases.length; j++) {
line += territory.aliases[j] + " ";
}
}
return line;
}
function createAlphabetLine(alphabet) {
return "<b>" + alphabet.name + "<" + "/b><" + "/u>";
}
</script>
<!--
~ --------------------------------------------------------------------------------
~ ENCODE LAT/LON TO MAPCODE
~ --------------------------------------------------------------------------------
-->
<script type="text/javascript">
function encodeButtonPressed() {
document.getElementById("resultEncode").innerHTML = "<font color=grey>Waiting...<" + "/font>";
/* Use the input fields to build the request URL. */
var territory = document.getElementById("territoryEncode").value;
var precision = document.getElementById("precision").value;
var alphabet = document.getElementById("alphabetEncode").value;
var lat = document.getElementById("lat").value;
var lon = document.getElementById("lon").value;
/* Add include=territory,alphabet to make sure the territory and alphabet values are always present. */
var url = apiBaseURL + "/codes/" + lat + "," + lon + "?include=territory,alphabet" +
((territory && (territory.length !== 0)) ? "&territory=" + territory : "") +
((precision && (precision.length !== 0)) ? "&precision=" + precision : "") +
((alphabet && (alphabet.length !== 0)) ? "&alphabet=" + alphabet : "") +
"&allowLog=true&client=web";
document.getElementById("requestEncode").innerHTML = createRequestLine(url);
/* Asynchronously request a solution, call encodeResponseReady when done. */
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState == 4) {
encodeResponseReady(httpRequest.responseText, httpRequest.status);
}
};
httpRequest.open("GET", url, true);
httpRequest.setRequestHeader('Accept', 'application/json');
httpRequest.send();
}
function encodeResponseReady(response, status) {
/* Did the request succeed? */
document.getElementById("responseEncode").innerHTML = createResponseLine(status, response);
if ((status == 200) && response) {
/* Parse the response into an array of mapcodes. */
var result = JSON.parse(response);
/* Print the mapcodes, grey them if they are not the shortest (i.e. the first) in a territory. */
var resultLines = "<b><u>There are " + result.mapcodes.length + " possible mapcodes:<" + "/b><" + "/u><br><tt>";
for (i = 0; i < result.mapcodes.length; i++) {
var isGrey = ((i > 0) && (result.mapcodes[i].territory == result.mapcodes[i - 1].territory));
if (isGrey) {
resultLines += "<font color=\"#888888\">";
}
resultLines += ((result.mapcodes[i].territory == "AAA") ? "" :
result.mapcodes[i].territoryInAlphabet + " ") + "<b>" + result.mapcodes[i].mapcodeInAlphabet + "<" + "/b> ";
if (result.mapcodes[i].mapcode != result.mapcodes[i].mapcodeInAlphabet) {
resultLines += " (" + ((result.mapcodes[i].territory == "AAA") ? "" :
result.mapcodes[i].territory + " ") + result.mapcodes[i].mapcode + ") ";
}
if (result.mapcodes[i].territory == "AAA") {
resultLines += "(international)";
}
if (isGrey) {
resultLines += "<" + "/font>";
}
resultLines += "<br/>"
}
resultLines += "<" + "/tt>";
document.getElementById("resultEncode").innerHTML = resultLines;
}
else {
document.getElementById("resultEncode").innerHTML = "<b>Error<" + "/b> (status = " + status + ")";
}
}
</script>
<h2>Convert a coordinate (latitude, longitude) to mapcode(s)</h2>
<p>
<tt>HTTP GET /mapcode/codes/{lat},{lon}[?territory={restrictToTerritory}&precision=[0..8]]</tt>
</p>
<form action="#" onsubmit="return encodeButtonPressed();">
<input type="text" id="lat" value="51.427624"> = latitude [-90, 90] (must be in range)
<br/>
<input type="text" id="lon" value="5.488075"> = longitude [-180, 180] (wrapped if outside of range)
<br/>
<input type="text" id="territoryEncode" value=""> = restrict results to one territory (use territory name, e.g. NLD, empty = show
all)
<br/>
<input type="text" id="alphabetEncode" value=""> = alphabet used for results (use alphabet name, e.g. GREEK, empty = ROMAN)
<br/>
<input type="text" id="precision" value=""> = precision (use '0'..'8', empty = '0')
<br/>
<input type="button" value="Encode" onclick="encodeButtonPressed()">
</form>
<div id="resultEncode"></div>
<br/>
<div id="requestEncode"></div>
<div id="responseEncode"></div>
<hr/>
<!--
~ --------------------------------------------------------------------------------
~ DECODE MAPCODE TO LAT/LON
~ --------------------------------------------------------------------------------
-->
<script type="text/javascript">
function decodeButtonPressed() {
/* Use the input fields to build the request URL. */
var context = document.getElementById("contextDecode").value;
if (context == "") {
context = "AAA";
}
var mapcode = document.getElementById("mapcode").value;
var url = apiBaseURL + "/coords/" + mapcode + "?context=" + context + "&allowLog=true&client=web";
/* Update page. */
document.getElementById("resultDecode").innerHTML = "<font color=grey>Waiting...<" + "/font>";
document.getElementById("requestDecode").innerHTML = createRequestLine(url);
/* Asynchronously request to decode mapcode, call decodeResponseReady when done. */
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState == 4) {
decodeResponseReady(httpRequest.responseText, httpRequest.status);
}
};
httpRequest.open("GET", url, true);
httpRequest.setRequestHeader('Accept', 'application/json');
httpRequest.send();
}
function decodeResponseReady(response, status) {
/* Did the request succeed? */
document.getElementById("responseDecode").innerHTML = createResponseLine(status, response);
if ((status == 200) && response) {
/* Parse the response into a coordinate. */
var result = JSON.parse(response);
document.getElementById("resultDecode").innerHTML = "<b><u>Coordinate:<" + "/u><" + "/b><br><tt>(" + result.latDeg + ", " + result.lonDeg + ")<" + "/tt>";
}
else {
document.getElementById("resultDecode").innerHTML = "<b>Error<" + "/b> (status = " + status + ")";
}
}
</script>
<h2>Convert a mapcode to a coordinate (latitude, longitude)</h2>
<p>
<tt>HTTP GET /mapcode/coords/{code}[?context={territoryContext}]</tt>
</p>
<form action="#" onsubmit="return decodeButtonPressed();">
<input type="text" id="contextDecode" value="NLD"> = mapcode territory (use territory name, e.g. NLD, empty = international)
<br/>
<input type="text" id="mapcode" value="XX.XX"> = mapcode code
<br/>
<input type="button" value="Decode" onclick="decodeButtonPressed()">
</form>
<div id="resultDecode"></div>
<br/>
<div id="requestDecode"></div>
<div id="responseDecode"></div>
<hr>
<!--
~ --------------------------------------------------------------------------------
~ TERRITORY INFORMATION
~ --------------------------------------------------------------------------------
-->
<script>
function territoryButtonPressed() {
document.getElementById("resultTerritory").innerHTML = "<font color=grey>Waiting...<" + "/font>";
var territory = document.getElementById("territory").value;
var context = document.getElementById("context").value;
var url = apiBaseURL + "/territories" + (territory ? ('/' + territory) : "") + "?allowLog=true&client=web" +
(context ? ("&context=" + context) : "");
document.getElementById("requestTerritory").innerHTML = createRequestLine(url);
/* Asynchronously request a solution, call encodeResponseReady when done. */
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState == 4) {
territoryResponseReady(httpRequest.responseText, httpRequest.status);
}
};
httpRequest.open("GET", url, true);
httpRequest.setRequestHeader('Accept', 'application/json');
httpRequest.send();
}
function territoryResponseReady(response, status) {
/* Did the request succeed? */
document.getElementById("responseTerritory").innerHTML = createResponseLine(status, response);
if ((status == 200) && response) {
var result = JSON.parse(response);
var resultLines = "";
if (result.territories) {
resultLines += "<b><u>List all territories (" + result.territories.length + "):<" + "/u><" + "/b><" + "/u><br/><small>";
for (i = 0; i < result.territories.length; i++) {
var territory = result.territories[i];
resultLines += createTerritoryLine(territory) + "<br/>";
}
}
else {
resultLines += "<b><u>List one territory:<" + "/u><" + "/b><" + "/u><br/><small>";
resultLines += createTerritoryLine(result);
}
resultLines += "<" + "/small>";
document.getElementById("resultTerritory").innerHTML = resultLines;
}
else {
document.getElementById("resultTerritory").innerHTML = "<b>Error<" + "/b> (status = " + status + ")";
}
}
</script>
<h2>Get territory information</h2>
<p>
<tt>HTTP GET /mapcode/territory[/{territory}[?context={territoryContext}]]</tt>
</p>
<form action="#" onsubmit="return decodeButtonPressed();">
<input type="text" id="territory" value=""> = territory to show info for (use territory name, e.g. NLD, empty = all territories)
<br/>
<input type="text" id="context" value=""> = territory context (use parent territory, e.g. IND, for disambiguation)
<br/>
<input type="button" value="List" onclick="territoryButtonPressed()">
</form>
<div id="resultTerritory"></div>
<br/>
<div id="requestTerritory"></div>
<div id="responseTerritory"></div>
<hr/>
<!--
~ --------------------------------------------------------------------------------
~ ALPHABET INFORMATION
~ --------------------------------------------------------------------------------
-->
<script>
function alphabetButtonPressed() {
document.getElementById("resultAlphabet").innerHTML = "<font color=grey>Waiting...<" + "/font>";
var alphabet = document.getElementById("alphabet").value;
var url = apiBaseURL + "/alphabets" + (alphabet ? ('/' + alphabet) : "") + "?allowLog=true&client=web";
document.getElementById("requestAlphabet").innerHTML = createRequestLine(url);
/* Asynchronously request a solution, call encodeResponseReady when done. */
var httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function () {
if (httpRequest.readyState == 4) {
alphabetResponseReady(httpRequest.responseText, httpRequest.status);
}
};
httpRequest.open("GET", url, true);
httpRequest.setRequestHeader('Accept', 'application/json');
httpRequest.send();
}
function alphabetResponseReady(response, status) {
/* Did the request succeed? */
document.getElementById("responseAlphabet").innerHTML = createResponseLine(status, response);
if ((status == 200) && response) {
var result = JSON.parse(response);
var resultLines = "";
if (result.alphabets) {
resultLines += "<b><u>List all alphabets (" + result.alphabets.length + "):<" + "/u><" + "/b><" + "/u><br/><small>";
for (i = 0; i < result.alphabets.length; i++) {
var alphabet = result.alphabets[i];
resultLines += createAlphabetLine(alphabet) + "<br/>";
}
}
else {
resultLines += "<b><u>List one alphabet:<" + "/u><" + "/b><" + "/u><br/><small>";
resultLines += createAlphabetLine(result);
}
resultLines += "<" + "/small>";
document.getElementById("resultAlphabet").innerHTML = resultLines;
}
else {
document.getElementById("resultAlphabet").innerHTML = "<b>Error<" + "/b> (status = " + status + ")";
}
}
</script>
<h2>Get alphabet information</h2>
<p>
<tt>HTTP GET /mapcode/alphabet[/{alphabet}[?context={territoryContext}]]</tt>
</p>
<form action="#" onsubmit="return decodeButtonPressed();">
<input type="text" id="alphabet" value=""> = alphabet to show info for (use alphabet name, e.g. GREEK, empty = all alphabets)
<br/>
<input type="button" value="List" onclick="alphabetButtonPressed()">
</form>
<div id="resultAlphabet"></div>
<br/>
<div id="requestAlphabet"></div>
<div id="responseAlphabet"></div>
<hr/>
<p><font color="#882222">
<b>Important:</b> The Mapcode REST API on <tt>api.mapcode.com</tt> is provided primarily
for examples and reference purposes. If you plan to use the REST API more extensively,
please consider running your own REST API server. You can find the REST API server sources
and installation notes on
<a href="https://github.com/mapcode-foundation/mapcode-rest-service" target="_blank">Github</a>.
</font></p>
</body>
</html>