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

Event handling of plugins #328

Closed
ghost opened this issue Jul 24, 2017 · 10 comments
Closed

Event handling of plugins #328

ghost opened this issue Jul 24, 2017 · 10 comments

Comments

@ghost
Copy link

ghost commented Jul 24, 2017

How would a plugin e.g. mapbox-gl-draw be integrated into react-map-gl? I tried using the mapControls attribute in InteractiveMap but I got an error that simply meant it did not match the interface mapControls was expecting.

I'm also not sure how the construction of the Popup and Marker controls work. How do they manage to keep track of the map coordinates?

@ghost ghost changed the title Plugins? Event handling of plugins Jul 25, 2017
@ghost
Copy link
Author

ghost commented Jul 25, 2017

I've managed to add mapbox-gl-draw by using the InteractiveMap ref during componentDidMount, used getMap() and added with map.addControl(draw), but there's this problem with (I guess) event handling of the plugin.

For reference, here is how the demo page at https://www.mapbox.com/mapbox-gl-js/example/mapbox-gl-draw/ does it:
draw-demo

In my app, here's how it turned out:
dom-demo

Both GIF's few seconds involve me clicking in and outside of the polygon feature: there is flickering because MapboxDraw uses two sources for the added features, one -hot and one -cold. But in react-map-gl the flickering was more pronounced, and MapboxDraw event handling wasn't done properly: note the changing pointers and draggable polygon in the first, while in the second GIF, as soon as the polygon is clicked, there is a significant slowdown (compared to no MapboxDraw added to map) and the polygon isn't draggable.

I've taken a look at the event object that was being returned in an onClick handler for InteractiveMap, and there's virtually no difference from a click done inside and outside the polygon feature, which means this cannot be handled easily by a custom map control.

Is there a way to include the MapboxDraw data into the mapControls's event handler parameter? Or is there a way to return to the default event handling by Mapbox GL JS?

@Pessimistress
Copy link
Collaborator

@ralphstodomingo Marker and Popup components are rendered as children of the InteractiveMap, and access the viewport via this.context.viewport. This is part of the work migrating mapbox-gl's controls system to the React paradigm.

Unfortunately we have to disable the native event handling of mapbox-gl, because the native interaction events are only fired after the map updates and therefore break the synchronization between the map and its overlays.

@abmai This topic has come up multiple times and we have written components like this for internal projects. Maybe we should add an example to share that code?

@ghost
Copy link
Author

ghost commented Jul 31, 2017

@Pessimistress Is it right to assume that the way to move forward with this particular plugin would be to write a child component? Without using map.addControl(drawPlugin) mentioned above (since it results in the behavior above)?

@Pessimistress
Copy link
Collaborator

Just speculating - you may be able to hack map.addControl to work by setting dragPan and dragRotate to false. But our own implementation extends SVGOverlay and does not use the mapbox-gl-draw plugin at all.

@ghost
Copy link
Author

ghost commented Aug 4, 2017

@Pessimistress Can you give me pointers on how to achieve a similar implementation? I've been hacking at it from the deck.gl side but has been having problems.
Also, is it an implementation that can work alongside the use of deck.gl?

@kriscarle
Copy link

I've made an attempt at it here: https://github.com/maphubs/mapbox-gl-draw

Had to hack mapbox-gl-draw to wire it to the react-map-gl eventManager, convert the wrapped events to match what is used internally in mapbox-gl-js, disable dragPan when needed, and update the cursors.

It is also needed to overlay the controls outside of the InteractiveMap component, otherwise they cause selected features to unselect, related to #304 This unfortunately is still an issue with the react-map-gl navigation control, zooming the map fires a click clearing the selection.

@akiori
Copy link

akiori commented Mar 16, 2018

also hope to use mapbox-gl-draw, or other similar implementation in react-map-gl/deck.gl. but find it really difficult.
Hi @kriscarle , I can't run your example code correctly. can you show me a detailed code copy?

@dekkofilms
Copy link

I would also love to know, @Pessimistress, how y'all handle this internally if possible? Or what other people have come up with in solving this feature.

@kriscarle
Copy link

@akiori I didn't end up using that mapbox-gl-draw fork, decided to keep my own React wrappers around mapbox-gl because I didn't really need the deck.gl overlays.

I do remember getting my measurement tools to work using that fork, the best I can do is point you to this commit in the branch where I was testing it, maybe it will show a bit more of how I wired it up. maphubs/maphubs@94f50a0

I'm getting the react-map-gl instance via a ref, and passing it down into my measurement tool component and then to the mapbox-gl-draw instance (from my fork) so it can rewire itself to react-map-gl's event manager... bit of a hack.

macobo added a commit to macobo/react-map-gl that referenced this issue Sep 8, 2019
This prop controls if map style should be updated on changes. Also
whilst this is true panning is disabled.

You might set this to false whilst integrating with mapbox-gl-draw.

The work is based off of the following issues:

- visgl#725 - @ibgreen pointed out that this sort of prop would make for an
  easy integration.
- visgl#450
- visgl#328
@Pessimistress
Copy link
Collaborator

Closing for inactivity. The latest recommendation is to use react-map-gl-draw: http://visgl.github.io/react-map-gl/examples/draw-polygon

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

No branches or pull requests

4 participants