Skip to content

ol-source-xyz

Layer source for tile data with URLs in a set XYZ format that are defined in a URL template.

ol-source-xyz allows you to use any tiled source from OpenStreetMaps to google maps.

Demo

Setup

Plugin usage

This component is part of the Sources 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 Sources plugin
ts
import { createApp } from "vue";
import App from "./App.vue";

import { 
  Map,
  Layers,
  Sources
} from 'vue3-openlayers';

const app = createApp(App);
// ...
app.use(Sources); 
// ...

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 Sources.

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 UsageExplicit Import
<ol-source-xyz><Sources.OlSourceXYZ>

Example of ol-source-xyz loading OSM tiles (Note that if you need an OSM layer you're better off using ol-source-osm, this is for demonstration purposes only).

Example of loading google maps satellite tiles. Notice that this is only allowed in places that are publicly available to the internet (as opposed to being behind a password for example).

vue
<template>
  <form>
    <select v-model="selected" class="select-default">
      <option value="https://{a-c}.tile.openstreetmap.org/{z}/{x}/{y}.png">
        OSM
      </option>
      <option value="https://mt1.google.com/vt/lyrs=s&x={x}&y={y}&z={z}">
        GOOGLE
      </option>
    </select>
  </form>
  <ol-map
    :loadTilesWhileAnimating="true"
    :loadTilesWhileInteracting="true"
    style="height: 400px"
  >
    <ol-view
      ref="view"
      :center="center"
      :zoom="zoom"
      :projection="projection"
    />

    <ol-tile-layer>
      <ol-source-xyz :url="selected" />
    </ol-tile-layer>
  </ol-map>
</template>

<script setup>
import { ref } from "vue";

const center = ref([40, 40]);
const projection = ref("EPSG:4326");
const zoom = ref(8);
const selected = ref("https://{a-c}.tile.openstreetmap.org/{z}/{x}/{y}.png");
</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

The following additional properties are available for setting specific params.

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.

html
<ol-source-xyz :url="url" @error="handleEvent" />

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:

vue
<template>
  <!-- ... -->
  <ol-source-xyz :url="url" ref="sourceRef" />
  <!-- ... -->
</template>

<script setup lang="ts">
import { ref, onMounted } from "vue";
import type XYZ from "ol/source/XYZ";

const sourceRef = ref<{ source: XYZ }>(null);

onMounted(() => {
  const source: XYZ = sourceRef.value?.source;
  // call your method on `source`
});
</script>