-
Notifications
You must be signed in to change notification settings - Fork 5
/
GeoLocationButton.js
200 lines (165 loc) · 5.65 KB
/
GeoLocationButton.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
import ol from 'openlayers'
import $ from 'jquery'
import Control from './Control'
import { addTooltip } from '../html/html'
import {VectorLayer} from '../layers/VectorLayer'
import MessageDisplay from '../MessageDisplay'
import {cssClasses} from '../globals'
import '../../less/geolocation.less'
/**
* @typedef {g4uControlOptions} GeolocationButtonOptions
* @property {boolean} [animated] if the move on the map to the geoposition should be animated
* @property {StyleLike} [style='#defaultStyle']
* @property {number} [maxZoom]
*/
/**
* This class provides a button to center the view on your current geoposition.
*/
export default class GeolocationButton extends Control {
/**
* @param {GeolocationButtonOptions} [options={}]
*/
constructor (options = {}) {
options.className = options.className || 'g4u-geolocation'
options.singleButton = true
options.element = $('<button>').get(0)
super(options)
/**
* @type {string}
* @private
*/
this.classNamePushed_ = this.className_ + '-pushed'
/**
* @type {boolean}
* @private
*/
this.animated_ = options.animated
/**
* @type {StyleLike}
* @private
*/
this.style_ = options.style || '#defaultStyle'
/**
* @type {number}
* @private
*/
this.maxZoom_ = options.maxZoom
this.setTitle(this.getTitle() || this.getLocaliser().localiseUsingDictionary('GeolocationButton title'))
this.setTipLabel(this.getTipLabel() || this.getLocaliser().localiseUsingDictionary('GeolocationButton tipLabel'))
this.get$Element()
.addClass(this.className_)
.addClass(cssClasses.mainButton)
.text(this.getTitle())
addTooltip(this.get$Element(), this.getTipLabel())
/**
* @type {MessageDisplay}
* @private
*/
this.buttonMessageDisplay_ = new MessageDisplay(this.get$Element())
/**
* @type {boolean}
* @private
*/
this.pushed_ = false
this.get$Element().on('click touch', () => {
if (ol.has.GEOLOCATION) {
if (!this.pushed_) {
this.showGeolocation()
this.pushed_ = true
this.get$Element().addClass(this.classNamePushed_)
} else {
this.hideGeolocation()
this.pushed_ = false
this.get$Element().removeClass(this.classNamePushed_)
}
} else {
let thisRef = this
console.log(thisRef.getMap().get('mobile'))
this.buttonMessageDisplay_.error(
this.getLocaliser().localiseUsingDictionary('geolocation geolocation-not-possible'),
thisRef.getMap().get('mobile') ? {position: 'top middle'} : {}
)
}
})
this.layer_ = null
this.geolocation_ = new ol.Geolocation()
this.geolocation_.on('error', () => {
let thisRef = this
this.buttonMessageDisplay_.error(
this.getLocaliser().localiseUsingDictionary('geolocation geolocation-not-possible'),
thisRef.getMap().get('mobile') ? {position: 'top middle'} : {}
)
this.hideGeolocation()
this.pushed_ = false
this.get$Element().removeClass(this.classNamePushed_)
})
}
/**
* @param {G4UMap} map
*/
setMap (map) {
if (map) {
let projection = map.getView().getProjection()
this.geolocation_.setProjection(projection)
let layerOptions = {source: new ol.source.Vector({projection: projection}), visible: true}
this.layer_ = new VectorLayer(layerOptions)
map.get('styling').styleLayer(this.layer_, this.style_)
map.getLayers().insertAt(1, this.layer_) // 0 is where the baseLayers are
} else {
this.getMap().getLayers().remove(this.layer_)
}
super.setMap(map)
}
/**
* Show the geolocation on the map as point with a circle in the size of the accuracy around
*/
showGeolocation () {
this.geolocation_.once('change:position', () => {
let view = this.getMap().getView()
let position = this.geolocation_.getPosition()
let source = this.layer_.getSource()
// point on the position
let geometry = new ol.geom.Point(position)
let feature = new ol.Feature({geometry: geometry})
source.addFeature(feature)
// radius around
let accuracy = this.geolocation_.getAccuracy()
if (accuracy > 0) {
geometry = this.geolocation_.getAccuracyGeometry()
if (!geometry) {
geometry = GeolocationButton.makeCircle_(position, view.getProjection(), accuracy)
}
feature = new ol.Feature({geometry: geometry})
source.addFeature(feature)
}
this.getMap().get('move').toExtent(geometry.getExtent(),
{animated: this.animated_, maxZoom: this.maxZoom_})
this.geolocation_.setTracking(false)
})
this.geolocation_.setTracking(true)
}
/**
* hides the geolocation geometries
*/
hideGeolocation () {
this.layer_.getSource().clear()
}
/**
* Creates a circle around a given position with a given radius in meters in a desired projection
* @param {ol.Coordinate} position
* @param {ol.ProjectionLike} projection
* @param {number} meters
* @returns {ol.geom.Circle}
* @private
*/
static makeCircle_ (position, projection, meters) {
// using assumption that earth is a sphere and that one degree in north/south direction
// on any point of the earth is 90/10000 km
let position4326 = ol.proj.transform(position, projection, 'EPSG:4326')
let secondPosition4326 = [position4326[0], position4326[1] +
meters / 1000 * 90 / 10000]
let secondPosition = ol.proj.transform(secondPosition4326, 'EPSG:4326', projection)
let radius = Math.abs(position[1] - secondPosition[1])
return new ol.geom.Circle(position, radius)
}
}