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);
});
}
});
function arcgis(accessToken, options) {
return new ArcGis(accessToken, options);
}
var Bing = L.Class.extend({

Timothee P
committed
initialize: function (key) {
Sébastien DA ROCHA
committed
this.key = key;
},

Timothee P
committed
geocode: function (query, cb, context) {
Sébastien DA ROCHA
committed
jsonp(
'https://dev.virtualearth.net/REST/v1/Locations',
{
query: query,
key: this.key
},

Timothee P
committed
function (data) {
Sébastien DA ROCHA
committed
var results = [];
if (data.resourceSets.length > 0) {
for (var i = data.resourceSets[0].resources.length - 1; i >= 0; i--) {
var resource = data.resourceSets[0].resources[i],
bbox = resource.bbox;
results[i] = {
name: resource.name,
bbox: L.latLngBounds([bbox[0], bbox[1]], [bbox[2], bbox[3]]),
center: L.latLng(resource.point.coordinates)
};
}
}
cb.call(context, results);
},
this,
'jsonp'
);
},

Timothee P
committed
reverse: function (location, scale, cb, context) {
Sébastien DA ROCHA
committed
jsonp(
'//dev.virtualearth.net/REST/v1/Locations/' + location.lat + ',' + location.lng,
{
key: this.key
},

Timothee P
committed
function (data) {
Sébastien DA ROCHA
committed
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
var results = [];
for (var i = data.resourceSets[0].resources.length - 1; i >= 0; i--) {
var resource = data.resourceSets[0].resources[i],
bbox = resource.bbox;
results[i] = {
name: resource.name,
bbox: L.latLngBounds([bbox[0], bbox[1]], [bbox[2], bbox[3]]),
center: L.latLng(resource.point.coordinates)
};
}
cb.call(context, results);
},
this,
'jsonp'
);
}
});
function bing(key) {
return new Bing(key);
}
var Google = L.Class.extend({
options: {
serviceUrl: 'https://maps.googleapis.com/maps/api/geocode/json',
geocodingQueryParams: {},
reverseQueryParams: {}
},

Timothee P
committed
initialize: function (key, options) {
Sébastien DA ROCHA
committed
this._key = key;
L.setOptions(this, options);
// Backwards compatibility
this.options.serviceUrl = this.options.service_url || this.options.serviceUrl;
},

Timothee P
committed
geocode: function (query, cb, context) {
Sébastien DA ROCHA
committed
var params = {
address: query
};
if (this._key && this._key.length) {
params.key = this._key;
}
params = L.Util.extend(params, this.options.geocodingQueryParams);

Timothee P
committed
getJSON(this.options.serviceUrl, params, function (data) {
Sébastien DA ROCHA
committed
var results = [],
loc,
latLng,
latLngBounds;
if (data.results && data.results.length) {
for (var i = 0; i <= data.results.length - 1; i++) {
loc = data.results[i];
latLng = L.latLng(loc.geometry.location);
latLngBounds = L.latLngBounds(
L.latLng(loc.geometry.viewport.northeast),
L.latLng(loc.geometry.viewport.southwest)
);
results[i] = {
name: loc.formatted_address,
bbox: latLngBounds,
center: latLng,
properties: loc.address_components
};
}
}
cb.call(context, results);
});
},

Timothee P
committed
reverse: function (location, scale, cb, context) {
Sébastien DA ROCHA
committed
var params = {
latlng: encodeURIComponent(location.lat) + ',' + encodeURIComponent(location.lng)
};
params = L.Util.extend(params, this.options.reverseQueryParams);
if (this._key && this._key.length) {
params.key = this._key;
}

Timothee P
committed
getJSON(this.options.serviceUrl, params, function (data) {
Sébastien DA ROCHA
committed
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
var results = [],
loc,
latLng,
latLngBounds;
if (data.results && data.results.length) {
for (var i = 0; i <= data.results.length - 1; i++) {
loc = data.results[i];
latLng = L.latLng(loc.geometry.location);
latLngBounds = L.latLngBounds(
L.latLng(loc.geometry.viewport.northeast),
L.latLng(loc.geometry.viewport.southwest)
);
results[i] = {
name: loc.formatted_address,
bbox: latLngBounds,
center: latLng,
properties: loc.address_components
};
}
}
cb.call(context, results);
});
}
});
function google(key, options) {
return new Google(key, options);
}
var HERE = L.Class.extend({
options: {
geocodeUrl: 'https://geocoder.api.here.com/6.2/geocode.json',
reverseGeocodeUrl: 'https://reverse.geocoder.api.here.com/6.2/reversegeocode.json',
app_id: '<insert your app_id here>',
app_code: '<insert your app_code here>',
geocodingQueryParams: {},
reverseQueryParams: {},
reverseGeocodeProxRadius: null
},

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

Timothee P
committed
geocode: function (query, cb, context) {
Sébastien DA ROCHA
committed
var params = {
searchtext: query,
gen: 9,
app_id: this.options.app_id,
app_code: this.options.app_code,
jsonattributes: 1
};
params = L.Util.extend(params, this.options.geocodingQueryParams);
this.getJSON(this.options.geocodeUrl, params, cb, context);
},

Timothee P
committed
reverse: function (location, scale, cb, context) {
Sébastien DA ROCHA
committed
var _proxRadius = this.options.reverseGeocodeProxRadius
? this.options.reverseGeocodeProxRadius
: null;
var proxRadius = _proxRadius ? ',' + encodeURIComponent(_proxRadius) : '';
var params = {
prox: encodeURIComponent(location.lat) + ',' + encodeURIComponent(location.lng) + proxRadius,
mode: 'retrieveAddresses',
app_id: this.options.app_id,
app_code: this.options.app_code,
gen: 9,
jsonattributes: 1
};
params = L.Util.extend(params, this.options.reverseQueryParams);
this.getJSON(this.options.reverseGeocodeUrl, params, cb, context);
},

Timothee P
committed
getJSON: function (url, params, cb, context) {
getJSON(url, params, function (data) {
Sébastien DA ROCHA
committed
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
var results = [],
loc,
latLng,
latLngBounds;
if (data.response.view && data.response.view.length) {
for (var i = 0; i <= data.response.view[0].result.length - 1; i++) {
loc = data.response.view[0].result[i].location;
latLng = L.latLng(loc.displayPosition.latitude, loc.displayPosition.longitude);
latLngBounds = L.latLngBounds(
L.latLng(loc.mapView.topLeft.latitude, loc.mapView.topLeft.longitude),
L.latLng(loc.mapView.bottomRight.latitude, loc.mapView.bottomRight.longitude)
);
results[i] = {
name: loc.address.label,
properties: loc.address,
bbox: latLngBounds,
center: latLng
};
}
}
cb.call(context, results);
});
}
});
function here(options) {
return new HERE(options);
}
var LatLng = L.Class.extend({
options: {
// the next geocoder to use
next: undefined,
sizeInMeters: 10000
},

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

Timothee P
committed
geocode: function (query, cb, context) {
Sébastien DA ROCHA
committed
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
var match;
var center;
// regex from https://github.com/openstreetmap/openstreetmap-website/blob/master/app/controllers/geocoder_controller.rb
if ((match = query.match(/^([NS])\s*(\d{1,3}(?:\.\d*)?)\W*([EW])\s*(\d{1,3}(?:\.\d*)?)$/))) {
// [NSEW] decimal degrees
center = L.latLng(
(/N/i.test(match[1]) ? 1 : -1) * parseFloat(match[2]),
(/E/i.test(match[3]) ? 1 : -1) * parseFloat(match[4])
);
} else if (
(match = query.match(/^(\d{1,3}(?:\.\d*)?)\s*([NS])\W*(\d{1,3}(?:\.\d*)?)\s*([EW])$/))
) {
// decimal degrees [NSEW]
center = L.latLng(
(/N/i.test(match[2]) ? 1 : -1) * parseFloat(match[1]),
(/E/i.test(match[4]) ? 1 : -1) * parseFloat(match[3])
);
} else if (
(match = query.match(
/^([NS])\s*(\d{1,3})°?\s*(\d{1,3}(?:\.\d*)?)?['′]?\W*([EW])\s*(\d{1,3})°?\s*(\d{1,3}(?:\.\d*)?)?['′]?$/
))
) {
// [NSEW] degrees, decimal minutes
center = L.latLng(
(/N/i.test(match[1]) ? 1 : -1) * (parseFloat(match[2]) + parseFloat(match[3] / 60)),
(/E/i.test(match[4]) ? 1 : -1) * (parseFloat(match[5]) + parseFloat(match[6] / 60))
);
} else if (
(match = query.match(
/^(\d{1,3})°?\s*(\d{1,3}(?:\.\d*)?)?['′]?\s*([NS])\W*(\d{1,3})°?\s*(\d{1,3}(?:\.\d*)?)?['′]?\s*([EW])$/
))
) {
// degrees, decimal minutes [NSEW]
center = L.latLng(
(/N/i.test(match[3]) ? 1 : -1) * (parseFloat(match[1]) + parseFloat(match[2] / 60)),
(/E/i.test(match[6]) ? 1 : -1) * (parseFloat(match[4]) + parseFloat(match[5] / 60))
);
} else if (
(match = query.match(
/^([NS])\s*(\d{1,3})°?\s*(\d{1,2})['′]?\s*(\d{1,3}(?:\.\d*)?)?["″]?\W*([EW])\s*(\d{1,3})°?\s*(\d{1,2})['′]?\s*(\d{1,3}(?:\.\d*)?)?["″]?$/
))
) {
// [NSEW] degrees, minutes, decimal seconds
center = L.latLng(
(/N/i.test(match[1]) ? 1 : -1) *

Timothee P
committed
(parseFloat(match[2]) + parseFloat(match[3] / 60 + parseFloat(match[4] / 3600))),
Sébastien DA ROCHA
committed
(/E/i.test(match[5]) ? 1 : -1) *

Timothee P
committed
(parseFloat(match[6]) + parseFloat(match[7] / 60) + parseFloat(match[8] / 3600))
Sébastien DA ROCHA
committed
);
} else if (
(match = query.match(
/^(\d{1,3})°?\s*(\d{1,2})['′]?\s*(\d{1,3}(?:\.\d*)?)?["″]\s*([NS])\W*(\d{1,3})°?\s*(\d{1,2})['′]?\s*(\d{1,3}(?:\.\d*)?)?["″]?\s*([EW])$/
))
) {
// degrees, minutes, decimal seconds [NSEW]
center = L.latLng(
(/N/i.test(match[4]) ? 1 : -1) *

Timothee P
committed
(parseFloat(match[1]) + parseFloat(match[2] / 60 + parseFloat(match[3] / 3600))),
Sébastien DA ROCHA
committed
(/E/i.test(match[8]) ? 1 : -1) *

Timothee P
committed
(parseFloat(match[5]) + parseFloat(match[6] / 60) + parseFloat(match[7] / 3600))
Sébastien DA ROCHA
committed
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
);
} else if (
(match = query.match(/^\s*([+-]?\d+(?:\.\d*)?)\s*[\s,]\s*([+-]?\d+(?:\.\d*)?)\s*$/))
) {
center = L.latLng(parseFloat(match[1]), parseFloat(match[2]));
}
if (center) {
var results = [
{
name: query,
center: center,
bbox: center.toBounds(this.options.sizeInMeters)
}
];
cb.call(context, results);
} else if (this.options.next) {
this.options.next.geocode(query, cb, context);
}
}
});
function latLng(options) {
return new LatLng(options);
}
var Mapbox = L.Class.extend({
options: {
serviceUrl: 'https://api.mapbox.com/geocoding/v5/mapbox.places/',
geocodingQueryParams: {},
reverseQueryParams: {}
},

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

Timothee P
committed
geocode: function (query, cb, context) {
Sébastien DA ROCHA
committed
var params = this.options.geocodingQueryParams;
if (
params.proximity !== undefined &&
params.proximity.lat !== undefined &&
params.proximity.lng !== undefined
) {
params.proximity = params.proximity.lng + ',' + params.proximity.lat;
}

Timothee P
committed
getJSON(this.options.serviceUrl + encodeURIComponent(query) + '.json', params, function (data) {
Sébastien DA ROCHA
committed
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
var results = [],
loc,
latLng,
latLngBounds;
if (data.features && data.features.length) {
for (var i = 0; i <= data.features.length - 1; i++) {
loc = data.features[i];
latLng = L.latLng(loc.center.reverse());
if (loc.bbox) {
latLngBounds = L.latLngBounds(
L.latLng(loc.bbox.slice(0, 2).reverse()),
L.latLng(loc.bbox.slice(2, 4).reverse())
);
} else {
latLngBounds = L.latLngBounds(latLng, latLng);
}
var properties = {
text: loc.text,
address: loc.address
};
for (var j = 0; j < (loc.context || []).length; j++) {
var id = loc.context[j].id.split('.')[0];
properties[id] = loc.context[j].text;
// Get country code when available
if (loc.context[j].short_code) {
properties['countryShortCode'] = loc.context[j].short_code;
}
}
results[i] = {
name: loc.place_name,
bbox: latLngBounds,
center: latLng,
properties: properties
};
}
}
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
getJSON(
this.options.serviceUrl +

Timothee P
committed
encodeURIComponent(location.lng) +
',' +
encodeURIComponent(location.lat) +
'.json',
Sébastien DA ROCHA
committed
this.options.reverseQueryParams,

Timothee P
committed
function (data) {
Sébastien DA ROCHA
committed
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
var results = [],
loc,
latLng,
latLngBounds;
if (data.features && data.features.length) {
for (var i = 0; i <= data.features.length - 1; i++) {
loc = data.features[i];
latLng = L.latLng(loc.center.reverse());
if (loc.bbox) {
latLngBounds = L.latLngBounds(
L.latLng(loc.bbox.slice(0, 2).reverse()),
L.latLng(loc.bbox.slice(2, 4).reverse())
);
} else {
latLngBounds = L.latLngBounds(latLng, latLng);
}
results[i] = {
name: loc.place_name,
bbox: latLngBounds,
center: latLng
};
}
}
cb.call(context, results);
}
);
}
});
function mapbox(accessToken, options) {
return new Mapbox(accessToken, options);
}
var MapQuest = L.Class.extend({
options: {
serviceUrl: 'https://www.mapquestapi.com/geocoding/v1'
},

Timothee P
committed
initialize: function (key, options) {
Sébastien DA ROCHA
committed
// MapQuest seems to provide URI encoded API keys,
// so to avoid encoding them twice, we decode them here
this._key = decodeURIComponent(key);
L.Util.setOptions(this, options);
},

Timothee P
committed
_formatName: function () {
Sébastien DA ROCHA
committed
var r = [],
i;
for (i = 0; i < arguments.length; i++) {
if (arguments[i]) {
r.push(arguments[i]);
}
}
return r.join(', ');
},

Timothee P
committed
geocode: function (query, cb, context) {
Sébastien DA ROCHA
committed
getJSON(
this.options.serviceUrl + '/address',
{
key: this._key,
location: query,
limit: 5,
outFormat: 'json'
},

Timothee P
committed
L.bind(function (data) {
Sébastien DA ROCHA
committed
var results = [],
loc,
latLng;
if (data.results && data.results[0].locations) {
for (var i = data.results[0].locations.length - 1; i >= 0; i--) {
loc = data.results[0].locations[i];
latLng = L.latLng(loc.latLng);
results[i] = {
name: this._formatName(loc.street, loc.adminArea4, loc.adminArea3, loc.adminArea1),
bbox: L.latLngBounds(latLng, latLng),
center: latLng
};
}
}
cb.call(context, results);
}, this)
);
},

Timothee P
committed
reverse: function (location, scale, cb, context) {
Sébastien DA ROCHA
committed
getJSON(
this.options.serviceUrl + '/reverse',
{
key: this._key,
location: location.lat + ',' + location.lng,
outputFormat: 'json'
},

Timothee P
committed
L.bind(function (data) {
Sébastien DA ROCHA
committed
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
var results = [],
loc,
latLng;
if (data.results && data.results[0].locations) {
for (var i = data.results[0].locations.length - 1; i >= 0; i--) {
loc = data.results[0].locations[i];
latLng = L.latLng(loc.latLng);
results[i] = {
name: this._formatName(loc.street, loc.adminArea4, loc.adminArea3, loc.adminArea1),
bbox: L.latLngBounds(latLng, latLng),
center: latLng
};
}
}
cb.call(context, results);
}, this)
);
}
});
function mapQuest(key, options) {
return new MapQuest(key, options);
}
var Neutrino = L.Class.extend({
options: {
userId: '<insert your userId here>',
apiKey: '<insert your apiKey here>',
serviceUrl: 'https://neutrinoapi.com/'
},

Timothee P
committed
initialize: function (options) {
Sébastien DA ROCHA
committed
L.Util.setOptions(this, options);
},
// https://www.neutrinoapi.com/api/geocode-address/

Timothee P
committed
geocode: function (query, cb, context) {
Sébastien DA ROCHA
committed
getJSON(
this.options.serviceUrl + 'geocode-address',
{
apiKey: this.options.apiKey,
userId: this.options.userId,
//get three words and make a dot based string
address: query.split(/\s+/).join('.')
},

Timothee P
committed
function (data) {
Sébastien DA ROCHA
committed
var results = [],
latLng,
latLngBounds;
if (data.locations) {
data.geometry = data.locations[0];
latLng = L.latLng(data.geometry['latitude'], data.geometry['longitude']);
latLngBounds = L.latLngBounds(latLng, latLng);
results[0] = {
name: data.geometry.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);
},
// https://www.neutrinoapi.com/api/geocode-reverse/

Timothee P
committed
reverse: function (location, scale, cb, context) {
Sébastien DA ROCHA
committed
getJSON(
this.options.serviceUrl + 'geocode-reverse',
{
apiKey: this.options.apiKey,
userId: this.options.userId,
latitude: location.lat,
longitude: location.lng
},

Timothee P
committed
function (data) {
Sébastien DA ROCHA
committed
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
var results = [],
latLng,
latLngBounds;
if (data.status.status == 200 && data.found) {
latLng = L.latLng(location.lat, location.lng);
latLngBounds = L.latLngBounds(latLng, latLng);
results[0] = {
name: data.address,
bbox: latLngBounds,
center: latLng
};
}
cb.call(context, results);
}
);
}
});
function neutrino(accessToken) {
return new Neutrino(accessToken);
}
var Nominatim = L.Class.extend({
options: {
serviceUrl: 'https://nominatim.openstreetmap.org/',
geocodingQueryParams: {},
reverseQueryParams: {},

Timothee P
committed
htmlTemplate: function (r) {
Sébastien DA ROCHA
committed
var a = r.address,
className,
parts = [];
if (a.road || a.building) {
parts.push('{building} {road} {house_number}');
}
if (a.city || a.town || a.village || a.hamlet) {
className = parts.length > 0 ? 'leaflet-control-geocoder-address-detail' : '';
parts.push(
'<span class="' + className + '">{postcode} {city} {town} {village} {hamlet}</span>'
);
}
if (a.state || a.country) {
className = parts.length > 0 ? 'leaflet-control-geocoder-address-context' : '';
parts.push('<span class="' + className + '">{state} {country}</span>');
}
return template(parts.join('<br/>'), a);
}
},

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

Timothee P
committed
geocode: function (query, cb, context) {
Sébastien DA ROCHA
committed
getJSON(
this.options.serviceUrl + 'search',
L.extend(
{
q: query,
limit: 5,
format: 'json',
addressdetails: 1
},
this.options.geocodingQueryParams
),

Timothee P
committed
L.bind(function (data) {
Sébastien DA ROCHA
committed
var results = [];
for (var i = data.length - 1; i >= 0; i--) {
var bbox = data[i].boundingbox;
for (var j = 0; j < 4; j++) bbox[j] = parseFloat(bbox[j]);
results[i] = {
icon: data[i].icon,
name: data[i].display_name,
html: this.options.htmlTemplate ? this.options.htmlTemplate(data[i]) : undefined,
bbox: L.latLngBounds([bbox[0], bbox[2]], [bbox[1], bbox[3]]),
center: L.latLng(data[i].lat, data[i].lon),
properties: data[i]
};
}
cb.call(context, results);
}, this)
);
},
suggest: function (query, cb, context) {
return this.geocode(query, cb, context);

Timothee P
committed
},
Sébastien DA ROCHA
committed

Timothee P
committed
reverse: function (location, scale, cb, context) {
Sébastien DA ROCHA
committed
getJSON(
this.options.serviceUrl + 'reverse',
L.extend(
{
lat: location.lat,
lon: location.lng,
zoom: Math.round(Math.log(scale / 256) / Math.log(2)),
addressdetails: 1,
format: 'json'
},
this.options.reverseQueryParams
),

Timothee P
committed
L.bind(function (data) {
Sébastien DA ROCHA
committed
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
var result = [],
loc;
if (data && data.lat && data.lon) {
loc = L.latLng(data.lat, data.lon);
result.push({
name: data.display_name,
html: this.options.htmlTemplate ? this.options.htmlTemplate(data) : undefined,
center: loc,
bounds: L.latLngBounds(loc, loc),
properties: data
});
}
cb.call(context, result);
}, this)
);
}
});
function nominatim(options) {
return new Nominatim(options);
}
var OpenLocationCode = L.Class.extend({
options: {
OpenLocationCode: undefined,
codeLength: undefined
},

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

Timothee P
committed
geocode: function (query, cb, context) {
Sébastien DA ROCHA
committed
try {
var decoded = this.options.OpenLocationCode.decode(query);
var result = {
name: query,
center: L.latLng(decoded.latitudeCenter, decoded.longitudeCenter),
bbox: L.latLngBounds(
L.latLng(decoded.latitudeLo, decoded.longitudeLo),
L.latLng(decoded.latitudeHi, decoded.longitudeHi)
)
};
cb.call(context, [result]);
} catch (e) {
console.warn(e); // eslint-disable-line no-console
cb.call(context, []);
}
},

Timothee P
committed
reverse: function (location, scale, cb, context) {
Sébastien DA ROCHA
committed
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
try {
var code = this.options.OpenLocationCode.encode(
location.lat,
location.lng,
this.options.codeLength
);
var result = {
name: code,
center: L.latLng(location.lat, location.lng),
bbox: L.latLngBounds(
L.latLng(location.lat, location.lng),
L.latLng(location.lat, location.lng)
)
};
cb.call(context, [result]);
} catch (e) {
console.warn(e); // eslint-disable-line no-console
cb.call(context, []);
}
}
});
function openLocationCode(options) {
return new OpenLocationCode(options);
}
var OpenCage = L.Class.extend({
options: {
serviceUrl: 'https://api.opencagedata.com/geocode/v1/json',
geocodingQueryParams: {},
reverseQueryParams: {}
},

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

Timothee P
committed
geocode: function (query, cb, context) {
Sébastien DA ROCHA
committed
var params = {
key: this._accessToken,
q: query
};
params = L.extend(params, this.options.geocodingQueryParams);

Timothee P
committed
getJSON(this.options.serviceUrl, params, function (data) {
Sébastien DA ROCHA
committed
var results = [],
latLng,
latLngBounds,
loc;
if (data.results && data.results.length) {
for (var i = 0; i < data.results.length; i++) {
loc = data.results[i];
latLng = L.latLng(loc.geometry);
if (loc.annotations && loc.annotations.bounds) {
latLngBounds = L.latLngBounds(
L.latLng(loc.annotations.bounds.northeast),
L.latLng(loc.annotations.bounds.southwest)
);
} else {