ol-interaction-dragbox
Allows the user to draw a vector box by clicking and dragging on the map, normally combined with a condition that limits it to when the
shift
or other key is held down.
Usage
<template>
<ol-map
:loadTilesWhileAnimating="true"
:loadTilesWhileInteracting="true"
style="height: 400px"
>
<ol-view
ref="view"
:center="center"
:zoom="zoom"
:projection="projection"
:constrainRotation="16"
/>
<ol-vector-layer background="#1a2b39" ref="vectorSourceRef">
<ol-source-vector :url="url" :format="geoJson">
<ol-style :overrideStyleFunction="styleFn"></ol-style>
</ol-source-vector>
</ol-vector-layer>
<ol-interaction-dragbox
:condition="shiftKeyOnly"
@boxstart="log('boxstart', $event)"
@boxdrag="log('boxdrag', $event)"
@boxend="log('boxend', $event)"
@boxcancel="log('boxcancel', $event)"
></ol-interaction-dragbox>
</ol-map>
</template>
<script setup lang="ts">
import type { Feature } from "ol";
import { Fill, Style } from "ol/style";
import { shiftKeyOnly } from "ol/events/condition";
import { ref, inject } from "vue";
import type { DragBoxEvent } from "ol/interaction/DragBox";
const center = ref([0, 0]);
const projection = ref("EPSG:4326");
const zoom = ref(0);
const url = ref("https://openlayers.org/data/vector/ecoregions.json");
const format = inject("ol-format");
const geoJson = new format.GeoJSON();
function styleFn(feature: Feature) {
return new Style({
fill: new Fill({
color: feature.get("COLOR_BIO") || "#eeeeee",
}),
});
}
function log(eventType: string, event: DragBoxEvent) {
console.log(eventType, event);
}
</script>
Properties
Props from OpenLayers
Properties are passed-trough from OpenLayers directly. Their types and default values can be checked-out in the official OpenLayers docs. Only some properties deviate caused by reserved keywords from Vue / HTML. This deviating props are described in the section below.
Deviating Properties
None.
Events
You have access to all Events from the underlying interaction. Check out the official OpenLayers docs to see the available events tht will be fired.
<ol-interaction-dragbox @error="handleEvent">
<!-- ... -->
</ol-interaction-dragbox>
Methods
You have access to all Methods from the underlying interaction. Check out the official OpenLayers docs to see the available methods.
To access the source, you can use a ref()
as shown below:
<template>
<!-- ... -->
<ol-map ref="mapRef" @error="handleEvent">
<!-- ... -->
</ol-map>
<!-- ... -->
</template>
<script setup lang="ts">
import { ref, onMounted } from "vue";
import type DragBox from "ol/interaction/DragBox";
const drgboxRef = ref<{ dragbox: DragBox } | null>(null);
onMounted(() => {
const dragbox: DragBox = drgboxRef.value?.dragbox;
// call your method on `dragbox`
});
</script>