Vue2Leaflet Demo

by Michael Underwood

HTML

<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.16/vue.js"></script>
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css">
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
<script src="https://unpkg.com/[email protected]/dist/vue2-leaflet.js"></script>
<body>
  <div id="app">
    <l-map :zoom="zoom" :center="center" :padding-top-left="paddingTopLeft" ref="map">
      <l-tile-layer :url="url" :attribution="attribution"></l-tile-layer>
      <l-marker :lat-lng="marker"></l-marker>
    </l-map>
  </div>
</body>

CSS

html, body, #app {
  height: 100%;
  margin: 0;
}

JavaScript

var { LMap, LTileLayer, LMarker } = Vue2Leaflet;

new Vue({
  el: '#app',
  components: { LMap, LTileLayer, LMarker },
  data() {
    return {
      zoom:13,
      center: L.latLng(47.413220, -1.219482),
      url:'http://{s}.tile.osm.org/{z}/{x}/{y}.png',
      attribution:'&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors',
      marker: L.latLng(47.413220, -1.219482),
      paddingTopLeft: [500, 500],
    }
  },
  mounted () {
  	setTimeout(this.fit, 2000);
  },
  methods: {
  	fit() {
    	const map = this.$refs.map;
      const Lmap = map.mapObject;
      const bounds = Lmap.getBounds();
      const opts = map.fitBoundsOptions;
      console.log('Fitting bounds', bounds);
      console.log('with options', opts);
    	Lmap.fitBounds(bounds, opts);
    }
  }
});