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

add setter for offset to marker #5758

Closed
bartvde opened this issue Nov 28, 2017 · 1 comment
Closed

add setter for offset to marker #5758

bartvde opened this issue Nov 28, 2017 · 1 comment

Comments

@bartvde
Copy link
Contributor

bartvde commented Nov 28, 2017

Currently you can only provide offset for a marker in the constructor. I'm creating a div as the element for the marker, and inside of this div a React popup component is rendered. I only know the size after addTo is called on the marker, since it will only get added tot the DOM at that point, which is too late to know the offset if it can only be provided at construction time. I'm able to work around this by using private variables [1] but I'd rather have a public method for updating the offset of a marker dynamically.

[1]

marker._offset = offset;
marker._update();

Just for reference, this is the full code:

  addPopup(popup) {
    const id = uuid.v4();
    const elem = document.createElement('div');
    elem.setAttribute('class', 'sdk-mapbox-gl-popup');
    const overlay = new mapboxgl.Marker(elem);
    const self = this;
    // render the element into the popup's DOM.
    ReactDOM.render(popup, elem, (function addInstance() {
      self.popups[id] = this;
      self.elems[id] = elem;
      this.setMap(self);
    }));

    // set the popup id so we can match the component
    //  to the overlay.
    overlay.popupId = id;
    const coord = popup.props.coordinate;
    const lngLat = new mapboxgl.LngLat(coord['0'], coord['1']);
    this.overlays.push(overlay.setLngLat(lngLat).addTo(this.map));
    const size = ReactDOM.findDOMNode(elem).getBoundingClientRect();
    const yTransform = size.height / 2 + 11;
    const xTransform = size.width / 2 - 48;
    const offset = new mapboxgl.Point.convert([xTransform, -yTransform]);
    // TODO do not use mapbox internals here
    overlay._offset = offset;
    overlay._update();
  }

I'd be willing to work on a PR for this ofcourse if the idea is accepted.

@mourner
Copy link
Member

mourner commented Nov 28, 2017

Sounds good! A PR would be welcome.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants