Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Markers and storing map-related objects in vue component data break map layout #58

Open
Jones-S opened this issue Feb 5, 2019 · 3 comments

Comments

@Jones-S
Copy link

Jones-S commented Feb 5, 2019

Hi

I am trying to use mapbox with mapbox gl to add markers and lines.
I also would like to save a reference of the map in the data object, like you mention here:
#23

This alone works, but I am also requiring mapbox gl to add markers.
Like mentioned here: #47
together with nuxt.

Somehow this

methods: {
  initMap(map) {
    this.map = map
    const mapboxgl = require('mapbox-gl/dist/mapbox-gl')
    this.addMarkers(mapboxgl, map)
  },

will again cause the map to look very weird.
(It loads ok initially but then it switches to this grey overlay:
https://user-images.githubusercontent.com/7059580/52290750-0f861700-2971-11e9-92ac-bc45accde1f3.png)

I also mentioned this behaviour in issue #57.
There I wanted to save the markers in the data object.

So somehow this is all a bit weird, but I don't have a clue where to start.
If anyone has a hint for me, I would be very thankful.

Cheers

@Jones-S Jones-S changed the title Markers and storing map related object in component data Markers and storing map-related objects in vue component data break map layout Feb 5, 2019
@soal
Copy link

soal commented Feb 18, 2019

There isn't much to do. Just don't store Map object in data, vuex state or any reactive property. When you make Map reactive, Vue adds getters and setters and it can break internal Mapbox machinery.
Instead, you can store Map as non-reactive, for example:

created() {
  this.map = null // here we define map as non-reactive
},

methods: {
  initMap(map) {
    this.map = map
    const mapboxgl = require('mapbox-gl/dist/mapbox-gl')
    this.addMarkers(mapboxgl, map)
  }
}

Similar issue with VueMapbox library.

@Jones-S
Copy link
Author

Jones-S commented Feb 19, 2019

Ok thanks. I moved away from vue-mapbox anyway. It was too rigid for my use-case.

@Tofandel
Copy link

Had the same issue and couldn't figure out why.. The sea would disappear for a grey background

Would be nice to document this

To make the map non reactive you should do:

{
  map: null,
  methods: {
    initMap(map) {
      this.$options.map = map
      const mapboxgl = require('mapbox-gl/dist/mapbox-gl')
      this.addMarkers(mapboxgl, map)
  }
}

Oddly enough this issue didn't happen with the library @studiometa/vue-mapbox-gl which has the same usage

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants