-
Notifications
You must be signed in to change notification settings - Fork 6
/
control.w3w.js
78 lines (69 loc) · 2.21 KB
/
control.w3w.js
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
L.Control.w3w = L.Control.extend({
options: {
position: 'bottomleft',
locationText:'- - -',
promptText: 'Press Ctrl+C to copy location',
precision: 4,
apikey : "YOURKEYHERE" //your api key
},
initialize: function(options)
{
L.Control.prototype.initialize.call(this, options);
},
onAdd: function(map)
{
var className = 'leaflet-control-w3w',
that = this,
container = this._container = L.DomUtil.create('div', className);
this.visible = false;
L.DomUtil.addClass(container, 'hidden');
L.DomEvent.disableClickPropagation(container);
this._addText(container, map);
L.DomEvent.addListener(container, 'click', function() {
window.prompt("Copy to clipboard: Ctrl+C, Enter", L.DomUtil.get(this._locationText).dataset.words);
}, this);
return container;
},
_addText: function(container, context)
{
this._locationText = L.DomUtil.create('span', 'leaflet-control-w3w-locationText' , container);
L.DomUtil.get(this._locationText).innerHTML = '<strong>w3w:</strong> ' + this.options.locationText;
return container;
},
_getWords: function(obj,locText){
var getJSON = function(url, successHandler, errorHandler) {
var xhr = typeof XMLHttpRequest != 'undefined' ? new XMLHttpRequest() : new ActiveXObject('Microsoft.XMLHTTP');
xhr.open('get', url, true);
xhr.responseType = 'json';
xhr.onreadystatechange = function() {
var status;
var data;
if (xhr.readyState == 4) {
status = xhr.status;
if (status == 200) {
successHandler && successHandler(xhr.response);
} else {
errorHandler && errorHandler(status);
}
}
};
xhr.send();
};
getJSON('https://api.what3words.com/v2/reverse?key='+this.options.apikey+'&coords='+obj.lat+','+obj.lng, function(data) {
console.log(data);
locText.innerHTML = '<strong>w3w:</strong> ' + data.words;
locText.dataset.words =("data-", data.words);
}, function(status) {
console.log('Something went wrong.');
});
},
setCoordinates: function(obj)
{
if (!this.visible) {
L.DomUtil.removeClass(this._container, 'hidden');
}
if (obj.latlng) {
this._getWords(obj.latlng,L.DomUtil.get(this._locationText));
}
}
});