Gert Cuykens
Gert Cuykens

Reputation: 7155

Check if map is ready to queryRenderedFeatures

map.on('mousemove', function (e) {
    map.getCanvas().style.cursor = ''
    // Need to check if markers layer is ready to query else 
    // I get Type error the first few seconds when you move the mouse.
    var features = map.queryRenderedFeatures(e.point, {layers: ['markers']})
    if (!features.length) return;
    map.getCanvas().style.cursor = 'pointer'
});

Its not clear to me how I can check if map has completed rendering. The above code will give error while still rendering the map.

enter image description here

Upvotes: 1

Views: 1632

Answers (1)

Gert Cuykens
Gert Cuykens

Reputation: 7155

Found answer from another stackoverflow mapbox question.

map.on('mousemove', function (e) {
    if (!map.loaded()) return;
    map.getCanvas().style.cursor = ''
    var features = map.queryRenderedFeatures(e.point, {layers: ['markers']})
    if (!features.length) return;
    map.getCanvas().style.cursor = 'pointer'
});

Not sure but I think mapbox should check it instead in there queryRenderedFeatures.

https://github.com/mapbox/mapbox-gl-js/issues/2614

EDIT: Mapbox changed their code no longer a problem :)

Upvotes: 1

Related Questions