Skip to content

Commit

Permalink
fix double geolocation.getCurrentPosition() call (incorrect behavior …
Browse files Browse the repository at this point in the history
…in the Firefox - function called once)
  • Loading branch information
ashvick committed Oct 27, 2022
1 parent 95071f4 commit c683316
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 51 deletions.
9 changes: 7 additions & 2 deletions src/components/poll/FeltReportPoll.vue
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,20 @@
<script>
import apiSettings from '@/settings/api'
import EarthquakeQuestions from './EarthquakeQuestions'
import geolocation from './mixins/geolocation'
import timezone from './mixins/timezone'
import DateTimeQuestions from './DateTimeQuestions'
import LocationQuestions from './LocationQuestions'
export default {
name: 'FeltReportPoll',
components: {LocationQuestions, DateTimeQuestions, EarthquakeQuestions},
mixins: [geolocation],
mixins: [timezone],
data() {
return {
location: {
lat: null,
lon: null
},
localisations: [],
questions: [],
actions: {},
Expand Down Expand Up @@ -109,6 +113,7 @@ export default {
deep: true,
handler: function(value) {
this.requestData.location = value
this.getTimezone()
}
}
},
Expand Down
46 changes: 27 additions & 19 deletions src/components/poll/LocationQuestions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -48,28 +48,36 @@ export default {
}
this.$emit('update', location)
this.marker = window.L.marker(event.latlng, {icon}).addTo(this.map.object)
},
getCurrentPosition() {
let vm = this
window.navigator.geolocation.getCurrentPosition(
function(position) {
vm.map.coordinates = [position.coords.latitude, position.coords.longitude]
vm.map.object = createMap(vm.map.id, vm.map.coordinates, {zoom: 10})
vm.addMarker({latlng: {lat: position.coords.latitude, lng: position.coords.longitude}})
vm.watchMapClick()
},
function(error) {
const errorTypes = {
1: 'PERMISSION_DENIED',
2: 'POSITION_UNAVAILABLE',
3: 'TIMEOUT'
}
console.error(error.message, {code: errorTypes[error.code]})
vm.map.object = createMap(vm.map.id, vm.map.coordinates, {zoom: 10})
vm.watchMapClick()
}
)
}
},
mounted() {
let vm = this
window.navigator.geolocation.getCurrentPosition(
function(position) {
vm.map.coordinates = [position.coords.latitude, position.coords.longitude]
vm.map.object = createMap(vm.map.id, vm.map.coordinates, {zoom: 10})
vm.addMarker({latlng: {lat: position.coords.latitude, lng: position.coords.longitude}})
vm.watchMapClick()
},
function(error) {
const errorTypes = {
1: 'PERMISSION_DENIED',
2: 'POSITION_UNAVAILABLE',
3: 'TIMEOUT'
}
console.error(error.message, {code: errorTypes[error.code]})
vm.map.object = createMap(vm.map.id, vm.map.coordinates, {zoom: 10})
vm.watchMapClick()
}
)
if (window.navigator.geolocation) {
this.getCurrentPosition()
}
else {
console.warn('GeoLocation API is unavailable for your browser')
}
}
}
</script>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,6 @@ import ts from '@mapbox/timespace'
export default {
data() {
return {
location: {
lat: null,
lon: null
},
timezoneOffset: 0,
timezone: 'UTC'
}
Expand All @@ -29,32 +25,6 @@ export default {
const time = ts.getFuzzyLocalTimeFromPoint(timestamp, point)
this.timezoneOffset = time._d.getTimezoneOffset()
this.timezone = this.formatTimezone(this.timezoneOffset)
},
getCurrentLocation() {
let vm = this
window.navigator.geolocation.getCurrentPosition(
function(position) {
vm.location.lat = position.coords.latitude
vm.location.lon = position.coords.longitude
vm.getTimezone()
},
function(error) {
const errorTypes = {
1: 'PERMISSION_DENIED',
2: 'POSITION_UNAVAILABLE',
3: 'TIMEOUT'
}
console.error(error.message, {code: errorTypes[error.code]})
}
)
}
},
mounted() {
if (window.navigator.geolocation) {
this.getCurrentLocation()
}
else {
console.warn('GeoLocation API is unavailable for your browser')
}
}
}

0 comments on commit c683316

Please sign in to comment.