Skip to content

Commit 751a676

Browse files
author
unknown
committed
Initial example code for new java mapcode library
0 parents  commit 751a676

File tree

1 file changed

+107
-0
lines changed

1 file changed

+107
-0
lines changed
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2014 Stichting Mapcode Foundation
3+
* For terms of use refer to http://www.mapcode.com/downloads.html
4+
******************************************************************************/
5+
package com.mapcode.example;
6+
7+
import java.util.ArrayList;
8+
9+
import com.mapcode.MapcodeDecoder;
10+
import com.mapcode.MapcodeEncoder;
11+
import com.mapcode.MapcodeParentTerritory;
12+
import com.mapcode.MapcodeTerritory;
13+
import com.mapcode.MapcodeTerritory.CodeFormat;
14+
import com.mapcode.Point;
15+
16+
public class MapCodeExample {
17+
public static void main(String[] args) {
18+
19+
// example 1 - disambiguate MN in several conditions
20+
System.out.println("Example 1:");
21+
String iso = "MN";
22+
System.out.println("ISO code " + iso + " if we don't care about context: "
23+
+ MapcodeTerritory.fromString(iso).toString());
24+
System.out.println("ISO code " + iso + " in USA context : "
25+
+ MapcodeTerritory.fromString(iso, MapcodeParentTerritory.USA).toString());
26+
System.out.println("ISO code " + iso + " in IND context : "
27+
+ MapcodeTerritory.fromString(iso, MapcodeParentTerritory.IND).toString());
28+
System.out.println("ISO code " + iso + " in RUS context : "
29+
+ MapcodeTerritory.fromString(iso, MapcodeParentTerritory.RUS).toString());
30+
System.out.println();
31+
32+
// example 2 - get the Mapcode Territory for a territory abbreviation 'isocode',
33+
// e.g. entered by the user
34+
35+
String isocode = "NLD"; // abbreviation for The Netherlands
36+
MapcodeTerritory mapcodeTerritory = MapcodeTerritory.fromString(isocode);
37+
38+
// example 3 - print the full English name of the territory.
39+
System.out.println("Example 2/3:");
40+
System.out.println("Territory " + isocode +": " + mapcodeTerritory.getFullName() );
41+
System.out.println();
42+
43+
// example 4 - decode some mapcodes in this territory
44+
System.out.println("Example 4 (Decode examples):");
45+
String mapcode = "49.4V";
46+
Point p = MapcodeDecoder.master_decode(mapcode, mapcodeTerritory);
47+
if (p.isDefined()) {
48+
System.out.println("Mapcode " + mapcode + " in territory " + mapcodeTerritory.toString() + " represents latitude " + p.getLatitudeDegrees() + ", longitude " + p.getLongitudeDegrees());
49+
} else {
50+
System.out.println("Mapcode " + mapcode + " in territory " + mapcodeTerritory.toString() + " is invalid");
51+
}
52+
53+
mapcode = "49.4A";
54+
p = MapcodeDecoder.master_decode(mapcode, mapcodeTerritory);
55+
if (p.isDefined()) {
56+
System.out.println("Mapcode " + mapcode + " in territory " + mapcodeTerritory.toString() + " represents latitude " + p.getLatitudeDegrees() + ", longitude " + p.getLongitudeDegrees());
57+
} else {
58+
System.out.println("Mapcode " + mapcode + " in territory " + mapcodeTerritory.toString() + " is invalid");
59+
}
60+
System.out.println();
61+
62+
// example 5 - encode a coordinate in this territory
63+
System.out.println("Example 5 (Encode examples):");
64+
double lat = 52.376514;
65+
double lon = 4.908542;
66+
67+
// get all results, exclude "world"
68+
ArrayList<String> s = MapcodeEncoder.master_encode(lat, lon, mapcodeTerritory, false, false, false);
69+
System.out.println("latitude "+ lat + ", longitude " + lon + " has "+ s.size() +" possible mapcodes in " + mapcodeTerritory.getFullName());
70+
int i = 1;
71+
for (String result: s) {
72+
String[] parts = result.split("/");
73+
String result_mapcode = parts[0];
74+
int result_ccode = Integer.parseInt(parts[1]); // the ccode
75+
76+
// let's give non-earth mapcodes an explicit, non-ambiguous territory prefix
77+
String result_prefix = "";
78+
if (result_mapcode.length() != 10) {
79+
result_prefix = MapcodeTerritory.fromTerritoryCode(result_ccode).toString(CodeFormat.MINIMAL_UNAMBIGUOUS) + " ";
80+
}
81+
System.out.println(" alternative " + String.valueOf(i++) + ": \"" + result_prefix + result_mapcode + "\"");
82+
}
83+
System.out.println();
84+
85+
// example 6 - encode a coordinate in ALL possible territories
86+
System.out.println("Example 6:");
87+
lat = 26.87016;
88+
lon = 75.847;
89+
System.out.println("All possible mapcodes in the world for latitude "+ lat +", longitude " + lon);
90+
91+
s = MapcodeEncoder.master_encode(lat, lon, null, false, false, true);
92+
93+
i=1;
94+
for (String result: s) {
95+
String[] parts = result.split("/");
96+
String result_mapcode = parts[0];
97+
int result_ccode = Integer.parseInt(parts[1]); // the ccode
98+
99+
// let's give non-earth mapcodes an explicit, non-ambiguous territory prefix
100+
String result_prefix = "";
101+
if (result_mapcode.length() != 10) {
102+
result_prefix = MapcodeTerritory.fromTerritoryCode(result_ccode).toString(CodeFormat.MINIMAL_UNAMBIGUOUS) + " ";
103+
}
104+
System.out.println(" alternative " + String.valueOf(i++) + ": \"" + result_prefix + result_mapcode + "\"");
105+
}
106+
}
107+
}

0 commit comments

Comments
 (0)