Member-only story

Using Leaflet to build a map in your Vue component

icelandcheng
Nerd For Tech
Published in
6 min readSep 29, 2024

Leaflet is a leading open-source JavaScript library for mobile-friendly interactive maps. The way to use Leaflet in Vue components is simple. This article shares some ways to add the map and the elements on the map by using Leaflet API in a Vue component. Let’s dive in!

The Setup and Basic Usage

  • Install Leaflet

Running npm or yarnto install Leaflet in your application.

npm install leaflet
  • Import Leaflet

Importing it in the Vue component that you would like to add a map.

// App.vue

import L from 'leaflet';
  • Create the map

Adding the map with openstreet tile layer as background in the Vue component.

// App.vue

<template>
<div id="map"></div>
</template>

<script>
import L from 'leaflet';

let openStreetMap = {};

export default {
...
mounted() {
openStreetMap = L.map('map', {
center: [0, 0],
zoom: 1,
});

L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
attribution: 'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a…

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Nerd For Tech
Nerd For Tech

Published in Nerd For Tech

NFT is an Educational Media House. Our mission is to bring the invaluable knowledge and experiences of experts from all over the world to the novice. To know more about us, visit https://www.nerdfortech.org/.

icelandcheng
icelandcheng

Written by icelandcheng

Programming Skill learner and Sharer | Ruby on Rails | Golang | Vue.js | Web Map API

No responses yet

Write a response