R
R
Rufix2019-10-21 23:39:00
leaflet
Rufix, 2019-10-21 23:39:00

How to add map from Leaflet via Vue?

app.vue

<template>
  <div id="app">
    <AppMap/>
  </div>
</template>

<script>
import AppMap from './components/AppMap'

export default {
  name: 'App',
  components: {
    AppMap
  }
}
</script>

<style>
#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;
}
</style>

Map component:
<template>
  <div class="AppMap" id="AppMap">
    <h1>{{msg}}</h1>
  </div>
</template>

<script>
const mymap = L.map('AppMap').setView([55.75222, 37.61556], 13);

const attribution = '&copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors';

const tileUrl = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
const tiles = L.tileLayer (tileUrl, {attribution});
tiles.addTo (mymap)

export default {
  name: 'AppMap',
  data () {
    return {
      msg: 'Welcome to Your Vue.js App'
    }
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
#AppMap {
  height: 500px;
  width: 800px;
}
</style>

The console gives an error "Map container is not found". Help solve the problem

Answer the question

In order to leave comments, you need to log in

1 answer(s)
0
0xD34F, 2019-10-21
@Rufix

<div ref="map">

mounted() {
  this.map = L.map(this.$refs.map).setView([ 55.75222, 37.61556 ], 13);
  ...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question