Newer
Older
Sébastien DA ROCHA
committed
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
/* @preserve
* Leaflet Control Geocoder 1.13.0
* https://github.com/perliedman/leaflet-control-geocoder
*
* Copyright (c) 2012 sa3m (https://github.com/sa3m)
* Copyright (c) 2018 Per Liedman
* All rights reserved.
*/
this.L = this.L || {};
this.L.Control = this.L.Control || {};
this.L.Control.Geocoder = (function (L) {
'use strict';
L = L && Object.prototype.hasOwnProperty.call(L, 'default') ? L['default'] : L;
var lastCallbackId = 0;
// Adapted from handlebars.js
// https://github.com/wycats/handlebars.js/
var badChars = /[&<>"'`]/g;
var possible = /[&<>"'`]/;
var escape = {
'&': '&',
'<': '<',
'>': '>',
'"': '"',
"'": ''',
'`': '`'
};
function escapeChar(chr) {
return escape[chr];
}
function htmlEscape(string) {
if (string == null) {
return '';
} else if (!string) {
return string + '';
}
// Force a string conversion as this will be done by the append regardless and
// the regex test will do this transparently behind the scenes, causing issues if
// an object's to string has escaped characters in it.
string = '' + string;
if (!possible.test(string)) {
return string;
}
return string.replace(badChars, escapeChar);
}
function jsonp(url, params, callback, context, jsonpParam) {
var callbackId = '_l_geocoder_' + lastCallbackId++;
params[jsonpParam || 'callback'] = callbackId;
window[callbackId] = L.Util.bind(callback, context);
var script = document.createElement('script');
script.type = 'text/javascript';
script.src = url + getParamString(params);
script.id = callbackId;
document.getElementsByTagName('head')[0].appendChild(script);
}
function getJSON(url, params, callback) {
var xmlHttp = new XMLHttpRequest();

Timothee P
committed
xmlHttp.onreadystatechange = function () {
Sébastien DA ROCHA
committed
if (xmlHttp.readyState !== 4) {
return;
}
var message;
if (xmlHttp.status !== 200 && xmlHttp.status !== 304) {
message = '';
} else if (typeof xmlHttp.response === 'string') {
// IE doesn't parse JSON responses even with responseType: 'json'.
try {
message = JSON.parse(xmlHttp.response);
} catch (e) {
// Not a JSON response
message = xmlHttp.response;
}
} else {
message = xmlHttp.response;
}
callback(message);
};
xmlHttp.open('GET', url + getParamString(params), true);
xmlHttp.responseType = 'json';
xmlHttp.setRequestHeader('Accept', 'application/json');
xmlHttp.send(null);
}
function template(str, data) {

Timothee P
committed
return str.replace(/\{ *([\w_]+) *\}/g, function (str, key) {
Sébastien DA ROCHA
committed
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
var value = data[key];
if (value === undefined) {
value = '';
} else if (typeof value === 'function') {
value = value(data);
}
return htmlEscape(value);
});
}
function getParamString(obj, existingUrl, uppercase) {
var params = [];
for (var i in obj) {
var key = encodeURIComponent(uppercase ? i.toUpperCase() : i);
var value = obj[i];
if (!L.Util.isArray(value)) {
params.push(key + '=' + encodeURIComponent(value));
} else {
for (var j = 0; j < value.length; j++) {
params.push(key + '=' + encodeURIComponent(value[j]));
}
}
}
return (!existingUrl || existingUrl.indexOf('?') === -1 ? '?' : '&') + params.join('&');
}
var ArcGis = L.Class.extend({
options: {
service_url: 'https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer'
},

Timothee P
committed
initialize: function (accessToken, options) {
Sébastien DA ROCHA
committed
L.setOptions(this, options);
this._accessToken = accessToken;
},

Timothee P
committed
geocode: function (query, cb, context) {
Sébastien DA ROCHA
committed
var params = {
SingleLine: query,
outFields: 'Addr_Type',
forStorage: false,
maxLocations: 10,
f: 'json'
};
if (this._key && this._key.length) {
params.token = this._key;
}
getJSON(
this.options.service_url + '/findAddressCandidates',
L.extend(params, this.options.geocodingQueryParams),

Timothee P
committed
function (data) {
Sébastien DA ROCHA
committed
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
var results = [],
loc,
latLng,
latLngBounds;
if (data.candidates && data.candidates.length) {
for (var i = 0; i <= data.candidates.length - 1; i++) {
loc = data.candidates[i];
latLng = L.latLng(loc.location.y, loc.location.x);
latLngBounds = L.latLngBounds(
L.latLng(loc.extent.ymax, loc.extent.xmax),
L.latLng(loc.extent.ymin, loc.extent.xmin)
);
results[i] = {
name: loc.address,
bbox: latLngBounds,
center: latLng
};
}
}
cb.call(context, results);
}
);
},

Timothee P
committed
suggest: function (query, cb, context) {
Sébastien DA ROCHA
committed
return this.geocode(query, cb, context);
},

Timothee P
committed
reverse: function (location, scale, cb, context) {
Sébastien DA ROCHA
committed
var params = {
location: encodeURIComponent(location.lng) + ',' + encodeURIComponent(location.lat),
distance: 100,
f: 'json'
};

Timothee P
committed
getJSON(this.options.service_url + '/reverseGeocode', params, function (data) {
Sébastien DA ROCHA
committed
var result = [],
loc;
if (data && !data.error) {
loc = L.latLng(data.location.y, data.location.x);
result.push({
name: data.address.Match_addr,
center: loc,
bounds: L.latLngBounds(loc, loc)
});
}
cb.call(context, result);
});
}
Loading
Loading full blame...