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

Cannot call map.on('load') for Mapbox GL #6

Closed
alsino opened this issue Oct 4, 2017 · 2 comments
Closed

Cannot call map.on('load') for Mapbox GL #6

alsino opened this issue Oct 4, 2017 · 2 comments

Comments

@alsino
Copy link

alsino commented Oct 4, 2017

Hi Cristóbal,

thanks again for this great library! It really helps connecting p5.js with tile maps. While playing around, I have had a small issue:

I cannot seem to call the map.on('load') method. I would need this functionality to handle events on the map object after it is initialized. Here is what I have come up with so far:

  let token = "xxx";
  let mappa = new Mappa("Mapboxgl", token);
  let myMap;
  let canvas;

  function setup(){
    canvas = createCanvas(windowWidth, windowHeight);
    myMap = mappa.tileMap(mapOptions); // lat 0, lng 0, zoom 4
    myMap.overlay(canvas);

    let mapObject = myMap.map;
  
    mapObject.on("load", function(){
      console.log("Map is ready!");
    })
}

When doing this, I get the following error:

bildschirmfoto 2017-10-04 um 15 07 49

Could you maybe help out?

@cvalenzuela
Copy link
Owner

Hi @alsino

Try this:

let token = "xxx";
let mappa = new Mappa("Mapboxgl", token);
let myMap;
let canvas;

function setup(){
  canvas = createCanvas(windowWidth, windowHeight);
  myMap = mappa.tileMap(0,0,4); // lat 0, lng 0, zoom 4

  // you can pass a callback that will execute when the map is loaded and the p5 canvas is ready.
  myMap.overlay(canvas, startListeningToEvents);
}

function startListeningToEvents () {
  myMap.map.on("load", function(){
    console.log("Map is ready!");
  })

  myMap.map.on("move", function(){
    console.log("Map is moving");
  })
}

I need to update the readme to show how to use that callback

Let me know if that works for you

@alsino
Copy link
Author

alsino commented Oct 5, 2017

Works like a charm, thanks! Probably a good idea to add this to the documentation ;)

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

2 participants