-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgeo_location_controller.js
More file actions
33 lines (28 loc) · 1011 Bytes
/
geo_location_controller.js
File metadata and controls
33 lines (28 loc) · 1011 Bytes
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
import { Controller } from "@hotwired/stimulus"
export default class extends Controller {
static targets = ["output", "longitude", "latitude"]
static values = { longitude: String, latitude: String, position: Object }
connect() {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition((position) => {
this.positionValue = position;
this.latitudeValue = position.coords.latitude;
this.longitudeValue = position.coords.longitude;
if(this.hasOutputTarget) {
this.outputTarget.innerHTML = "Latitude: " + position.coords.latitude +
"<br>Longitude: " + position.coords.longitude;
}
});
} else {
if (this.hasOutputTarget) {
this.outputTarget.innerHTML = "Geolocation is not supported by this browser.";
}
}
}
setInputField() {
if (this.hasLatitudeTarget) {
this.latitudeTarget.value = this.latitudeValue
this.longitudeTarget.value = this.longitudeValue
}
}
}