This repository was archived by the owner on Apr 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 377
Expand file tree
/
Copy pathgeocoder.js
More file actions
154 lines (127 loc) · 4.38 KB
/
Copy pathgeocoder.js
File metadata and controls
154 lines (127 loc) · 4.38 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
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
'use strict';
var util = require('./util'),
format_url = require('./format_url'),
feedback = require('./feedback'),
request = require('./request');
// Low-level geocoding interface - wraps specific API calls and their
// return values.
module.exports = function(url, options) {
if (!options) options = {};
var geocoder = {};
util.strict(url, 'string');
if (url.indexOf('/') === -1) {
url = format_url('/geocoding/v5/' + url + '/{query}.json', options.accessToken, 5);
}
function roundTo(latLng, precision) {
var mult = Math.pow(10, precision);
latLng.lat = Math.round(latLng.lat * mult) / mult;
latLng.lng = Math.round(latLng.lng * mult) / mult;
return latLng;
}
geocoder.getURL = function() {
return url;
};
geocoder.queryURL = function(_) {
var isArray = L.Util.isArray;
var isObject = !(isArray(_) || typeof _ === 'string'),
query = isObject ? _.query : _;
if (isArray(query)) {
var parts = [];
for (var i = 0; i < query.length; i++) {
parts[i] = encodeURIComponent(query[i]);
}
query = parts.join(';');
} else {
query = encodeURIComponent(query);
}
feedback.record({ geocoding: query });
var url = L.Util.template(geocoder.getURL(), {query: query});
if (isObject) {
if (_.types) {
if (isArray(_.types)) {
url += '&types=' + _.types.join();
} else {
url += '&types=' + _.types;
}
}
if (_.country) {
if (isArray(_.country)) {
url += '&country=' + _.country.join();
} else {
url += '&country=' + _.country;
}
}
if (_.limit) {
url += '&limit=' + _.limit;
}
if (_.bbox) {
if (isArray(_.bbox)) {
url += '&bbox=' + _.bbox.join();
} else {
url += '&bbox=' + _.bbox;
}
}
if (_.proximity) {
var proximity = roundTo(L.latLng(_.proximity), 3);
url += '&proximity=' + proximity.lng + ',' + proximity.lat;
}
if (typeof _.autocomplete === 'boolean') {
url += '&autocomplete=' + _.autocomplete;
}
}
return url;
};
geocoder.query = function(_, callback) {
util.strict(callback, 'function');
request(geocoder.queryURL(_), function(err, json) {
if (json && (json.length || json.features)) {
var res = {
results: json
};
if (json.features && json.features.length) {
res.latlng = [
json.features[0].center[1],
json.features[0].center[0]];
if (json.features[0].bbox) {
res.bounds = json.features[0].bbox;
res.lbounds = util.lbounds(res.bounds);
}
}
callback(null, res);
} else callback(err || true);
});
return geocoder;
};
// a reverse geocode:
//
// geocoder.reverseQuery([80, 20])
geocoder.reverseQuery = function(_, callback) {
var q = '';
// sort through different ways people represent lat and lon pairs
function normalize(x) {
var latLng;
if (x.lat !== undefined && x.lng !== undefined) {
latLng = L.latLng(x.lat, x.lng);
} else if (x.lat !== undefined && x.lon !== undefined) {
latLng = L.latLng(x.lat, x.lon);
} else {
latLng = L.latLng(x[1], x[0]);
}
latLng = roundTo(latLng, 5);
return latLng.lng + ',' + latLng.lat;
}
if (_.length && _[0].length) {
for (var i = 0, pts = []; i < _.length; i++) {
pts.push(normalize(_[i]));
}
q = pts.join(';');
} else {
q = normalize(_);
}
request(geocoder.queryURL(q), function(err, json) {
callback(err, json);
});
return geocoder;
};
return geocoder;
};