Giter Site home page Giter Site logo

vue2-leaflet-canvas's Introduction

vue2-leaflet-canvas

A Vue2Leaflet wrapper for @skinnyjames/leaflet-canvas

usage

npm install @skinnyjames/vue2-leaflet-canvas

in your app

<v-map ref="map" :zoom="10">
  <v-tilelayer url="http://{s}.tile.osm.org/{z}/{x}/{y}.png"></v-tilelayer>
  <v-canvas-layer
    :locations="locations"
    @l-drawing="drawing"
    @l-mousemove="hover"
    @l-click="click">
  </v-canvas-layer>
</v-map>
import Vue2Leaflet from 'vue2-leaflet'
import Vue2LeafletCanvas from '@skinnyjames/vue2-leaflet-canvas'

Vue.component('v-map', Vue2Leaflet.Map)
Vue.component('v-tilelayer', Vue2Leaflet.TileLayer)
Vue.component('v-canvas-layer': Vue2LeafletCanvas)

methods: {
  data() {
    // generate an array of locations that have a L.latLng property named "latlng"
    ...
    return {
      locations: locations,
    }
  },
  // use the drawing event to get access to the canvas element
  drawing(info) {
    let canvas = info.canvas
    let ctx = canvas.getContext('2d')
    let mapa = this.$refs.map.mapObject
    let bounds = mapa.getBounds()

    ctx.fillStyle = "rgb(106, 158, 242)"

    for (let i=0;i<this.locations.length;i++) {
      if (bounds.contains(this.locations[i].latlng)) {
        var dot = mapa.latLngToContainerPoint(this.locations[i].latlng)
        ctx.beginPath() 
        ctx.arc(dot.x, dot.y, 3, 0, Math.PI * 2)
        ctx.fill()
        ctx.closePath()
      }  
    }
  },
  // logic for hover
  hover(info) {
    let vm = this
    let container = document.getElementsByClassName('leaflet-container')[0]
    // isMoused has a boolean describing if a location is hovered over
    if (info.isMoused) {
      container.style.cursor = 'crosshair'
    } else {
      container.style.cursor = ''
    }
  },
  // logic for clicks
  click(info) {
    let vm = this
    // clickedLocations will have any clicked location objects
    let points = info.clickedLocations
    if (points[0]) {
      alert(points[0].message)
    }
  },
}

notes

You don't need to pass an array to the locations property, as you'll be drawing on the canvas yourself. However, the isMoused and clickedLocations properties only work when there are locations with valid latlngs

develop

git clone [email protected]:skinnyjames/vue2-leaflet-canvas.git && cd vue2-leaflet-canvas
npm install
npm run build
npm run demo

vue2-leaflet-canvas's People

Contributors

skinnyjames avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.