But a couple of items were buggy, or needed workarounds.
Published Changes Not Saving
I hit the /styles/v1/ endpoint in my debugger, and see that they really are serving up the old colors, are not yet including my new layer, are still showing the old name for the layer, and so on.
Workaround: go into Mapbox Studio again, make some change and then undo it, and hit Publish again. Sometimes a third time is required.
Clicking and mouseover effects
MAP.on('click', function (e) {
const canvas = MBOVERLAY._glMap.getCanvasContainer();
const rect = canvas.getBoundingClientRect();
const glpoint = new mapboxgl.Point(e.originalEvent.clientX - rect.left - canvas.clientLeft, e.originalEvent.clientY - rect.top - canvas.clientTop);
const features = MBOVERLAY._glMap.queryRenderedFeatures(glpoint, { layers: INTERACTION_LAYERS });
// now go do something with the resulting "features" list
});
Maps becoming desynchronized on a zoom change
The workaround here was a bit of a hack: any time the map becomes idle after changing, have it call setCenter() on its own center after a brief timeout. This seems to trigger a repaint of the Mapbox GL layer, bringing it into sync with the rest of the map.
MAP.on('moveend', function () {
if (! MAP.overlay._glMap) return; // map's not ready yet e.g. startup
const center = MAP.getCenter();
setTimeout(function () {
MAP.overlay._glMap.setCenter(center);
}, 250);
});
Again, an ugly hack but a lot less ugly than what it solves!
Race conditions on startup
The workaround is not surprising: GL Map's load event, which fires after the map has first loaded and completed its first painting of the layer. Now you know that MBOVERLAY._glMap is defined and ready!
MAP.overlay = L.mapboxGL({
accessToken: OVERLAY_ACCESSTOKEN,
style: OVERLAY_MAPBOXURL,
pane: 'overlayPane',
}).addTo(MAP);
MBOVERLAY._glMap.on('load', function () {
// addSource() and addLayer()
// and additional UI event handlers for folks who won't wait before clicking
});
What Went Well?
Most things worked well! Those were the focus of the previous articles. :)
Hooray Mapbox, for a really excellent product which allows for filtering and re-styling in ways that would traditionally have required a WMS server.
Hooray Mapbox, for a really excellent product which allows for filtering and re-styling in ways that would traditionally have required a WMS server.
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.