forked from Dominique92/ol-geocoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom-provider.js
100 lines (93 loc) · 2.86 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
/* global Geocoder */
/*eslint strict: 0*/
(function (win, doc) {
'use strict';
var olview = new ol.View({
center: [-264000, 7480000],
zoom: 5,
minZoom: 2,
maxZoom: 20
}),
baseLayer = new ol.layer.Tile({
source: new ol.source.OSM()
}),
map = new ol.Map({
target: doc.getElementById('map'),
view: olview,
layers: [baseLayer]
});
// Create an instance of the custom provider, passing any options that are
// required
var provider = OsOpenNamesSearch({
url: '//t0.ads.astuntechnology.com/open/search/osopennames/'
});
var geocoder = new Geocoder('nominatim', {
// Specify the custom provider instance as the "provider" value
provider: provider,
autoComplete: true,
autoCompleteMinLength: 3,
targetType: 'text-input',
lang: 'en',
keepOpen: false,
preventDefault: true
});
map.addControl(geocoder);
geocoder.on('addresschosen', function (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) {
var url = options.url;
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: function (opt) {
return {
url: 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: function (results) {
// The API returns a GeoJSON FeatureCollection
if (results && results.features && results.features.length) {
return results.features.map(function (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
};
});
} else {
return;
}
}
};
}
})(window, document);