Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ba0207a

Browse files
committed
Added Swift explanation
1 parent 6d4dd79 commit ba0207a

File tree

3 files changed

+90
-2
lines changed

3 files changed

+90
-2
lines changed

README.md

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,9 +177,73 @@ To add individual support support for other languages (of all territory names),
177177

178178
The list of support languages may grow over time.
179179

180+
## Using the C Library in an Xcode iOS Swift project
181+
182+
You can use this C library in an iOS application, built with Swift in Xcode, fairly easily.
183+
All you need to do is:
184+
185+
First, copy the directory `mapcodelib` to the source directory of your iOS application.
186+
187+
Then, add a text file called `module.modulemap` in the directory `mapcodelib` to export all symbols
188+
from the C library to Swift (note that `#defines` are not exported):
189+
190+
```
191+
module mapcodelib [system][extern_c]{
192+
header "mapcode_alphabets.h"
193+
header "mapcode_territories.h"
194+
header "mapcoder.h"
195+
export *
196+
}
197+
```
198+
199+
Now, you can access the C library methods like this in a Swift project:
200+
201+
```
202+
// Example of decoding a full mapcode to a lat/lon:
203+
let territory = getTerritoryCode(context, TERRITORY_NONE)
204+
var lat: Double = 0.0
205+
var lon: Double = 0.0
206+
let mapcodeError = decodeMapcodeToLatLonUtf8(&lat, &lon, fullMapcode, territory, nil)
207+
if mapcodeError == ERR_OK {
208+
// Use the decoded lat and lon.
209+
} else {
210+
// Something went wrong decoding the full mapcode string.
211+
}
212+
```
213+
Or encode a latitude, longitude pair to a set of Mapcodes like this:
214+
215+
```
216+
let buffer = UnsafeMutablePointer<Int8>.allocate(capacity: Int(_MAX_MAPCODE_RESULT_ASCII_LEN))
217+
buffer.initialize(to: 0, count: Int(_MAX_MAPCODE_RESULT_ASCII_LEN))
218+
var total: Int32 = 0;
219+
var i: Int32 = 0
220+
repeat {
221+
total = encodeLatLonToSelectedMapcode(buffer, lat, lon, TERRITORY_NONE, 0, i)
222+
if (total > 0) {
223+
let mapcode = String.init(cString: buffer);
224+
}
225+
i = i + 1
226+
} while (i < total)
227+
```
228+
229+
Or get a territory name like this:
230+
231+
```
232+
let buffer = UnsafeMutablePointer<CChar>.allocate(capacity: Int(MAX_TERRITORY_FULLNAME_UTF8_LEN + 1))
233+
buffer.initialize(to: 0, count: Int(_MAX_TERRITORY_FULLNAME_UTF8_LEN + 1))
234+
235+
// Get alpha code.
236+
getTerritoryIsoName(buffer, TERRITORY_NLD, 0)
237+
let alphaCode = String.init(cString: buffer)
238+
239+
// Get full name.
240+
getFullTerritoryNameEnglish(buffer, TERRITORY_NLD, 0)
241+
let fullName = String.init(cString: buffer)
242+
```
243+
180244
## Release Notes
181245

182-
### 2.5.4
246+
### 2.5.4 - 2.5.5
183247

184248
* Added `encodeLatLonToSelectedMapcode` as a convenience for languages that use the
185249
C library, but have difficulties dealing with multi-dimensional arrays (like Swift).

mapcodelib/mapcoder.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@
5353
#include "internal_territory_names_tr.h"
5454
#include "internal_territory_names_uk.h"
5555

56+
// The constants are also exported as variables, to allow other languages to use them.
57+
char *_MAPCODE_C_VERSION = MAPCODE_C_VERSION;
58+
int _MAX_NR_OF_MAPCODE_RESULTS = MAX_NR_OF_MAPCODE_RESULTS;
59+
int _MAX_PRECISION_DIGITS = MAX_PRECISION_DIGITS;
60+
int _MAX_PROPER_MAPCODE_ASCII_LEN = MAX_PROPER_MAPCODE_ASCII_LEN;
61+
int _MAX_ISOCODE_ASCII_LEN = MAX_ISOCODE_ASCII_LEN;
62+
int _MAX_CLEAN_MAPCODE_ASCII_LEN = MAX_CLEAN_MAPCODE_ASCII_LEN;
63+
int _MAX_MAPCODE_RESULT_ASCII_LEN = MAX_MAPCODE_RESULT_ASCII_LEN;
64+
int _MAX_TERRITORY_FULLNAME_UTF8_LEN = MAX_TERRITORY_FULLNAME_UTF8_LEN;
65+
int _MAX_MAPCODE_RESULT_UTF8_LEN = MAX_MAPCODE_RESULT_UTF8_LEN;
66+
int _MAX_MAPCODE_RESULT_UTF16_LEN = MAX_MAPCODE_RESULT_UTF16_LEN;
67+
int _MAX_ALPHABETS_PER_TERRITORY = MAX_ALPHABETS_PER_TERRITORY;
5668

5769
#ifdef DEBUG
5870

mapcodelib/mapcoder.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ extern "C" {
5858
#define MAPCODE_SUPPORT_LANGUAGE_UK
5959
#endif
6060

61-
#define MAPCODE_C_VERSION "2.5.3"
61+
#define MAPCODE_C_VERSION "2.5.5"
6262
#define UWORD unsigned short int // 2-byte unsigned integer.
6363

6464
#define MAX_NR_OF_MAPCODE_RESULTS 22 // Max. number of results ever returned by encoder (e.g. for 26.904899, 95.138515).
@@ -73,6 +73,18 @@ extern "C" {
7373
#define MAX_MAPCODE_RESULT_UTF8_LEN (MAX_MAPCODE_RESULT_ASCII_LEN * 3) // One mapcode character can become at most 3 UTF8characters.
7474
#define MAX_MAPCODE_RESULT_UTF16_LEN (MAX_MAPCODE_RESULT_ASCII_LEN) // Each mapcode character can become one UTF16 word.
7575

76+
// The constants are also exported as variables, to allow other languages to use them.
77+
extern char* _MAPCODE_C_VERSION;
78+
extern int _MAX_NR_OF_MAPCODE_RESULTS;
79+
extern int _MAX_PRECISION_DIGITS;
80+
extern int _MAX_PROPER_MAPCODE_ASCII_LEN;
81+
extern int _MAX_ISOCODE_ASCII_LEN;
82+
extern int _MAX_CLEAN_MAPCODE_ASCII_LEN;
83+
extern int _MAX_MAPCODE_RESULT_ASCII_LEN;
84+
extern int _MAX_TERRITORY_FULLNAME_UTF8_LEN;
85+
extern int _MAX_MAPCODE_RESULT_UTF8_LEN;
86+
extern int _MAX_MAPCODE_RESULT_UTF16_LEN;
87+
extern int _MAX_ALPHABETS_PER_TERRITORY;
7688

7789
/**
7890
* The type Mapcodes hold a number of mapcodes, for example from an encoding call.

0 commit comments

Comments
 (0)