-
Notifications
You must be signed in to change notification settings - Fork 6
/
ol-opencage-geosearch.js
77 lines (65 loc) · 1.94 KB
/
ol-opencage-geosearch.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
// Original source file, works well in an HTML page using old school script import
// do not use with bundlers
// eslint-disable-next-line no-undef
class OpenCageGeosearchControl extends ol.control.Control {
constructor(options) {
const pluginOptions = options || {};
const element = document.createElement('div');
element.className = 'ol-opencage-geosearch ol-control ol-unselectable';
super({
element,
target: options.target,
});
this.options = pluginOptions;
this.element = element;
this.setCssPosition(this.options.position);
// eslint-disable-next-line no-undef
opencage.algoliaAutocomplete({
container: this.element,
plugins: [
// eslint-disable-next-line no-undef
opencage.OpenCageGeoSearchPlugin(this.options, {
onSelect: this.handleSelect.bind(this),
onActive: this.options.onActive,
onSubmit: this.options.onSubmit,
}),
],
});
}
setMap(map) {
super.setMap(map);
this.map = map;
}
setCssPosition(positionName) {
if (!positionName) return;
// deal with
// position = 'topleft', 'topright', 'bottomleft' or 'bottomright'
if (positionName.match('top')) {
this.element.style.top = 0;
}
if (positionName.match('bottom')) {
this.element.style.bottom = 0;
}
if (positionName.match('left')) {
this.element.style.left = 0;
}
if (positionName.match('right')) {
this.element.style.right = 0;
}
}
handleSelect(params) {
if (typeof this.options.onSelect === 'function') {
this.options.onSelect(params);
return;
}
const { item } = params;
// eslint-disable-next-line no-undef
const coordinates = ol.proj.transform(
[item.geometry.lng, item.geometry.lat],
'EPSG:4326',
this.getMap().getView().getProjection()
);
this.getMap().getView().setCenter(coordinates);
this.getMap().getView().setZoom(13);
}
}