Skip to content

Mapbox/React wrapper - #87

Merged
easbar merged 85 commits into
masterfrom
react_mapbox_wrapper
Jun 21, 2021
Merged

easbar merged 85 commits into
masterfrom
react_mapbox_wrapper

Conversation

@easbar

@easbar easbar commented May 25, 2021 •

Copy link
Copy Markdown
Member

I was curious and tried Uber's Mapbox/React wrapper: https://visgl.github.io/react-map-gl/

This is just experimental and not meant to be merged in the current state. This branch is also based on top of #77 and #75, but that should not be so important. I just did not want to do the migration without the fix in #77 and before the path detail code was moved out of the map component in #75.

The philosophy of the wrapper is interesting: https://visgl.github.io/react-map-gl/docs#design-philosophy

The wrapper is meant to address the problem that Mapbox is stateful and therefore does not integrate so well with React. We already faced some of the related issues like switching the background layer without re-creating the Map component or changing the bounding box in #72 etc. So I was hoping using this wrapper the viewport updates and the style switching feels more natural.
The migration actually went relatively smooth and I did not run into bigger issues yet. The state handling definitely feels better to me and it seems like less workarounds are required.

Other advantages could be that according to the documentation the wrapper works with mapbox-gl 1&2 and maplibre as well (I only tried mapbox-gl 1 so far) (#81) and it will be easier to move the different map layers out of the central map component (#78), just like I already moved out the viewport state handling. Another advantage could be easier integration with deck.gl (but I am just speculating and not sure if we even need this). Anyway, so far I do not see many reasons not to use the wrapper. At least we should keep this in mind and maybe comeback here when we see more issues related to the Mapbox state management for example.

I will update this branch when #75 and #77 are finished so it will be easier to track the changes. Otherwise, there are the following todos on this branch to make it equal to master/#75/#77, currently (feel free to edit this in case you notice something):

  • use actual marker symbols for query points and the path detail hovering. this should not be a real problem, but I just could not find any default marker svgs to use. For the path detail marker we might need to adjust the code because I could not find a way how to use the HTML node we get from heightgraph currently as marker. improve look of the point markers and the path detail marker. so far I use a rather ugly point marker and just open a popup for the path detail marker. -> postponed to Numbered markers #106
  • put back the long touch handler (I just commented it out for now)
  • tune the fly to animation (it looks like its not entirely the same as before yet)
  • the Popup is missing the closeOnMove option
  • missing maxBounds option for the map (it zooms out too much)
  • should we move each map layer to a separate file/component so Map.tsx does not have to deal with all the different things? I think so and did this in Move layers to separate files #108

On the other hand this branch already fixes #82 (and maybe #80 as well) and it also implements the missing 'center map' context menu entry.

Btw there is also another such wrapper here: https://github.com/urbica/react-map-gl

easbar added 30 commits May 13, 2021 14:08
# Conflicts:
#	src/actions/Actions.ts
#	src/map/Mapbox.ts
# Conflicts:
#	src/App.tsx
#	src/map/Map.tsx
#	src/map/Mapbox.ts
# Conflicts:
#	src/App.tsx
#	src/api/Api.ts
#	src/stores/QueryStore.ts
# Conflicts:
#	src/App.module.css
#	src/App.tsx
#	src/custom.d.ts
#	src/map/Map.tsx
#	src/map/Mapbox.ts
# Conflicts:
#	src/App.tsx
#	src/actions/Actions.ts
#	src/index.tsx
#	src/map/Mapbox.ts
@Janekdererste

Copy link
Copy Markdown
Collaborator

Btw the bounding box is now more fixed than in master. In master we see empty space above the map when we scroll vertically (and zoom out too much)

I like this very much. The white bars were terrible.

@karussell

Copy link
Copy Markdown
Member

This is nice 👍

@Janekdererste

Copy link
Copy Markdown
Collaborator

So, I changed the Dispatch stuff and moved everything into the Viewport store.

I also noticed that there is no easing on zoom. Do we need to specify an easing function? The examples on the mapbox component page seem to have a smooth animation when I zoom by mouse wheel.

@Janekdererste

Copy link
Copy Markdown
Collaborator

Unit testing? At least of the store?

@easbar

easbar commented Jun 17, 2021

Copy link
Copy Markdown
Member Author

I also noticed that there is no easing on zoom. Do we need to specify an easing function? The examples on the mapbox component page seem to have a smooth animation when I zoom by mouse wheel.

Yes, see the end of this comment #87 (comment) and the todo in the calculateLatLngFromBbox function. I just wasn't sure what we want here, but feel free to tune this.

@easbar

easbar commented Jun 17, 2021

Copy link
Copy Markdown
Member Author

So, I changed the Dispatch stuff and moved everything into the Viewport store.

Thanks for this. To me it seems a bit strange that the ViewportStore knows about all the routesuccess, pathdetail etc. actions. Changing the viewport rather seems like a basic feature of the map that is used by different more specific features (like routes and path details). So I would have expected the dependency the other way around: The route store (same with path detail) is a client of the viewport store. For example when we add more features that modify the viewport I would rather add more client code that uses the viewport store than modify the viewport store's reduce function every time.

@Janekdererste

Janekdererste commented Jun 17, 2021 •

Copy link
Copy Markdown
Collaborator

To me it seems a bit strange that the ViewportStore knows about all the routesuccess, pathdetail etc. actions

That's an intersting question. I've thought of a store as something which holds and manages a portion of the app's state. The store reacts to Actions happening in the app and updates its state accordingly. So, when we want the viewport to be different after a certain event occured, the ViewportStore should take care of it. Also, every action is dispatched to every store anyway, so that every store knows about every action already.

I would rather add more client code that uses the viewport store than modify the viewport store's reduce function every time.

I think the set of actions our app has should represent the events that happen in our app. Introducing actions like the SetViewportFromBbox action don't actually represent an event of the app but a desired change to the state caused by something else ( a successfull routing request for example)

@easbar

easbar commented Jun 17, 2021

Copy link
Copy Markdown
Member Author

Ok, I understand there is a difference between 'reacting to a state change' and sending an event like SetViewportFromBBox. So feel free to leave it like this for now and I might think about this a bit more.

Also, every action is dispatched to every store anyway, so that every store knows about every action already.

No, I don't think so. Every action is passed to every store yes, but the code of the different stores is not coupled to all actions and the stores do not contain any code related to actions they do not handle explicitly. We can add/remove actions without changing all the stores. But now we cannot remove the path detail stuff (for example not needed for isochrone) without adjusting the (IMO unrelated) viewport changing code.

@easbar

easbar commented Jun 19, 2021

Copy link
Copy Markdown
Member Author

Unit testing? At least of the store?

I added a test in my last commit. It is actually quite nice that we can test the viewport transitions on this level now.

Comment thread jest.config.js Outdated
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
testEnvironment: 'jsdom',

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this ok? I needed this for the viewportstore test as it uses window.matchMedia.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have no preference here. I think I just put in, what the "getting-started-with-jest" tutorial says.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reverted. jsdom sounds more correct but who knows and as long as it works with the current value no need to change

Comment on lines +39 to +43
// ... select path details
state = store.reduce(state, new PathDetailsRangeSelected([10.46, 50.88, 10.47, 50.9]))
expect(state.longitude).toBeCloseTo(10.46, 2)
expect(state.latitude).toBeCloseTo(50.89, 2)
expect(state.zoom).toBeCloseTo(13.7, 1)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I really like we can do this now.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, this looks very nice.

Comment thread test/matchMedia.mock.ts Outdated
Comment on lines +1 to +3
Object.defineProperty(window, 'matchMedia', {
writable: true,
value: jest.fn().mockImplementation(query => ({

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I needed this because viewport store uses window.matchMedia.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment below.

Comment thread src/stores/ViewportStore.ts Outdated
Comment on lines +34 to +35
// have this right here for now. Not sure if this needs to be abstracted somewhere else
const mediaQuery = window.matchMedia('(max-width: 640px)')

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This makes testing the store a bit complicated, see my other comments. Can we just inject this into the constructor? And is it the same as isSmallScreen in App.tsx? Here we use 640px and in App.tsx we have something similar with 44rem.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it would be better to have this media query at 44rem as well. And yes, it is supposed to be the same as isSmallScreen.

I guess you could even inject something like isSmallScreen: () => boolean and pass new ViewportStore(() => window.matchMedia(...)) in index.tsx. Then in the test you could supply your own predicate.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did this. The only thing is that we have this media query twice now. One time in index.tsx where we use window.matchMedia and one time in App.tsx where we use the useMediaQuery hook. We cannot use the hook in index.tsx because it would be outside of any react component and I thought there is probably a reason you use the useMediaQuery hook in App.tsx

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the hook is used, so that the layout changes when the window is resized.

There are several other places in css where this media query is used as well. I think as long as we only have one breakpoint we may be fine.

Comment on lines 86 to 125
? {
top: 250,
bottom: 150,
top: 200,
bottom: 32,
right: 16,
left: 16,
}
: {
top: 100,
bottom: 100,
bottom: 300,
right: 100,
left: 500,
}

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice! This fixed the problem that sometimes the markers were hidden by the path detail diagram, see #53

@karussell

karussell commented Jun 21, 2021 •

Copy link
Copy Markdown
Member

This seems to solve several issue like @easbar said also the issue where the elevation widget hides the routes 👍
It zooms also to the route when you select the alternative route which is really nice.

There is a minor problem with the path details widget (not too important :) ) but it is hard to describe. Sometimes the elevation/pathdetails data does not show up when the mouse goes over the area (try hovering left and right). There seem to be areas that just do not work.

Hm ... the path detail widget with current mapbox seems to have this problem too but only for small areas of the widget.

@easbar

easbar commented Jun 21, 2021

Copy link
Copy Markdown
Member Author

also the issue where the elevation widget hides the routes

Yes, even though this was unrelated to the wrapper: #87 (comment)

@easbar
easbar merged commit f410367 into master Jun 21, 2021
@easbar
easbar deleted the react_mapbox_wrapper branch June 21, 2021 14:44
@easbar easbar mentioned this pull request Jun 21, 2021
@karussell

karussell commented Jun 21, 2021 •

Copy link
Copy Markdown
Member

I just played chaos monkey a bit more and now an error occurred when using the same coordinates (like #36). But now an error happens like "e is undefined". To avoid this in the future would we already need something like playwright or cypress or is this possible in a unit test?

@easbar

easbar commented Jun 21, 2021

Copy link
Copy Markdown
Member Author

This error occurs because we do this on the server-side:
java

 if (p.getPoints().size() >= 2) {
     jsonPath.putPOJO("bbox", p.calcBBox2D());
 }

so when the points are identical the bbox field is missing and we do not handle this yet. Things like this can be tested without UI/browser tests.

@easbar

easbar commented Jun 21, 2021

Copy link
Copy Markdown
Member Author

Should we change this on the server-side? It seems a bit ugly that the bbox is missing in this case. Why not just return [lat, lon, lat, lon] in case we request a route from (lat, lon) to (lat, lon)?

@easbar

easbar commented Jun 21, 2021

Copy link
Copy Markdown
Member Author

I made the error more visible by using a debug build for aws: d731b67 and logging errors for /route requests: fcfccc5

@karussell

Copy link
Copy Markdown
Member

I wasn't able to find the reason but will change this on the server side as this made problems before. The oldest location seems to be here.

ZeroGxMax pushed a commit to minhhpkp/graphhopper-maps that referenced this pull request Nov 12, 2024
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

Successfully merging this pull request may close these issues.

Do not show flicker/jump when map loads with route coordinates

3 participants