Skip to content

Commit 1c7025d

Browse files
kuzvacperliedman
authored andcommitted
Add ArcGis geocoding service (#120)
* Add ArcGis geocoding service * Add ArcGis geocoding service * Rename acrgis to arcgis * fix typo * add missing ArcGis.class * Rename arcgis to arcgis.js * fix code * Add ArcGis geocoding service
1 parent fb3228c commit 1c7025d

File tree

2 files changed

+88
-2
lines changed

2 files changed

+88
-2
lines changed

src/geocoders/arcgis.js

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
var L = require('leaflet'),
2+
Util = require('../util');
3+
4+
module.exports = {
5+
class: L.Class.extend({
6+
options: {
7+
service_url: 'http://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer'
8+
},
9+
10+
initialize: function(accessToken, options) {
11+
L.setOptions(this, options);
12+
this._accessToken = accessToken;
13+
},
14+
15+
geocode: function(query, cb, context) {
16+
var params = {
17+
SingleLine: query,
18+
outFields: 'Addr_Type',
19+
forStorage: false,
20+
maxLocations: 10,
21+
f: 'json'
22+
};
23+
24+
if (this._key && this._key.length) {
25+
params.token = this._key;
26+
}
27+
28+
Util.getJSON(this.options.service_url + '/findAddressCandidates', params, function(data) {
29+
var results = [],
30+
loc,
31+
latLng,
32+
latLngBounds;
33+
34+
if (data.candidates && data.candidates.length) {
35+
for (var i = 0; i <= data.candidates.length - 1; i++) {
36+
loc = data.candidates[i];
37+
latLng = L.latLng(loc.location.y, loc.location.x);
38+
latLngBounds = L.latLngBounds(L.latLng(loc.extent.ymax, loc.extent.xmax), L.latLng(loc.extent.ymin, loc.extent.xmin));
39+
results[i] = {
40+
name: loc.address,
41+
bbox: latLngBounds,
42+
center: latLng
43+
};
44+
}
45+
}
46+
47+
cb.call(context, results);
48+
});
49+
},
50+
51+
suggest: function(query, cb, context) {
52+
return this.geocode(query, cb, context);
53+
},
54+
55+
reverse: function(location, scale, cb, context) {
56+
var params = {
57+
location: encodeURIComponent(location.lng) + ',' + encodeURIComponent(location.lat),
58+
distance: 100,
59+
f: 'json'
60+
};
61+
62+
Util.getJSON(this.options.service_url + '/reverseGeocode', params, function(data) {
63+
var result = [],
64+
loc;
65+
66+
if (data && !data.error) {
67+
loc = L.latLng(data.location.y, data.location.x);
68+
result.push({
69+
name: data.address.Match_addr,
70+
center: loc,
71+
bounds: L.latLngBounds(loc, loc)
72+
});
73+
}
74+
75+
cb.call(context, result);
76+
});
77+
}
78+
}),
79+
80+
factory: function(accessToken, options) {
81+
return new L.Control.Geocoder.ArcGis(accessToken, options);
82+
}
83+
};

src/index.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ var L = require('leaflet'),
77
What3Words = require('./geocoders/what3words'),
88
Google = require('./geocoders/google'),
99
Photon = require('./geocoders/photon'),
10-
Mapzen = require('./geocoders/mapzen');
10+
Mapzen = require('./geocoders/mapzen'),
11+
ArcGis = require('./geocoders/arcgis');
1112

1213
module.exports = L.Util.extend(Control.class, {
1314
Nominatim: Nominatim.class,
@@ -25,7 +26,9 @@ module.exports = L.Util.extend(Control.class, {
2526
Photon: Photon.class,
2627
photon: Photon.factory,
2728
Mapzen: Mapzen.class,
28-
mapzen: Mapzen.factory
29+
mapzen: Mapzen.factory,
30+
ArcGis: ArcGis.class,
31+
arcgis: ArcGis.factory
2932
});
3033

3134
L.Util.extend(L.Control, {

0 commit comments

Comments
 (0)