Create your interactive maps with Vue.js and Leaflet

Introduction & Setup

Vue.js

As said previously, we will use Vue.js. If you don’t know it yet, it is a very popular Javascript open-source framework, standing up to Angular and REact. You can find more about it by folowing this link: https://vuejs.org/

Leaflet

Leaflet is a Javascript library allowing to create your own interactive and customizable maps. Very popular and easy to use, we’re going to use its Vue.js specific version: https://vue2-leaflet.netlify.app/

Generate the project

npm install -g @cli/vue
vue create projectname
npm run serve

Creating our first component

Let’s see our generated project and go in the folder src/components to create a new Vue file. We will call it map.vue.

npm install --save leaflet
npm install --save vue2-leaflet

Adding our markers

We will then add to our map what will make it special: the l-markers. As said previously, they will be put on the map at specific points. We put them in our code just before the l-tile-layer. Here’s the associated code:

<l-marker
:key="marker.id"
:lat-lng="marker.coordinates"
>
</l-marker>
markers: [
{id: 1, coordinates: [ 49.114910, 6.178810 ]},
{id: 2, coordinates: [ 49.133290, 6.154370 ]},
{id: 3, coordinates: [ 49.102160, 6.158850 ]},
{id: 4, coordinates: [ 49.136010, 6.199630 ]},
{id: 5, coordinates: [ 49.105563, 6.182234 ]},
]

Let’s get pretty

Unsatisfied? Let’s admit, their main design isn’t really trendy. Hopefully, we can easily customize the markers icons to make them fit our graphic standards. To make these specific icons and keep our code as clean as possible, let’s create a new component that will contain our futur icons: restaurant.vue

markers: [
{id: 1, imageUrl: 'https://img.icons8.com/doodle/48/000000/fish-food--v1.png', coordinates: [ 49.114910, 6.178810 ]},
{id: 2, imageUrl: 'https://img.icons8.com/doodle/48/000000/pizza--v1.png' ,coordinates: [ 49.133290, 6.154370 ]},
{id: 3, imageUrl: 'https://img.icons8.com/doodle/48/000000/croissant--v1.png', coordinates: [ 49.102160, 6.158850 ]},
{id: 4, imageUrl: 'https://img.icons8.com/doodle/48/000000/the-toast--v2.png', coordinates: [ 49.136010, 6.199630 ]},
{id: 5, imageUrl: 'https://img.icons8.com/doodle/48/000000/hamburger.png', coordinates: [ 49.105563, 6.182234 ]},
]

--

--

French Developper, passionate about learning all kind of stuff, and sharing the few knowledge I have.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Kevin Cluzel

French Developper, passionate about learning all kind of stuff, and sharing the few knowledge I have.