Integrating OpenLayers Map with VueJS: Create Layers Panel – Part 2

LayerPanel in OpenLayers and VueJS

This blog post is the second part in OpenLayers-VueJS integration series. In part one, we discussed basics of VueJS and how to integrate OpenLayers with VueJS to create a basic map.

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

To get full source code for this tutorial, click here.

To check the live demo for this tutorial, click here.

In this tutorial, we will create a Layer Panel which will display all the layers with check box to toggle visibility. We will divide this tutorial into following steps. It will make the process easy and will help you understand this tutorial in a easy way.

1. Install Quasar Framework

We will install Quasar Framework which is an Open Source VueJS framework into our project. Quasar Framework helps the developer to create high-performance VueJS user interfaces in record time. Go to vue-layers project in command terminal and then use vue add quasar command to install the quasar framework in our vue-layers project. This command will install Quasar framework into our project and will modify the main.js file of our project to integrate it with VueJS.

2. Install vuex: a state management library for VueJS

Vuex is a state management pattern + library for Vue.js applications. It serves as a centralized store for all the components in an application, with rules ensuring that the state can only be mutated in a predictable fashion.
In case of our vue-layers application, we will have multiple vue components like Map.vue, LayersPanel.vue, Legend.vue etc. The common object that will be used in all these component will be OpenLayers map object. Using vuex, we will be able to share the state of this object between different Vue components.
Use npm install vuex@next –save command to install vuex into vuejs project.

3. New project structure

Our new project structure will look like below image.

  1. index.html is where our vue.js app will be rendered
  2. src/assets/jsons/layer.json is json file that contains layers information to be added on map
  3. Map.vue and LayersPanel.vue are VueJS component. We have created Map.vue in part 1 tutorial. We will create LayersPanel.vue component in later steps.
  4. src/store.map.js is a vuex store for state management. We will create this store and learn how to use vuex store across multiple vue components.

4. Create vuex store

We will use vuex to create store for state management of objects. In the following code snippet, we are using createStore from vuex to create store. We have provide the initial state for map object in line number 7 and mutation for this object in line number 11.

The state of the object defined in store can be accessed by any Vue components. Mutation in vuex store means that we can change the state of any vuex store object in any time. We will use mutation to set the state of this vuex map object to openlayers map object in Map.vue component.
But before that, we have to configure our vue app to use this store in main.js. At line number 8, we are configuring our app to use vuex store.

5. Mutate vuex store with openlayers object in Map.vue

We have created the vuex store and have provided the initial state and mutation method to map object. Here will mutate the state of vuex map object. We will use this.$store.commit(‘setMap’, this.map) code to mutate at line number 31. Mutation will assign the new value for vuex map object. In this case, vuex map object will have openlayers map object.
Vuex map state now contains openlayers map object which is defined at line number 16. We will vuex map state in LayersPanel.vue component to add layers to map and create layers panel in UI.

6. Create LayersPanel.vue component

To create LayersPanel, we are importing layer.json file which contains all the layers information like it’s url, name, visibility etc. at line number 27. There is one issue in this component.
This component needs vuex store’s mutated map object and we are mutating this object in Map.vue component. Since LayersPanel.vue and Map.vue are independent component and there is no specific order in which they will be rendered. So we will watch for vuex store map object for change using watch method at line number 36.
We are also using map object as computed property in line number 45. We are using this property to add layers to map(see line number 60) and simultaneously showing layers in Layers panel in UI.
At line number 11 in html part, we are using map != null condition. This is because, it is vuex map state and it’s initial value is set to null. We are using map as computed property and using watch method to watch for mutation change. Once mutation is done, computed property will force the html template to rerender the LayersPanel because computed properties are reactive.
Apart from that, we are using toggleLayer function defined at line number 66 to toggle the layers visibility.

We have successfully developed the LayersPanel.vue component with toggle functionality. Now run the project using npm run serve command, you will able to see the map and layers panel in browser

To get full source code for this tutorial, click here.

To check the live demo for this tutorial, click here.

2 thoughts on “Integrating OpenLayers Map with VueJS: Create Layers Panel – Part 2”

  1. Pingback: Integrating OpenLayers Map with VueJS: Create Map - Part 1 - Spatial Dev Guru

  2. Pingback: Integrating OpenLayers Map with VueJS: Create Layers Panel – Part 2 - July 24, 2022 at 11:21AM - John Jason Fallows

Leave a Reply Cancel reply

%%footer%%