Skip to content

Commit 0a1ecca

Browse files
committed
Added help text to README.md
1 parent 89c851a commit 0a1ecca

1 file changed

Lines changed: 330 additions & 0 deletions

File tree

README.md

Lines changed: 330 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,336 @@ The following files provide the Javascript interfaces for mapcodes:
4545

4646
To run the Javascript unit tests, simply open the file `unittest/unittest/html`.
4747

48+
# Using the Library
49+
50+
## Converting a Coordinate into a Mapcode
51+
52+
In the mapcode system, territories are limited by rectangles, not by actual or political borders.
53+
This means that a coordinate will often yield mapcode possibilities in more than one territory.
54+
Each possibility correctly represents the coordinate, but which one is *politically* correct is a
55+
choice that must be made (in advance or afterwards) by the caller or user of the routines.
56+
57+
There are several routines to generate the possible mapcodes for a coordinate.
58+
The right one to use depends on the situation. All routines take a latitude and a longitude,
59+
and all routines yield an array of possible mapcodes, with the following structure:
60+
61+
attribute | description
62+
--- | ---
63+
fullmapcode | string, full mapcode including full territory alphacode
64+
mapcode | string, full mapcode excluding territory alphacode
65+
territoryAlphaCode | string, full territory alphacode
66+
67+
### The Shortest Mapcode
68+
69+
The most widely used routine to generate mapcodes is probably:
70+
71+
encodeShortest (lat, lon, territory)
72+
73+
parameter | description
74+
--- | ---
75+
lat | latitude in degrees (maximized by routine to 90.0 and minimized to -90.0)
76+
lon | longitude in degrees (all values allowed, wrapped to -180.0 and +180.0)
77+
territory | a territory (an iso code such as “NLD”)
78+
return value | an array of result records (see above)
79+
80+
This routine will yield at most one result: the shortest mapcode (if any) that exists
81+
for that coordinate within the specified territory. Such a mapcode is also
82+
sometimes called the “default mapcode” for a particular territory.
83+
84+
Example Javascript:
85+
86+
function displayMapcodes(e) {
87+
document.write('<u>',e.length,' result(s)</u><br>');
88+
for(var i=0;i<e.length;i++) {
89+
if ( e[i].territoryAlphaCode != "AAA" )
90+
document.write(e[i].territoryAlphaCode,' ');
91+
document.write('<b>',e[i].mapcode,'</b><br>');
92+
}
93+
}
94+
displayMapcodes( encodeShortest( 34.00956,-118.210572, 'US-CA' ) );
95+
96+
Output:
97+
98+
1 result(s):
99+
US-CA XX.XX
100+
101+
To get a mapcode for every territory in which a coordinate can be encoded, use
102+
103+
encodeShortest (lat, lon)
104+
105+
Since any coordinate is somewhere on the world, this routine is guaranteed to deliver at least one possibility.
106+
107+
Example Javascript:
108+
109+
var e = encodeShortest( 34.00956,-118.210572 );
110+
111+
Output using displayMapcodes(e):
112+
113+
3 result(s)
114+
US-CA XX.XX
115+
USA KKYP.19MN
116+
R59KJ.W5FT
117+
118+
This particular coordinate thus has a California mapcode (XX.XX), a national 8-letter mapcode,
119+
and (like any coordinate) an international 9-letter mapcode (by tradition shown without its
120+
alphacode “AAA”).
121+
122+
### All Possible Mapcodes
123+
124+
Even within a particular territory, there are often several possible mapcodes.
125+
126+
encode(lat,lon, territory)
127+
128+
will yield all possible mapcodes (if any) within the specified territory, while
129+
130+
encode(lat, lon)
131+
132+
will simply yield all mapcodes that can represent the coordinate.
133+
134+
Example Javascript:
135+
136+
var e = encode( 36.115, -115.1731, 'US-NV' );
137+
138+
Output using `displayMapcodes(e)`:
139+
140+
5 result(s)
141+
US-NV CN.NN
142+
US-NV BX.5KF
143+
US-NV NJ7.127
144+
US-NV F978.JY1
145+
US-NV L70X.6V4G
146+
147+
The results are ordered by length, the first is the shortest (the default).
148+
The last is usually the “national” encoding, i.e. a format that all coordinates within a
149+
country share. Of course, the shortest mapcode is preferred in almost every circumstance.
150+
151+
Example Javascript:
152+
153+
var e = encode( 36.115, -115.1731 );
154+
155+
Output using `displayMapcodes(e)`:
156+
157+
9 result(s)
158+
US-NV CN.NN
159+
US-NV BX.5KF
160+
US-NV NJ7.127
161+
US-NV F978.JY1
162+
US-NV L70X.6V4G
163+
US-CA F978.JY1
164+
US-CA L70X.6V4G
165+
USA L70X.6V4G
166+
R5KDM.C8C8
167+
168+
Note that two of these (in US-CA) are politically incorrect, since the specified coordinate
169+
lies within the borders of Nevada and not California. However, they are valid in that the
170+
mapcodes correctly represent the original coordinate.
171+
172+
### International Mapcodes
173+
174+
For very specific applications, the following routine only returns the international mapcode
175+
176+
encodeInternational (lat, lon)
177+
178+
returns an array with exactly one result: the 9-letter international mapcode representing
179+
the coordinate.
180+
181+
### Higher Precision Mapcodes
182+
183+
A mapcode represents a coordinate with a precision of a few meters. To be precise,
184+
most mapcodes represent the center point of a 10x10 meter area. The coordinate may
185+
thus be 5 meters off both longitudinally and latitudinal, or 3,6 meters on average.
186+
This is precise enough for everyday use, but mapcodes can also be generate with higher
187+
precisions. With one extra letter (after a hyphen), a mapcode represents a 2x2 meter area,
188+
with two letters, an area of a square foot. The extra letters defeat the purpose of the
189+
mapcode system (which is to offer short, easy codes for everyday use) but there may be
190+
cases where the extra letters are appropriate.
191+
192+
For all routines mentioned, there are “`WithPrecision`” variants:
193+
194+
encodeShortestWithPrecision(lat, lon, territory, precision)
195+
encodeShortestWithPrecision(lat, lon, precision)
196+
encodeWithPrecision(lat, lon, territory, precision)
197+
encodeWithPrecision(lat, lon, precision)
198+
encodeInternationalWithPrecision(lat, lon, precision)
199+
200+
where precision is an integer specifying how many extra letters must be generated.
201+
Using the value 0 just yields normal mapcodes.
202+
203+
## Converting a Mapcode into a Coordinate
204+
205+
Given a string with a mapcode (which may or may not include a territory alphacode and may or may not include high-precision letters at the end), the following routines are available to decode them back to a coordinate:
206+
207+
decode(mapcodeString)
208+
209+
parameter | description |
210+
--- | ---
211+
mapcodeString | a string containing a mapcode
212+
return value | an object with fields x (longitude) and y (latitude), or false
213+
214+
This routine is sufficient to decode a full mapcodeString into a coordinate.
215+
216+
Example Javascript:
217+
218+
var result = decode( 'NLD 49.4V' );
219+
document.write( result.y + ',' + result.x + '<BR>' );
220+
221+
Output:
222+
223+
52.376514, 4. 908543375
224+
225+
However, in daily life you will often have to cope with mapcodes provided by people who
226+
abbreviate or completely leave out the alphacode of the territory. For example, the
227+
alphacode `US-AR` may have been abbreviated to `AR`, which within the context of the
228+
United States of America clearly identifies the state of Arkansas, but could in fact
229+
just as well represent Arunachal Pradesh, India. Someone in The Netherlands may even
230+
abbreviate a mapcode to just “`49.4V`”, assuming that the country is obvious.
231+
232+
The following routine provides an argument to “help” decoding such input:
233+
234+
decode(mapcodeString, contextTerritory)
235+
236+
parameter | description
237+
--- | ---
238+
mapcodeString | a string containing a mapcode
239+
contextTerritory | a string (an ISO code such as “NLD”)
240+
returns | an object with fields x (longitude) and y (latitude), or false
241+
242+
By passing a context territory, you allow the decode routine to solve any ambiguities.
243+
244+
Example Javascript:
245+
246+
// Assume the user of the system is Delhi, India
247+
var defaultcontext = 'IN-DL';
248+
249+
var result;
250+
result = decode( 'US-AR 49.4V', defaultcontext );
251+
document.write( result.y + ',' + result.x + '<BR>' );
252+
result = decode( 'AR 49.4V', defaultcontext );
253+
document.write( result.y + ',' + result.x + '<BR>' );
254+
result = decode( '49.4V', defaultcontext );
255+
document.write( result.y + ',' + result.x + '<BR>' );
256+
result = decode( 'xxx.xxxx', defaultcontext );
257+
document.write( result.y + ',' + result.x + '<BR>' );
258+
259+
Output:
260+
261+
34.77035,-92.3273875
262+
27.0693605,93.59582
263+
28.648506,77.183788
264+
24.423323,92.506517
265+
266+
Thus, the first, complete mapcode (`US-AR 49.4V`) does not require the provided context,
267+
and simply returns a coordinate in Arkansas. But the second, specifying only `AR`,
268+
is interpreted as `IN-AR` (without the context, it might just as well have been
269+
interpreted as Arkansas, USA). The third and fourth examples specify no territory
270+
whatsoever so could never be interpreted correctly without the aid of the context provided.
271+
The third, `49.4V`, is simply interpreted within the specified context, `IN-DL`, and yields a
272+
New Delhi address. The fourth can not be interpreted in the state of Delhi, but can be
273+
interpreted in India and yields a coordinate in Assam. Note that the input “`49.4V`
274+
would not have yielded results if the context had been `IND` (since many states in
275+
India have mapcode `49.4V`).
276+
277+
## Routines Related to Territories
278+
279+
getTerritoryFullname(territory)
280+
281+
parameter | description
282+
--- | ---
283+
territory | an string (an ISO code such as “NLD”)
284+
return value | string (the full name of the territory)
285+
286+
isSubdivision(territory)
287+
288+
parameter | description
289+
--- | ---
290+
territory | an string (an ISO code such as “NLD”)
291+
return value | true iff territory is a subdivision of a country
292+
293+
hasSubdivisions(territory)
294+
295+
parameter | description
296+
--- | ---
297+
territory | an string (an ISO code such as “NLD”)
298+
return value | true iff territory is a country that has subdivisions
299+
300+
getTerritoryAlphaCode(territory, format)
301+
302+
parameter | description
303+
--- | ---
304+
territory | string, or internal integer between 0 and ccode_earth
305+
format | 0: leave out country for subdivisions
306+
. | undefined or 1: always return full abbreviation
307+
. | 2: leave out country for subdivisions unless this would make the abbreviation ambigious
308+
return value | territory abbreviation in the specified format, or empty
309+
310+
## Routines Related to Distance
311+
312+
distanceInMeters(lat1, lon1, lat2, lon2)
313+
314+
parameter | description
315+
--- | ---
316+
lat1, lon1 | a latitude/longitude (in degrees)
317+
lat2, lon2 | another latitude/longitude (in degrees)
318+
return value | distance between the coordinates, in meters
319+
320+
**Note:** estimate, only correct for coordinates that are within a few miles of each other.
321+
322+
maxErrorInMeters(precision)
323+
324+
parameter | description
325+
--- | ---
326+
precision | the number of “high precision digits” in a mapcode
327+
return value | worst-case distance in meters between the original coordinate and the decode location of the mapcode.
328+
329+
## Routines Related to Unicode and Alphabets
330+
331+
Mapcodes may be specified in other alphabets. These routines takes a mapcode in any
332+
alphabet and returns its equivalent in the target alphabet. Characters that have no
333+
roman equivalent in the mapcode system are replaced by question marks. Territories
334+
(in fact anything before a separating space) are left untouched.
335+
336+
convertToAlphabet(mapcode, targetAlphabet)
337+
convertToAlphabetAsHTML(mapcode, targetAlphabet)
338+
339+
parameter | description
340+
--- | ---
341+
mapcode | a string
342+
targetAlphabet | an integer (identifying one of the languages)
343+
return value | a string
344+
345+
Example Javascript:
346+
347+
document.write(convertToAlphabetAsHTML( ‘PQ.RS’, 4 ) ,’<br>’)
348+
document.write(convertToAlphabetAsHTML( ‘PQ.RS’, 2 ) ,’<br>’)
349+
350+
Output:
351+
352+
नप.भम
353+
РФ.ЯЦ
354+
355+
The following alphabets have been approved for official use,
356+
357+
0: roman
358+
2: cyrillic
359+
4: hindi
360+
12: gurmukhi
361+
362+
Other values are available but have not yet officially been formally approved:
363+
364+
1: Greek
365+
3: Hebrew
366+
5: Malay
367+
6: Georgian
368+
7: Katakana
369+
8: Thai
370+
9: Lao
371+
10: Armenian
372+
11: Bengali
373+
13: Tibetan
374+
375+
Please check http://www.mapcode.com to see if there is a more up-to-date version.
376+
377+
48378
# Version History
49379

50380
### 2.4.0

0 commit comments

Comments
 (0)