-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgeocoder.js
74 lines (66 loc) · 2.23 KB
/
geocoder.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
(function () {
"use strict";
var rp = require("request-promise"),
_ = require("lodash"),
q = require("q");
require("stringformat").extendString("format");
var geocoder = (function (baseUrl) {
var cookieJar = rp.jar(),
request = rp.defaults({
proxy: process.env.CRHME_PROXY || null,
jar: cookieJar,
rejectUnauthorized: false,
followRedirect: true,
followAllRedirects: true,
headers: {
"accept": "application/json, text/javascript, */*",
"user-agent": "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; Trident/6.0)",
"dnt": "1",
"accept-language": "en-US,en;q=0.8",
"x-requested-with": "XMLHttpRequest",
"content-type": "application/x-www-form-urlencoded",
"connection": "keep-alive",
"origin": baseUrl,
"referer": baseUrl
}
});
return {
getCoordinates: function (address) {
if (typeof address === "string") {
address = address.replace(/[^\w\d\s\.\#\,]/g, "").replace(/\s+/g, " ");
}
else {
throw(new Error("Invalid address argument - must be a string"));
}
var deferred = q.defer();
try {
request({
method: "GET",
uri: "{0}?sensor=false&address={1}".format(baseUrl, encodeURIComponent(address))
}).then(function (response) {
try {
var json = JSON.parse(response);
if (json && json.results && json.results.length && json.results[0].geometry && json.results[0].geometry.location) {
deferred.resolve(json.results[0].geometry.location);
}
else {
deferred.reject("Unexpected JSON response format: results[0].geometry.location not found");
}
}
catch (ex) {
deferred.reject("Cannot parse response as JSON");
}
}, deferred.reject)
.catch(function (ex) {
deferred.reject(ex.message);
});
}
catch (ex) {
deferred.reject(ex.message);
}
return deferred.promise;
}
};
})("https://maps.google.com/maps/api/geocode/json");
module.exports = geocoder;
})();