Skip to content

Commit 44249b5

Browse files
committed
feat(ol-geolocation): pass-through original events
BREAKING CHANGE: pass-through original events from Openlayers directly and remove legacy events The following events are not emitted anymore and replaced with the events from Openlayers. Please refer to [the official Openlayers docs for Geolocation](https://openlayers.org/en/latest/apidoc/module-ol_Geolocation-Geolocation.html) for details of the emitted event data format: - `positionChanged` -> `change:position` - `speedChanged` -> `change:speed` - `headingChanged` -> `change:heading` - `altitudeChanged` -> `change:altitude` - `altitudeAccuracyChanged` -> `change:altitudeAccuracy` - `accuracyGeometryChanged` -> `change:accuracyGeometry` closes #209
1 parent 6f52a8c commit 44249b5

File tree

3 files changed

+69
-98
lines changed

3 files changed

+69
-98
lines changed

docs/componentsguide/geolocation/index.md

Lines changed: 40 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -22,32 +22,53 @@ import GeoLocationDemo from "@demos/GeoLocationDemo.vue"
2222

2323
## Properties
2424

25-
### tracking
25+
### Props from OpenLayers
2626

27-
- **Type**: `boolean`
28-
- **Default**: `true`
27+
Properties are passed-trough from OpenLayers directly.
28+
Their types and default values can be checked-out [in the official OpenLayers docs](https://openlayers.org/en/latest/apidoc/module-ol_Geolocation-Geolocation.html).
29+
Only some properties deviate caused by reserved keywords from Vue / HTML.
30+
This deviating props are described in the section below.
2931

30-
Enables / disables tracking.
32+
### Deviating Properties
3133

32-
### tracking-options
34+
None.
3335

34-
- **Type**: `Object`
35-
- **Default**: `undefined`
36+
## Events
3637

37-
Tracking options. See [PositionOptions](https://www.w3.org/TR/geolocation-API/#position_options_interface) documentation.
38+
You have access to all Events from the underlying Openlayers Geolocation API.
39+
Check out [the official OpenLayers docs](https://openlayers.org/en/latest/apidoc/module-ol_Geolocation-Geolocation.html) to see the available events tht will be fired.
3840

39-
### projection
41+
```html
42+
<ol-geolocation :projection="projection" @change:position="geoLocChange" />
43+
```
4044

41-
- **Type**: `string`
42-
- **Default**: `EPSG:3857`
45+
## Methods
4346

44-
Projection of the current position.
47+
You have access to all Methods from the underlying Openlayers Geolocation API.
48+
Check out [the official OpenLayers docs](https://openlayers.org/en/latest/apidoc/module-ol_Geolocation-Geolocation.html) to see the available methods.
4549

46-
## Emits
50+
To access the source, you can use a `ref()` as shown below:
4751

48-
- `positionChanged`
49-
- `speedChanged`
50-
- `headingChanged`
51-
- `altitudeChanged`
52-
- `altitudeAccuracyChanged`
53-
- `accuracyGeometryChanged`
52+
```vue
53+
<template>
54+
<!-- ... -->
55+
<ol-geolocation
56+
:projection="projection"
57+
@change:position="geoLocChange"
58+
ref="geoLocRef"
59+
/>
60+
<!-- ... -->
61+
</template>
62+
63+
<script setup lang="ts">
64+
import { ref, onMounted } from "vue";
65+
import type Geolocation from "ol/Geolocation";
66+
67+
const geoLocRef = ref<{ geoLoc: Geolocation }>(null);
68+
69+
onMounted(() => {
70+
const geoLocation: Geolocation = geoLocRef.value?.geoLoc;
71+
// call your method on `geoLocation`
72+
});
73+
</script>
74+
```

src/components/map/OlGeoLocation.vue

Lines changed: 24 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,37 @@
1111
</template>
1212

1313
<script setup lang="ts">
14-
import { watch, computed, ref } from "vue";
15-
import Geolocation from "ol/Geolocation";
14+
import { computed, ref } from "vue";
15+
import Geolocation, { type Options } from "ol/Geolocation";
1616
import type { Coordinate } from "ol/coordinate";
1717
import type Polygon from "ol/geom/Polygon";
1818
import usePropsAsObjectProperties from "@/composables/usePropsAsObjectProperties";
19-
import type { ProjectionLike } from "ol/proj";
19+
import { useOpenLayersEvents } from "@/composables/useOpenLayersEvents";
2020
21-
const emit = defineEmits([
22-
"positionChanged",
23-
"speedChanged",
24-
"headingChanged",
25-
"altitudeChanged",
26-
"altitudeAccuracyChanged",
27-
"accuracyGeometryChanged",
28-
]);
29-
30-
const props = withDefaults(
31-
defineProps<{
32-
projection: ProjectionLike;
33-
tracking?: boolean;
34-
trackingOptions?: {
35-
enableHighAccuracy: boolean;
36-
};
37-
}>(),
38-
{
39-
projection: "EPSG:3857",
40-
tracking: true,
41-
trackingOptions: () => ({
42-
enableHighAccuracy: true,
43-
}),
44-
}
45-
);
21+
const props = withDefaults(defineProps<Options>(), {
22+
projection: "EPSG:3857",
23+
tracking: true,
24+
trackingOptions: () => ({
25+
enableHighAccuracy: true,
26+
}),
27+
});
4628
4729
const { properties } = usePropsAsObjectProperties(props);
4830
49-
const geoLoc = computed(() => {
50-
const g = new Geolocation(properties);
51-
g.on("change", changeEvt);
52-
return g;
53-
});
31+
const geoLoc = computed(() => new Geolocation(properties));
32+
33+
useOpenLayersEvents(geoLoc, [
34+
"change:accuracy",
35+
"change:accuracyGeometry",
36+
"change:altitude",
37+
"change:altitudeAccuracy",
38+
"change:heading",
39+
"change:position",
40+
"change:projection",
41+
"change:speed",
42+
"change:tracking",
43+
"change:trackingOptions",
44+
]);
5445
5546
const position = ref<Coordinate | undefined>([]);
5647
const accuracy = ref<number | undefined>(0);
@@ -60,45 +51,6 @@ const speed = ref<number | undefined>(0);
6051
const heading = ref<number | undefined>(0);
6152
const accuracyGeometry = ref<Polygon | null>(null);
6253
63-
watch(position, () => {
64-
emit("positionChanged", position.value);
65-
});
66-
67-
watch(speed, () => {
68-
emit("speedChanged", speed.value);
69-
});
70-
71-
watch(heading, () => {
72-
emit("headingChanged", heading.value);
73-
});
74-
75-
watch(altitude, () => {
76-
emit("altitudeChanged", altitude.value);
77-
});
78-
79-
watch(altitudeAccuracy, () => {
80-
emit("altitudeAccuracyChanged", altitudeAccuracy.value);
81-
});
82-
83-
watch(accuracyGeometry, () => {
84-
emit("accuracyGeometryChanged", accuracyGeometry.value);
85-
});
86-
87-
const changeEvt = () => {
88-
position.value = geoLoc.value.getPosition();
89-
speed.value = geoLoc.value.getSpeed();
90-
heading.value = geoLoc.value.getHeading();
91-
accuracy.value = geoLoc.value.getAccuracy();
92-
altitude.value = geoLoc.value.getAltitude();
93-
altitudeAccuracy.value = geoLoc.value.getAltitudeAccuracy();
94-
accuracyGeometry.value = geoLoc.value.getAccuracyGeometry();
95-
};
96-
97-
watch(geoLoc, (newVal, oldVal) => {
98-
oldVal.un("change", changeEvt);
99-
newVal.on("change", changeEvt);
100-
});
101-
10254
defineExpose({
10355
geoLoc,
10456
position,

src/demos/GeoLocationDemo.vue

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<ol-source-osm />
1818
</ol-tile-layer>
1919

20-
<ol-geolocation :projection="projection" @positionChanged="geoLocChange">
20+
<ol-geolocation :projection="projection" @change:position="geoLocChange">
2121
<template v-slot="slotProps">
2222
<!-- SlotProps: {{ slotProps }} -->
2323
<ol-vector-layer :zIndex="2">
@@ -39,18 +39,16 @@
3939
import hereIcon from "@/assets/here.png";
4040
import { ref } from "vue";
4141
import type { View } from "ol";
42+
import type { ObjectEvent } from "ol/Object";
4243
4344
const center = ref([40, 40]);
4445
const projection = ref("EPSG:4326");
45-
const zoom = ref(8);
46+
const zoom = ref(12);
4647
const rotation = ref(0);
4748
const view = ref<View>();
4849
const map = ref(null);
4950
50-
const geoLocChange = (loc: number[]) => {
51-
console.log(loc);
52-
view.value?.fit([loc[0], loc[1], loc[0], loc[1]], {
53-
maxZoom: 14,
54-
});
51+
const geoLocChange = (event: ObjectEvent) => {
52+
view.value?.setCenter(event.target?.getPosition());
5553
};
5654
</script>

0 commit comments

Comments
 (0)