-
Notifications
You must be signed in to change notification settings - Fork 92
/
custom-provider.js
106 lines (94 loc) · 2.71 KB
/
custom-provider.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
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
((win, doc) => {
const olview = new ol.View({
center: [-264000, 7480000],
zoom: 5,
minZoom: 2,
maxZoom: 20,
});
const baseLayer = new ol.layer.Tile({
source: new ol.source.OSM(),
});
const map = new ol.Map({
target: doc.querySelector('#map'),
view: olview,
layers: [baseLayer],
});
// Create an instance of the custom provider, passing any options that are
// required
const provider = OsOpenNamesSearch({
url: '//t0.ads.astuntechnology.com/open/search/osopennames/',
});
const geocoder = new Geocoder('nominatim', {
// Specify the custom provider instance as the "provider" value
provider,
targetType: 'text-input',
lang: 'en',
keepOpen: false,
preventDefault: true,
});
map.addControl(geocoder);
geocoder.on('addresschosen', (evt) => {
if (evt.bbox) {
map.getView().fit(evt.bbox, {
duration: 500
});
} else {
map.getView().animate({
zoom: 14,
center: evt.coordinate
});
}
});
/**
* Custom provider for OS OpenNames search covering Great Britian.
* Factory function which returns an object with the methods getParameters
* and handleResponse called by the Geocoder
*/
function OsOpenNamesSearch(options) {
const {
url
} = options;
return {
/**
* Get the url, query string parameters and optional JSONP callback
* name to be used to perform a search.
* @param {object} options Options object with query, key, lang,
* countrycodes and limit properties.
* @return {object} Parameters for search request
*/
getParameters(opt) {
return {
url,
callbackName: 'callback',
params: {
q: opt.query,
},
};
},
/**
* Given the results of performing a search return an array of results
* @param {object} data returned following a search request
* @return {Array} Array of search results
*/
handleResponse(results) {
// The API returns a GeoJSON FeatureCollection
if (results && results.features && results.features.length !== 0) {
return results.features.map((feature) => {
return {
lon: feature.geometry.coordinates[0],
lat: feature.geometry.coordinates[1],
address: {
// Simply return a name in this case, could also return road,
// building, house_number, city, town, village, state,
// country
name: feature.properties.search_full,
},
bbox: feature.bbox,
};
});
}
return [];
},
};
}
})(window, document);