cvalenzuela / Mappa

A canvas wrapper for Maps 🗺 🌍
https://mappa.js.org
359 stars 104 forks source link

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

Closed alsino closed 6 years ago

alsino commented 6 years ago

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 commented 6 years ago

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 commented 6 years ago

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