
In this tutorial, we will learn how to integrate OpenLayers map with VueJS . VueJS is a progressive framework that covers most of common features needed for the front end development. Using any framework be it ReactJS or VueJS makes sense because if your application is becoming more and more complex, using any modern framework like those can reduce the complexity in your application.
OpenLayers VueJS integration series:
1. Integrating OpenLayers Map with VueJS: Create Map – Part 1
2. Integrating OpenLayers Map with VueJS: Create Layers Panel – Part 2
3. Integrating OpenLayers Map with VueJS: Create Implement Style and legend for vector layer – Part_3
4. Integrating OpenLayers Map With VueJS: Open Feature Information Popup On Click – Part 4
5. Integrating OpenLayers Map with VueJS: Recreating the project with Vite, Pinia and TypeScript-Part 5
To get full source code for this tutorial, click here.
To check the live demo for this tutorial, click here.
Before proceeding further, you need to install nodejs and npm package manager . Make sure you have installed it
We will divide this tutorial into four following steps. It will make the process easy and will help you understand this tutorial in a easy way.
1. Install Vue CLI and Create VueJS project using Vue CLI
CLI stands for Command Line Interface. @vue/cli is an npm package and provides the vue command in your terminal. In our tutorial, this will be used to create VueJS project. You can install Vue CLI using npm install -g @vue/cli command. The -g in this command means you are installing it globally. Once Vue CLI is installed, we can use it to create a vuejs project using vue create vue-layers command where vue-layers is our project name. This command will prompt you to select which version of VueJS you want to create project. For this tutorial, we are selecting Vue 3. This command will create a VueJS boilerplate code by automatically installing related dependencies.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
(base) geoknight@pop-os:~$ npm install -g @vue/cli (base) geoknight@pop-os:~$ vue create vue-layers vue CLI v4.5.13 ┌──────────────────────────────────────────┐ │ │ │ New version available 4.5.13 → 5.0.1 │ │ Run npm i -g @vue/cli to update! │ │ │ └──────────────────────────────────────────┘ ? Please pick a preset: Default ([Vue 2] babel, eslint) ❯ Default (Vue 3) ([Vue 3] babel, eslint) Manually select features |
Once, this sample project is created, it will have following structure as shown in the image. For our OpenLayers map integration, we will modify this project accordingly.
To run the sample project we created using VueJS CLI, we will use npm run serve command. This command will compile the VueJS project and host it automatically by creating a local server. Following image will be the output of of this sample project
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
(base) geoknight@pop-os:~$ cd vue-layers (base) geoknight@pop-os:~/vue-layers$ npm run serve > vue-layers1@0.1.0 serve > vue-cli-service serve INFO Starting development server... 98% after emitting CopyPlugin DONE Compiled successfully in 1139ms 10:50:47 PM App running at: - Local: http://localhost:8080/ - Network: http://192.168.2.101:8080/ Note that the development build is not optimized. To create a production build, run npm run build. |
2. Install OpenLayers dependency
In the first step, we have created a sample project. Now, we will install OpenLayers library into this project. To install OpeLayers library, use npm install ol command. It will install latest OpenLayers version into our VueJS project.
1 |
(base) geoknight@pop-os:~/vue-layers$ npm install ol |
3. Understand Project Structure in VueJS
The VueJS project we created using vue cli has following structure. In the src folder, there are App.vue and HelloWorld.vue files. These files with .vue extension are vue components. This is where the power of VueJS lies. You can create different components for your different requirements. These components can we reused further. For our requirement, we will create a Map.vue component where we will write OpenLayers specific code to integrate it.
The following image depicts a simple representation of VueJS application. The index.html is where our vue app will be mounted/rendered. main.js will be imported in index.html. In main.js, we are creating VueJS app and importing App.vue component in it. Then we are using createApp method to create and mount/render the App.vue component to div#app element of index.html. Here App.vue is main or parent component. Map.vue and Layers.vue are the child component and will be used inside App.vue component.
1 2 3 |
import { createApp } from 'vue' import App from './App.vue' createApp(App).mount('#app') |
4. Integrate OpenLayers Map in VueJS
We are almost done with basic VueJS project setup. Now we will create Map.vue and App.vue components. Remember App.vue is parent component and Map.vue is child component.
We will first create Map.vue component. In the <template></template> tag, we write html code. We are using <div id=”map”> element where OpenLayers map will be rendered. Then, we will use <script></script> tag to write VueJS/JavaScript specific code. Here, we are importing some OpenLayers module that we will use to define OpenLayers map. Inside the script tag, we add a default exported object export default {} which will let this component to be imported in other files. We are using mounted() hook method to define our OpenLayers Map. The mounted() method is most commonly used life cycle hook method in VueJS. VueJS invokes the mounted() method when your component is added to the DOM.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
<template> <div id = "map"> </div> </template> <script> import { Map, View } from 'ol'; import { Tile as TileLayer } from 'ol/layer'; import { OSM } from 'ol/source'; export default { name: 'Map', mounted() { this.map = new Map({ target: 'map', layers: [ new TileLayer({ source: new OSM() }) ], view: new View({ center: [0, 0], zoom: 2 }) }) } } </script> |
We have defined the OpenLayers map in VueJS component. Now, we will import Map.vue component in App.vue component. After importing Map.vue component, it can be used as normal html tag after specifying it under components in export default{}. In <template></template>, we are using <Map></Map> as normal html tag.
After using Map.vue as child component in App.vue, we are creating VueJS application in main.js and then mounting the App.vue to div#app element in index.html.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<template> <Map></Map> </template> <script> import Map from './components/Map.vue' export default { name: 'App', components: { Map } } </script> <style> @import url('../node_modules/ol/src/ol.css'); #app { font-family: Avenir, Helvetica, Arial, sans-serif; -webkit-font-smoothing: antialiased; -moz-osx-font-smoothing: grayscale; text-align: center; color: #2c3e50; margin-top: 60px; } html, body, #map, #app { height: 100%; margin: 0; padding: 0; } </style> |
We have successfully integrated OpenLayers map in VueJS application. Now run the project using npm run serve command, you will able to see the map in browser
I hope this tutorial will create a good foundation for you. If you want tutorials on another GIS topic or you have any queries, please send an email at contact@spatial-dev.guru
Pingback: Integrating OpenLayers Map with VueJS: Create Layers Panel – Part 2 - Spatial Dev Guru
Pingback: Integrating OpenLayers Map with VueJS: Implement Style and legend for vector layer – Part 3 - Spatial Dev Guru
Pingback: Integrating OpenLayers Map with VueJS: Open Feature Information Popup on click – Part 4 - Spatial Dev Guru
Pingback: Integrating OpenLayers Map with VueJS: Recreating the project with Vite, Pinia and TypeScript-Part 5 - Spatial Dev Guru
Hey!
quick correction: since a while, when installing ol via npm it installes the ol.css directly in the /node_modules/ol/ directory. so in App.vue the correct path would be: @import url(‘/node_modules/ol/ol.css’);
Hope that helps!
Cheers,
Simon