mapbox / mapbox-gl-js

Interactive, thoroughly customizable maps in the browser, powered by vector tiles and WebGL
https://docs.mapbox.com/mapbox-gl-js/
Other
11.21k stars 2.22k forks source link

Marker API: add setColor, setScale, setLabel #9820

Open mariusa opened 4 years ago

mariusa commented 4 years ago

Motivation

When users select a location from a list (not on the map, a separate list), we want to highlight the corresponding marker on the map.

Marker has options to set initial color, scale, but can't change them: https://docs.mapbox.com/mapbox-gl-js/api/markers/#marker

Same for 'label', which is documented at https://docs.mapbox.com/api/maps/#marker but not at https://docs.mapbox.com/mapbox-gl-js/api/markers/#marker

Hope 'label' support wasn't dropped, it's extremely useful to add 2 digit numbers/letters to standard markers, without going through the complexity of a custom marker.

Thanks!

Design Alternatives

Design

Mock-Up

Concepts

Implementation

ryanhamley commented 4 years ago

We had a PR https://github.com/mapbox/mapbox-gl-js/pull/9427 submitted awhile back for Marker#setColor but it was closed because it wasn't flexible enough to accommodate any arbitrary marker element. It gets a bit tricky since the marker can be an SVG or a div or whatever.

Similarly, the PR https://github.com/mapbox/mapbox-gl-js/pull/9414 that added the scale option almost included getter and setter methods but we backtracked at the last minute for the same reason as with setColor. It seems like there's developer need for these types of methods though so it's probably worth figuring out a proper way to do this type of runtime update of marker properties.

andrewharvey commented 4 years ago

We had a PR #9427 submitted awhile back for Marker#setColor but it was closed because it wasn't flexible enough to accommodate any arbitrary marker element. It gets a bit tricky since the marker can be an SVG or a div or whatever.

I think in hindsight a better design might have been to have HTMLElement as a stripped down class for building your own Markers, and then have plugins for different kinds of out of the box Markers like https://leafletjs.com/plugins.html#markers--renderers. So you could have a SimpleMarker plugin which extends it which adds the default icon with customisation colour, image, style, label/number etc, rather than try to pack in and endless list of features Marker supports.

Hope 'label' support wasn't dropped, it's extremely useful to add 2 digit numbers/letters to standard markers, without going through the complexity of a custom marker.

That was never supported in GL JS, https://docs.mapbox.com/api/maps/#marker is for the Static Images API.

SephReed commented 3 years ago

What if we made it so these options: https://docs.mapbox.com/mapbox-gl-js/api/markers/#marker-parameters

were updatable? I can't seem to find any way to scale a marker after it's been created.

rolandm2017 commented 2 years ago

I still want to see an easy way to update a specific marker's color.

IMO there should be a way to say: Marker.updateColorOnMap({color: #FFFFFF })

Like that.

I guess currently I can select the marker, delete the marker, and then add a new one with a different color?

jringeisen commented 1 year ago

@plutownium that's one option or you can update the fill attribute of the first path element within the svg, like so:

let element = data.marker.getElement();
let svg = element.getElementsByTagName("svg")[0];
let path = svg.getElementsByTagName("path")[0];
path.setAttribute("fill", "#dc2626");

This is what I ended up doing and it works great.

nweissberg commented 1 month ago

@Plutownium that's one option or you can update the fill attribute of the first path element within the svg, like so:

let element = data.marker.getElement();
let svg = element.getElementsByTagName("svg")[0];
let path = svg.getElementsByTagName("path")[0];
path.setAttribute("fill", "#dc2626");

This is what I ended up doing and it works great.

Gambiarra de qualidade ⭐