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.
Demo
Setup
Plugin usage
This component is part of the Interactions
plugin. If not installed globally, you need to import and use
the plugin in your main.ts
or use the explicit component import (see section below).
Import and use the Interactions
plugin
import { createApp } from "vue";
import App from "./App.vue";
import {
Map,
Layers,
Sources,
Interactions,
} from "vue3-openlayers";
const app = createApp(App);
// ...
app.use(Interactions);
// ...
Explicit import
If you don't want to install a plugin, you can import the component explicitly. It's available as a child of the named export Interactions
.
NOTE
The following documentation refers to the plugin usage. Please make sure to adopt the component names, when you decided to use explicit component imports (e. g. <ol-map>
becomes <Map.OlMap>
etc.).
Usage
Plugin Usage | Explicit Import |
---|---|
<ol-interaction-dragbox> | <Interactions.OlInteractionDragbox> |
<template>
<form>
<fieldset>
<label for="opacity">Layer Opacity</label>
<input
type="range"
id="opacity"
min="0"
max="1"
step="0.1"
v-model.number="opacity"
/>
<span class="description">{{ opacity }}</span>
</fieldset>
</form>
<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"
:opacity="opacity"
>
<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 opacity = ref(1);
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-interaction-dragbox ref="dragboxRef" @error="handleEvent" />
<!-- ... -->
</template>
<script setup lang="ts">
import { ref, onMounted } from "vue";
import type DragBox from "ol/interaction/DragBox";
const dragboxRef = ref<{ dragbox: DragBox } | null>(null);
onMounted(() => {
const dragbox: DragBox = dragboxRef.value?.dragbox;
// call your method on `dragbox`
});
</script>