-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathbigdatacloud_reverse_geocode.js
127 lines (125 loc) · 3.39 KB
/
bigdatacloud_reverse_geocode.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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
;(function(_w,$) {
var BDCReverseGeocode=function(localityLanguage,endpoint,server) {
this.endpoint=endpoint ? endpoint : 'reverse-geocode-client';
this.server=server ? server : 'api.bigdatacloud.net';
this.localityLanguage=localityLanguage ? localityLanguage : 'en';
};
BDCReverseGeocode.prototype={
setApi:function(api) {
this.api=api;
return this;
},
getApi:function() {
return this.api;
},
getClientCoordinates:function(cb) {
if (!cb) return false;
if (!navigator.geolocation || !navigator.geolocation.getCurrentPosition) return cb(false);
return navigator.geolocation.getCurrentPosition(
(function(position) { return this.cb(position);}).bind({cb:cb}),
(function(err) { console.error(err); return this.cb(false);}).bind({cb:cb}),
{
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
}
);
},
getClientLocation:function(latLng,cb) {
var _this=this;
if (typeof latLng=='function' && !cb) {
cb=latLng;
latLng=null;
} else if (latLng=='function') {
latLng=latLng();
}
if (!cb) return false;
if (!latLng && latLng!=-1) {
return this.getClientCoordinates(function(position) {
_this.getClientLocation(position ? position : -1,cb);
})
} else {
this.callApi(this.processLatLng(latLng),function(result) {
cb(result);
},function(err) {
console.error(err);
cb(false);
});
}
},
processLatLng:function(latLng) {
var result={};
if (!latLng || latLng==-1) return {};
if (latLng.coords) {
latLng=latLng.coords;
}
if (!typeof latLng.latitude) {
if (latLng.lat) {
latLng.latitude=latLng.lat;
}
}
if (!typeof latLng.longitude) {
if (latLng.long) {
latLng.longitude=latLng.long;
}
if (latLng.lng) {
latLng.longitude=latLng.lng;
}
}
if (typeof latLng.latitude!= 'undefined') {
result.latitude=parseFloat(parseFloat(latLng.latitude).toFixed(5));
}
if (typeof latLng.longitude!= 'undefined') {
result.longitude=parseFloat(parseFloat(latLng.longitude).toFixed(5));
}
return result;
},
callApi:function(payload,cb) {
var xhr = new XMLHttpRequest()
xhr.open(
'GET',
'https://'+this.server+'/data/'+this.endpoint+'?'+this.prepareData(payload),
true
);
xhr.onreadystatechange = function() {
if (this.readyState === XMLHttpRequest.DONE) {
if (this.status === 200) {
try {
cb(JSON.parse(this.responseText))
} catch (e) {
cb(false)
}
} else {
try {
var result=JSON.parse(this.responseText);
console.error(result,this.status);
cb(false);
} catch (e) {
console.error(this.responseText,this.status);
cb(false);
}
}
}
}
xhr.send();
},
prepareData:function(payload) {
var data=[];
var hasLocalityLanguage=false;
if (payload) {
for (var i in payload) {
switch(i) {
case 'localityLanguage':
hasLocalityLanguage=true;
break;
}
data.push(encodeURIComponent(i)+'='+encodeURIComponent(payload[i]));
}
}
if (!hasLocalityLanguage) data.push('localityLanguage='+this.localityLanguage);
data=data.join('&');
return data;
}
}
_w.BDCReverseGeocode=BDCReverseGeocode;
})(window,typeof jQuery=='undefined' ? null : jQuery);