-
Notifications
You must be signed in to change notification settings - Fork 918
/
Copy pathPluginGeocoder.js
187 lines (173 loc) · 6.16 KB
/
PluginGeocoder.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
// @ts-ignore
var BaseArrayClass = require('cordova-plugin-googlemaps.BaseArrayClass');
var geocoder = null;
var lastRequestTime = 0;
var QUEUE = new BaseArrayClass();
QUEUE.on('insert_at', function() {
if (QUEUE.getLength() === 1) {
this.trigger('next');
}
});
QUEUE.one('insert_at', function() {
geocoder = new google.maps.Geocoder();
this.trigger('next');
});
QUEUE.on('next', function() {
var self = QUEUE;
if (!geocoder || self._executing || QUEUE.getLength() === 0) {
return;
}
if (Date.now() - lastRequestTime < 300) {
setTimeout(function() {
self.trigger('next');
}, 300 + Math.floor(Math.random() * 200));
return;
}
lastRequestTime = Date.now();
self._executing = true;
var cmd = QUEUE.removeAt(0, true);
geocoder.geocode(cmd.geocoderRequest, function(results, status) {
switch(status) {
case google.maps.GeocoderStatus.ERROR:
cmd.onError('[geocoding] Cannot connect to Google servers');
return;
case google.maps.GeocoderStatus.INVALID_REQUEST:
cmd.onError('[geocoding] Invalid request for geocoder');
return;
case google.maps.GeocoderStatus.OVER_QUERY_LIMIT:
QUEUE.insertAt(0, cmd);
console.warn('[geocoding] Due to the OVER_QUERY_LIMIT error, wait 3 sec, then try again.');
setTimeout(function() {
self._executing = false;
self.trigger('next');
}, 3000 + Math.floor(Math.random() * 200));
return;
case google.maps.GeocoderStatus.REQUEST_DENIED:
cmd.onError('[geocoding] Google denited your geocoding request.');
return;
case google.maps.GeocoderStatus.UNKNOWN_ERROR:
cmd.onError('[geocoding] There was an unknown error. Please try again.');
return;
}
var pluginResults = results.map(function(geocoderResult) {
var result = {
'position': {
'lat': geocoderResult.geometry.location.lat(),
'lng': geocoderResult.geometry.location.lng()
},
extra: {
lines: []
}
};
if (geocoderResult.place_id) {
result.extra.place_id = geocoderResult.place_id;
}
if (geocoderResult.plus_code) {
result.extra.plus_code = geocoderResult.plus_code;
}
if (geocoderResult.types) {
result.extra.types = geocoderResult.types;
}
var administrative_area = [];
var sublocality_area = [];
geocoderResult.address_components.forEach(function(addrComp) {
result.extra.lines.push(addrComp.long_name);
if (!result.locality && addrComp.types.indexOf('locality') > -1) {
result.locality = addrComp.short_name;
}
if (addrComp.types.indexOf('administrative_area_level_1') > -1 ||
addrComp.types.indexOf('administrative_area_level_2') > -1 ||
addrComp.types.indexOf('administrative_area_level_3') > -1 ||
addrComp.types.indexOf('administrative_area_level_4') > -1) {
addrComp.types.forEach(function(type) {
if (type.indexOf('administrative_area_level_') === 0) {
var idx = parseInt(type.replace('administrative_area_level_', ''), 10);
administrative_area[idx - 1] = addrComp.long_name;
}
});
}
if (addrComp.types.indexOf('sublocality_level_1') > -1 ||
addrComp.types.indexOf('sublocality_level_2') > -1 ||
addrComp.types.indexOf('sublocality_level_3') > -1 ||
addrComp.types.indexOf('sublocality_level_4') > -1 ||
addrComp.types.indexOf('sublocality_level_5') > -1) {
addrComp.types.forEach(function(type) {
if (type.indexOf('sublocality_level_') === 0) {
var idx = parseInt(type.replace('sublocality_level_', ''), 10);
sublocality_area[idx - 1] = addrComp.long_name;
}
});
}
if (!result.country && addrComp.types.indexOf('country') > -1) {
result.country = addrComp.long_name;
result.countryCode = addrComp.short_name;
}
if (!result.postalCode && addrComp.types.indexOf('postal_code') > -1) {
result.postalCode = addrComp.long_name;
}
if (!result.postalCode && addrComp.types.indexOf('postal_code') > -1) {
result.postalCode = addrComp.long_name;
}
if (!result.thoroughfare && addrComp.types.indexOf('street_address') > -1) {
result.thoroughfare = addrComp.long_name;
}
});
if (administrative_area.length > 0) {
result.adminArea = administrative_area.shift();
result.subAdminArea = administrative_area.join(',');
}
if (sublocality_area.length > 0) {
result.subLocality = sublocality_area.join(',');
}
//result.extra = geocoderResult.address_components; // for debug
return result;
});
self._executing = false;
cmd.onSuccess({
'idx': cmd.pluginRequest.__pgmIdx,
'results': pluginResults
});
// Insert delay to prevent the OVER_QUERY_LIMIT error.
var delay = 300 + Math.floor(Math.random() * 200);
setTimeout(function() {
self._executing = false;
self.trigger('next');
}, delay);
});
});
module.exports = {
/**
*
* @param {(result: GeocoderResult[]) => void} onSuccess
* @param {(err: string) => void} onError
* @param {[GeocoderRequest]} args
*/
'geocode': function(onSuccess, onError, args) {
var request = args[0];
var geocoderRequest = {};
if (!request.position && request.address) {
//---------------------
// Geocoding
//---------------------
if (request.bounds && Array.isArray(request.bounds)) {
var bounds = new google.maps.LatLngBounds();
request.bounds.forEach(function(position) {
bounds.extend(position);
});
geocoderRequest.bounds = bounds;
}
geocoderRequest.address = request.address;
}
if (request.position && !request.address) {
geocoderRequest.location = request.position;
}
QUEUE.push({
'geocoderRequest': geocoderRequest,
'pluginRequest': request,
'onSuccess': onSuccess,
'onError': onError
});
}
};
// @ts-ignore
require('cordova/exec/proxy').add('PluginGeocoder', module.exports);