-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathwhat3words.js
More file actions
58 lines (47 loc) · 1.57 KB
/
Copy pathwhat3words.js
File metadata and controls
58 lines (47 loc) · 1.57 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
var what3words = new function (language) {
this.API_KEY = 'YOURAPIKEY'; // Change to your what3words API key
this.language = language || 'en'; // Change to your default language
// --
this.postRequest = function (url, data, callback) {
data.key = this.API_KEY;
data.lang = this.language;
$.post('http://api.what3words.com/' + url, data, callback, 'JSON');
};
/**
* Sets the language in which to return words
*/
this.setLanguage = function (language) {
if (language !== undefined)
this.language = language;
};
/**
* Convert 3 words into position
* Takes words either as string, or array of words
* Returns array of [lat, long]
*/
this.wordsToPosition = function (words, callback) {
if (typeof words === 'undefined')
throw 'No valid words passed';
else if (typeof words === 'object')
words = words.join('.');
else if (typeof words !== 'string')
throw 'Invalid words passed';
var data = { string: words };
this.postRequest('w3w', data, function (ret) { callback(ret.position); });
};
/**
* Convert a position into 3 words
* Takes position either as string, or array of [lat, lng]
* Returns array of [word1, word2, word3]
*/
this.positionToWords = function (position, callback) {
if (typeof position === 'undefined')
throw 'No valid position passed';
else if (typeof position === 'object')
position = position.join(',');
else if (typeof position !== 'string')
throw 'Invalid position passed';
var data = { position: position };
this.postRequest('position', data, function (ret) { callback(ret.words); });
};
};