ol-map
The core component of vue3-openlayers
This is the main container for all other vue3-openlayers components and has one default
slot to place them all. Usually you will use it together with ol-view
component to setup zoom
, center
, projection
and other view related properties for the map.
Setup
Plugin usage
This component is part of the Map
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 Map
plugin
import { createApp } from "vue";
import App from "./App.vue";
import {
Map,
Layers,
Sources,
} from "vue3-openlayers";
const app = createApp(App);
// ...
app.use(Map);
// ...
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 Map
.
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-map> | <Map.OlMap> |
Example of a simple map. See also documentation of ol-view
component.
<template>
<form>
<label for="zoom">Zoom:</label>
<input type="number" id="zoom" v-model="zoom" />
</form>
<ol-map style="height: 400px">
<ol-view
ref="view"
:center="center"
:rotation="rotation"
:zoom="zoom"
:projection="projection"
@change:center="centerChanged"
@change:resolution="resolutionChanged"
@change:rotation="rotationChanged"
/>
<ol-tile-layer>
<ol-source-osm />
</ol-tile-layer>
<ol-rotate-control></ol-rotate-control>
<ol-interaction-link />
</ol-map>
<ul>
<li>center : {{ currentCenter }}</li>
<li>resolution : {{ currentResolution }}</li>
<li>zoom : {{ currentZoom }}</li>
<li>rotation : {{ currentRotation }}</li>
</ul>
</template>
<script setup>
import { ref } from "vue";
const center = ref([40, 40]);
const projection = ref("EPSG:4326");
const zoom = ref(8);
const rotation = ref(0);
const currentCenter = ref(center.value);
const currentZoom = ref(zoom.value);
const currentRotation = ref(rotation.value);
const currentResolution = ref(0);
function resolutionChanged(event) {
currentResolution.value = event.target.getResolution();
currentZoom.value = event.target.getZoom();
}
function centerChanged(event) {
currentCenter.value = event.target.getCenter();
}
function rotationChanged(event) {
currentRotation.value = event.target.getRotation();
}
</script>
<style scoped>
.ol-map {
position: relative;
}
.ol-map-loading:after {
content: "";
box-sizing: border-box;
position: absolute;
top: 50%;
left: 50%;
width: 80px;
height: 80px;
margin-top: -40px;
margin-left: -40px;
border-radius: 50%;
border: 5px solid rgba(180, 180, 180, 0.6);
border-top-color: var(--vp-c-brand-1);
animation: spinner 0.6s linear infinite;
}
@keyframes spinner {
to {
transform: rotate(360deg);
}
}
</style>
re-use existing map
<template>
<div id="map" class="map" tabindex="0" style="height: 400px; width: 100%" />
<ol-map v-if="mapInstance" :instance="mapInstance">
<ol-zoomslider-control />
</ol-map>
</template>
<script setup lang="ts">
import Map from "ol/Map";
import { View } from "ol";
import TileLayer from "ol/layer/Tile";
import OSM from "ol/source/OSM";
import { onMounted, ref } from "vue";
const mapInstance = ref<Map>();
onMounted(() => {
mapInstance.value = new Map({
layers: [
new TileLayer({
source: new OSM(),
}),
],
target: "map",
view: new View({
center: [40, 40],
zoom: 2,
projection: "EPSG:4326",
}),
});
});
</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
instance
You can re-use an existing OpenLayers Map
instance. This is probably useful if your app already includes OpenLayers or some other library which uses OpenLayers and exposes its instance. The passed instance
property must be a valid instance of ol/Map
.
Events
You have access to all Events from the underlying source. Check out the official OpenLayers docs to see the available events tht will be fired.
<ol-map @error="handleEvent">
<!-- ... -->
</ol-map>
Methods
You have access to all Methods from the underlying source. 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 Map from "ol/Map";
const mapRef = ref<{ map: Map }>(null);
onMounted(() => {
const map: Map = mapRef.value?.map;
// call your method on `map`
});
</script>
Show loading state
When layers are loaded, you can show a loading indicator by styling the ol-map-loading
class.