Skip to content

Commit

Permalink
Added /json methods, fixed Javascript
Browse files Browse the repository at this point in the history
  • Loading branch information
rijnb committed Nov 23, 2016
1 parent 268fdb4 commit 7aa1a6d
Show file tree
Hide file tree
Showing 12 changed files with 368 additions and 94 deletions.
2 changes: 1 addition & 1 deletion deployment/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>com.mapcode</groupId>
<artifactId>mapcode-rest-service</artifactId>
<version>2.4.1.2</version>
<version>2.4.1.3</version>
</parent>

<artifactId>deployment</artifactId>
Expand Down
76 changes: 38 additions & 38 deletions example/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ <h1>Sample HTML/Javascript using the mapcode REST API</h1>
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></p>
<b>Copyright (C) 2016, Stichting Mapcode Foundation</b>, <a href="http://www.mapcode.com">http://www.mapcode.com</a>

<hr/>
Expand All @@ -63,10 +63,10 @@ <h2>REST API Version</h2>
<script type="text/javascript">

/* This is the base URL of the mapcode REST API. */
var apiBaseURL = "https://api.mapcode.com/mapcode";
// var apiBaseURL = "https://api.mapcode.com/mapcode";

// For local testing, use:
// var apiBaseURL = "http://localhost:8080/mapcode";
var apiBaseURL = "http://localhost:8080/mapcode";

function onLoad() {
var url = apiBaseURL + "/version";
Expand All @@ -75,7 +75,7 @@ <h2>REST API Version</h2>
if (httpRequest.readyState == 4) {
versionResponseReady(httpRequest.responseText, httpRequest.status);
}
}
};
httpRequest.open("GET", url, true);
httpRequest.setRequestHeader('Accept', 'application/json');
httpRequest.send();
Expand All @@ -90,16 +90,16 @@ <h2>REST API Version</h2>

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/>";
"<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/>";
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>";
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];
Expand All @@ -108,14 +108,14 @@ <h2>REST API Version</h2>
if (territory.aliases) {
line += ", also known as: ";
for (j = 0; j < territory.aliases.length; j++) {
line += territory.aliases[j] + ' ';
line += territory.aliases[j] + " ";
}
}
return line;
}

function createAlphabetLine(alphabet) {
return "<b>" + alphabet.name + "</b></u>";
return "<b>" + alphabet.name + "<" + "/b><" + "/u>";
}
</script>

Expand All @@ -127,7 +127,7 @@ <h2>REST API Version</h2>

<script type="text/javascript">
function encodeButtonPressed() {
document.getElementById("resultEncode").innerHTML = '<font color=grey>Waiting...</font>';
document.getElementById("resultEncode").innerHTML = "<font color=grey>Waiting...<" + "/font>";

/* Use the input fields to build the request URL. */
var territory = document.getElementById("territoryEncode").value;
Expand All @@ -138,10 +138,10 @@ <h2>REST API Version</h2>

/* 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";
((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. */
Expand All @@ -150,7 +150,7 @@ <h2>REST API Version</h2>
if (httpRequest.readyState == 4) {
encodeResponseReady(httpRequest.responseText, httpRequest.status);
}
}
};
httpRequest.open("GET", url, true);
httpRequest.setRequestHeader('Accept', 'application/json');
httpRequest.send();
Expand All @@ -166,33 +166,33 @@ <h2>REST API Version</h2>
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>";
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> ";
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 + ") ";
result.mapcodes[i].territory + " ") + result.mapcodes[i].mapcode + ") ";
}

if (result.mapcodes[i].territory == "AAA") {
resultLines += "(international)";
}
if (isGrey) {
resultLines += "</font>";
resultLines += "<" + "/font>";
}
resultLines += "<br/>"
}
resultLines += "</tt>"
resultLines += "<" + "/tt>";
document.getElementById("resultEncode").innerHTML = resultLines;
}
else {
document.getElementById("resultEncode").innerHTML = "<b>Error</b> (status = " + status + ')';
document.getElementById("resultEncode").innerHTML = "<b>Error<" + "/b> (status = " + status + ")";
}
}
</script>
Expand Down Expand Up @@ -244,7 +244,7 @@ <h2>Convert a coordinate (latitude, longitude) to mapcode(s)</h2>
var url = apiBaseURL + "/coords/" + mapcode + "?context=" + context + "&allowLog=true&client=web";

/* Update page. */
document.getElementById("resultDecode").innerHTML = '<font color=grey>Waiting...</font>';
document.getElementById("resultDecode").innerHTML = "<font color=grey>Waiting...<" + "/font>";
document.getElementById("requestDecode").innerHTML = createRequestLine(url);

/* Asynchronously request to decode mapcode, call decodeResponseReady when done. */
Expand All @@ -253,7 +253,7 @@ <h2>Convert a coordinate (latitude, longitude) to mapcode(s)</h2>
if (httpRequest.readyState == 4) {
decodeResponseReady(httpRequest.responseText, httpRequest.status);
}
}
};
httpRequest.open("GET", url, true);
httpRequest.setRequestHeader('Accept', 'application/json');
httpRequest.send();
Expand All @@ -267,10 +267,10 @@ <h2>Convert a coordinate (latitude, longitude) to mapcode(s)</h2>

/* 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>";
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 + ')';
document.getElementById("resultDecode").innerHTML = "<b>Error<" + "/b> (status = " + status + ")";
}
}
</script>
Expand Down Expand Up @@ -305,12 +305,12 @@ <h2>Convert a mapcode to a coordinate (latitude, longitude)</h2>

<script>
function territoryButtonPressed() {
document.getElementById("resultTerritory").innerHTML = '<font color=grey>Waiting...</font>';
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) : "");
(context ? ("&context=" + context) : "");
document.getElementById("requestTerritory").innerHTML = createRequestLine(url);

/* Asynchronously request a solution, call encodeResponseReady when done. */
Expand All @@ -319,7 +319,7 @@ <h2>Convert a mapcode to a coordinate (latitude, longitude)</h2>
if (httpRequest.readyState == 4) {
territoryResponseReady(httpRequest.responseText, httpRequest.status);
}
}
};
httpRequest.open("GET", url, true);
httpRequest.setRequestHeader('Accept', 'application/json');
httpRequest.send();
Expand All @@ -334,21 +334,21 @@ <h2>Convert a mapcode to a coordinate (latitude, longitude)</h2>

var resultLines = "";
if (result.territories) {
resultLines += "<b><u>List all territories (" + result.territories.length + "):</u></b></u><br/><small>";
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 += "<b><u>List one territory:<" + "/u><" + "/b><" + "/u><br/><small>";
resultLines += createTerritoryLine(result);
}
resultLines += "<small>"
resultLines += "<" + "/small>";
document.getElementById("resultTerritory").innerHTML = resultLines;
}
else {
document.getElementById("resultTerritory").innerHTML = "<b>Error</b> (status = " + status + ')';
document.getElementById("resultTerritory").innerHTML = "<b>Error<" + "/b> (status = " + status + ")";
}
}
</script>
Expand Down Expand Up @@ -383,7 +383,7 @@ <h2>Get territory information</h2>

<script>
function alphabetButtonPressed() {
document.getElementById("resultAlphabet").innerHTML = '<font color=grey>Waiting...</font>';
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";
Expand All @@ -395,7 +395,7 @@ <h2>Get territory information</h2>
if (httpRequest.readyState == 4) {
alphabetResponseReady(httpRequest.responseText, httpRequest.status);
}
}
};
httpRequest.open("GET", url, true);
httpRequest.setRequestHeader('Accept', 'application/json');
httpRequest.send();
Expand All @@ -410,21 +410,21 @@ <h2>Get territory information</h2>

var resultLines = "";
if (result.alphabets) {
resultLines += "<b><u>List all alphabets (" + result.alphabets.length + "):</u></b></u><br/><small>";
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 += "<b><u>List one alphabet:<" + "/u><" + "/b><" + "/u><br/><small>";
resultLines += createAlphabetLine(result);
}
resultLines += "<small>"
resultLines += "<" + "/small>";
document.getElementById("resultAlphabet").innerHTML = resultLines;
}
else {
document.getElementById("resultAlphabet").innerHTML = "<b>Error</b> (status = " + status + ')';
document.getElementById("resultAlphabet").innerHTML = "<b>Error<" + "/b> (status = " + status + ")";
}
}
</script>
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<artifactId>mapcode-rest-service</artifactId>

<packaging>pom</packaging>
<version>2.4.1.2</version>
<version>2.4.1.3</version>

<name>Mapcode REST API Web Service</name>
<description>
Expand Down
2 changes: 1 addition & 1 deletion service/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<parent>
<groupId>com.mapcode</groupId>
<artifactId>mapcode-rest-service</artifactId>
<version>2.4.1.2</version>
<version>2.4.1.3</version>
</parent>

<artifactId>service</artifactId>
Expand Down
Loading

0 comments on commit 7aa1a6d

Please sign in to comment.