All Projects → phegman → Vue Mapbox Gl

phegman / Vue Mapbox Gl

Licence: gpl-3.0
A Vue.js component for Mapbox GL JS

Programming Languages

javascript
184084 projects - #8 most used programming language

Projects that are alternatives of or similar to Vue Mapbox Gl

Vue Barcode
Barcode generator for Vue.js
Stars: ✭ 164 (-32.23%)
Mutual labels:  vuejs2, vue2
Vue2 Demo
Vue 基于 Genesis + TS + Vuex 实现的 SSR demo
Stars: ✭ 2,072 (+756.2%)
Mutual labels:  vuejs2, vue2
Vue Objccn
🔥 Use Vue.js to develop a cross-platform full stack application / 用 Vue.js 开发的跨三端应用
Stars: ✭ 1,993 (+723.55%)
Mutual labels:  vuejs2, vue2
Vuetify Todo Pwa
✔️ A simple Todo PWA built with Vue CLI 3 + Vuex + Vuetify.
Stars: ✭ 160 (-33.88%)
Mutual labels:  vuejs2, vue2
Vue Emoji Picker
Very simple, yet powerful, vue emoji picker 🎉🔥🚀
Stars: ✭ 218 (-9.92%)
Mutual labels:  vuejs2, vue2
Github Ranking
🔍GitHub不同语言热门项目排行,Vue.js做页面展示
Stars: ✭ 160 (-33.88%)
Mutual labels:  vuejs2, vue2
Vue2 Scrollbar
The Simplest Pretty Scroll Area Component with custom scrollbar for Vue 2. https://bosnaufal.github.io/vue2-scrollbar
Stars: ✭ 233 (-3.72%)
Mutual labels:  vuejs2, vue2
Vue Awesome Swiper
🏆 Swiper component for @vuejs
Stars: ✭ 12,072 (+4888.43%)
Mutual labels:  vuejs2, vue2
V Bar
The virtual responsive crossbrowser scrollbar component for VueJS 2x
Stars: ✭ 216 (-10.74%)
Mutual labels:  vuejs2, vue2
Easy Dnd
A drag and drop implementation for Vue.js 2 https://codesandbox.io/s/easy-dnd-demo-9mbij https://codesandbox.io/s/easy-dnd-demo-2-xnqbz
Stars: ✭ 202 (-16.53%)
Mutual labels:  vuejs2, vue2
Vue Codemirror
⌨️ @codemirror component for @vuejs
Stars: ✭ 2,115 (+773.97%)
Mutual labels:  vuejs2, vue2
Vuemmerce
👉 Responsive ecommerce template 🛒 built with Vue.js and Nuxt.js
Stars: ✭ 223 (-7.85%)
Mutual labels:  vuejs2, vue2
Gpk admin
✨ GeekPark Content Management System
Stars: ✭ 150 (-38.02%)
Mutual labels:  vuejs2, vue2
Vue Easy Gantt
A simple Vue.js gantt chart plugin for presenting weekly tasks
Stars: ✭ 226 (-6.61%)
Mutual labels:  vuejs2, vue2
Vuex Namespaced Module Structure
📈 A Vue.js project powered by Vuex namespaced modules in a simple structure based on Large-scale Vuex application structures
Stars: ✭ 146 (-39.67%)
Mutual labels:  vuejs2, vue2
Vue Atlas
A Vue.js 2 UI component library.
Stars: ✭ 173 (-28.51%)
Mutual labels:  vuejs2, vue2
Semantic Ui Vue2
Semantic UI Integration for Vue 2
Stars: ✭ 128 (-47.11%)
Mutual labels:  vuejs2, vue2
Vue Wechat
🔥 基于Vue2.0高仿微信App的单页应用
Stars: ✭ 1,832 (+657.02%)
Mutual labels:  vuejs2, vue2
Weex Vue Starter Kit
weex starter kit in vue to use weexpack & weex both.(support hot-reload)
Stars: ✭ 182 (-24.79%)
Mutual labels:  vuejs2, vue2
Vuex Feature Scoped Structure
📈 Feature scoped Vuex modules to have a better organization of business logic code inside Vuex modules based on Large-scale Vuex application structures @3yourmind
Stars: ✭ 218 (-9.92%)
Mutual labels:  vuejs2, vue2

Mapbox GL JS Vue.js

A simple lightweight (9kb/3kb gzipped) Mapbox GL JS Vue component.

Demo

Installation

Yarn

yarn add mapbox-gl-vue

NPM

npm install mapbox-gl-vue --save

Vue CDN

Download latest vue-mapbox-gl.min.js from https://github.com/phegman/vue-mapbox-gl/releases

Include using a <script> tag

<script src="vue-mapbox-gl.min.js"></script>

Including Mapbox GL JS

This package does not include the Mapbox GL JS and CSS files. See Mapbox GL JS installation guide here: https://www.mapbox.com/install/js/

Importing Mapbox GL JS with Webpack

If you decide to include Mapbox GL JS by installing it with Yarn/NPM you should use Shimming for it to work correctly.

webpack.config.js

const webpack = require('webpack')

plugins: [
  new webpack.ProvidePlugin({
    mapboxgl: 'mapbox-gl',
  }),
]

Projects setup with Vue CLI 3:

vue.config.js

const webpack = require('webpack')

module.exports = {
  configureWebpack: {
    plugins: [
      new webpack.ProvidePlugin({
        mapboxgl: 'mapbox-gl',
      }),
    ],
  },
}

Setup

In the file you will be including the component:

<script>
import Mapbox from 'mapbox-gl-vue'

export default {
  components: { Mapbox },
}
</script>

In your template block:

<template>
  <div id="app">
    <mapbox
      access-token="your access token"
      :map-options="{
        style: 'mapbox://styles/mapbox/light-v9',
        center: [-96, 37.8],
        zoom: 3,
      }"
    />
  </div>
</template>

CSS

CSS needs to be added for the map to show up. The #map container needs a height and a width. Example:

<style>
#map {
  width: 100%;
  height: 500px;
}
</style>

Props

Vue.js Documentation https://vuejs.org/v2/guide/components.html#Props

access-token
Type: string
Required: true

Your access token is required for Mapbox to work. It can be obtained in the Mapbox Studio dashboard


map-options
Type: MapboxOptions
Required: true

Overview of available Mapbox options can be found here: https://www.mapbox.com/mapbox-gl-js/api/#map

container will default to map (giving the container an id of map). If you want to change this or use multiple map components on the same page simply set the container property.


nav-control
Type: NavigationControlOptions
Required: false
Default: { show: true, position: 'top-right' }

More information about navigation control here: https://docs.mapbox.com/mapbox-gl-js/api/#navigationcontrol


geolocate-control
Type: GeolocateControlOptions
Required: false
Default: { show: false, position: 'top-left', options: {} }

More information about geolocate control here: https://docs.mapbox.com/mapbox-gl-js/api/#geolocatecontrol


scale-control
Type: ScaleControlOptions
Required: false
Default: { show: false, position: 'top-left', options: {} }

More information about scale control here: https://docs.mapbox.com/mapbox-gl-js/api/#scalecontrol


fullscreen-control
Type: FullscreenControlOptions
Required: false
Default: { show: false, position: 'top-right' }

More information about full screen control here: https://docs.mapbox.com/mapbox-gl-js/api/#fullscreencontrol


attribution-control
Type: AttributionControlOptions
Required: false
Default: { show: false, position: 'top-right' }

More information about full screen control here: https://docs.mapbox.com/mapbox-gl-js/api/#attributioncontrol

Example

<template>
  <div id="app">
    <mapbox
      access-token="your access token"
      :map-options="{
        style: 'mapbox://styles/mapbox/light-v9',
        center: [-96, 37.8],
        zoom: 3,
      }"
      :geolocate-control="{
        show: true,
        position: 'top-left',
      }"
      :scale-control="{
        show: true,
        position: 'top-left',
      }"
      :fullscreen-control="{
        show: true,
        position: 'top-left',
      }"
    />
  </div>
</template>

Map Events

@map-init : This event is fired when the map is initialized. It can be used to integrate plugins.

All Mapbox GL JS events are available for use. List of events here: https://docs.mapbox.com/mapbox-gl-js/api/#map.event:resize

Map events can be used by adding the @map- prefix to the beginning of the Mapbox event name. For example for the click event @map-click can be used. All events are passed the mapboxgl Map instance as the first parameter and, if the event has one, the MapboxEvent as the second parameter.

For events that support specifying a layerId as documented here https://docs.mapbox.com/mapbox-gl-js/api/#map#on the layerId can be specified by using a colon to separate the event from the layerId. For example if you have a layer with an id of points the click event can be registered like so: @map-click:points

Geolocation Events

Geolocation events are available for use by adding the @geolocate- prefix to the beginning of the Mapbox event name. A list of Geolocation events can be found here: https://docs.mapbox.com/mapbox-gl-js/api/#geolocatecontrol.event:geolocate

Example

App.vue

<template>
  <div id="app">
    <mapbox
      access-token="your access token"
      :map-options="{
        style: 'mapbox://styles/mapbox/light-v9',
        center: [-96, 37.8],
        zoom: 3,
      }"
      :geolocate-control="{
        show: true,
        position: 'top-left',
      }"
      @map-load="loaded"
      @map-zoomend="zoomend"
      @map-click:points="clicked"
      @geolocate-error="geolocateError"
      @geolocate-geolocate="geolocate"
    />
  </div>
</template>

<script>
import Mapbox from 'mapbox-gl-vue'

export default {
  components: { Mapbox },
  methods: {
    loaded(map) {
      map.addLayer({
        id: 'points',
        type: 'symbol',
        source: {
          type: 'geojson',
          data: {
            type: 'FeatureCollection',
            features: [
              {
                type: 'Feature',
                geometry: {
                  type: 'Point',
                  coordinates: [-77.03238901390978, 38.913188059745586],
                },
                properties: {
                  title: 'Mapbox DC',
                  icon: 'monument',
                },
              },
              {
                type: 'Feature',
                geometry: {
                  type: 'Point',
                  coordinates: [-122.414, 37.776],
                },
                properties: {
                  title: 'Mapbox SF',
                  icon: 'harbor',
                },
              },
            ],
          },
        },
        layout: {
          'icon-image': '{icon}-15',
          'text-field': '{title}',
          'text-font': ['Open Sans Semibold', 'Arial Unicode MS Bold'],
          'text-offset': [0, 0.6],
          'text-anchor': 'top',
        },
      })
    },
    zoomend(map, e) {
      console.log('Map zoomed')
    },
    clicked(map, e) {
      const title = e.features[0].properties.title
      console.log(title)
    },
    geolocateError(control, positionError) {
      console.log(positionError)
    },
    geolocate(control, position) {
      console.log(
        `User position: ${position.coords.latitude}, ${position.coords.longitude}`
      )
    },
  },
}
</script>

<style>
#map {
  width: 100%;
  height: 500px;
}
</style>

Plugins

Plugins (https://www.mapbox.com/mapbox-gl-js/plugins/) can be integrated using the map-init event that is fired when Mapbox is initialized. Below is an example:

<template>
  <div id="app">
    <mapbox
      access-token="your access token"
      :map-options="{
        style: 'mapbox://styles/mapbox/light-v9',
        center: [-96, 37.8],
        zoom: 3,
      }"
      :geolocate-control="{
        show: true,
        position: 'top-left',
      }"
      :scale-control="{
        show: true,
        position: 'top-left',
      }"
      :fullscreen-control="{
        show: true,
        position: 'top-left',
      }"
      @map-init="mapInitialized"
    />
  </div>
</template>

<script>
import Mapbox from 'mapbox-gl-vue'

export default {
  components: { Mapbox },
  methods: {
    initialized(map) {
      const Draw = new MapboxDraw()
      map.addControl(Draw)
    },
  },
}
</script>

<style>
#map {
  width: 100%;
  height: 500px;
}
</style>

Popups

Popups can be a bit tricky if you are trying to use Vue directives inside the popup content. This is because the popups are added to the DOM by Mapbox and not compiled by Vue. See below for one approach to solving this problem.

App.vue

<template>
  <div id="app">
    <mapbox
      access-token="your access token"
      :map-options="{
        style: 'mapbox://styles/mapbox/light-v9',
        center: [-96, 37.8],
        zoom: 3,
      }"
      @map-load="loaded"
      @map-click:points="clicked"
      @map-mouseenter:points="mouseEntered"
      @map-mouseleave:points="mouseLeft"
    />
  </div>
</template>

<script>
import Mapbox from 'mapbox-gl-vue'
import PopupContent from './PopupContent.vue'

export default {
  components: { Mapbox },
  methods: {
    loaded(map) {
      map.addLayer({
        id: 'points',
        type: 'symbol',
        source: {
          type: 'geojson',
          data: {
            type: 'FeatureCollection',
            features: [
              {
                type: 'Feature',
                geometry: {
                  type: 'Point',
                  coordinates: [-77.03238901390978, 38.913188059745586],
                },
                properties: {
                  title: 'Mapbox DC',
                  icon: 'monument',
                },
              },
              {
                type: 'Feature',
                geometry: {
                  type: 'Point',
                  coordinates: [-122.414, 37.776],
                },
                properties: {
                  title: 'Mapbox SF',
                  icon: 'harbor',
                },
              },
            ],
          },
        },
        layout: {
          'icon-image': '{icon}-15',
          'text-field': '{title}',
          'text-font': ['Open Sans Semibold', 'Arial Unicode MS Bold'],
          'text-offset': [0, 0.6],
          'text-anchor': 'top',
        },
      })
    },
    clicked(map, e) {
      if (e.features) {
        const coordinates = e.features[0].geometry.coordinates.slice()

        // Ensure that if the map is zoomed out such that multiple
        // copies of the feature are visible, the popup appears
        // over the copy being pointed to.
        while (Math.abs(e.lngLat.lng - coordinates[0]) > 180) {
          coordinates[0] += e.lngLat.lng > coordinates[0] ? 360 : -360
        }

        new mapboxgl.Popup()
          .setLngLat({ lng: coordinates[0], lat: coordinates[1] })
          .setHTML('<div id="vue-popup-content"></div>')
          .addTo(map)

        new PopupContent({
          propsData: { feature: e.features[0] },
        }).$mount('#vue-popup-content')
      }
    },
    mouseEntered(map) {
      map.getCanvas().style.cursor = 'pointer'
    },
    mouseLeft(map) {
      map.getCanvas().style.cursor = ''
    },
  },
}
</script>

<style>
#map {
  width: 100%;
  height: 500px;
}
</style>

PopupContent.vue

<template>
  <div>
    <h3>{{ feature.properties.title }}</h3>
    <button @click="popupClicked">Learn more</button>
  </div>
</template>

<script>
import Vue from 'vue'
export default Vue.extend({
  props: {
    feature: {
      required: true,
      type: Object,
    },
  },
  methods: {
    popupClicked() {
      alert('Learn more clicked')
    },
  },
})
</script>

Development

Install dependencies

yarn install

Start development server

This will start a dev server with HMR at localhost:8080.

ACCESS_TOKEN=yourAccessToken yarn dev

Linting

Please make sure all your code passes linting before opening a PR.

yarn lint
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].