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

Improve zooming to vectors when map is collapsed #80

Open
paul121 opened this issue Jun 26, 2020 · 1 comment
Open

Improve zooming to vectors when map is collapsed #80

paul121 opened this issue Jun 26, 2020 · 1 comment

Comments

@paul121
Copy link
Member

paul121 commented Jun 26, 2020

If the map.zoomToLayer(myLayer) is called before the farmOS-map instance has been rendered in the DOM, the resulting map view is rendered poorly once the map is displayed. This happens in farmOS with collapsed fieldsets. Might be an easy fix?

Notably map.getSize() returns [0,0] before being rendered.

A potential fix might be defaulting to a lower zoom level (meaning less zoomed in) if the map size hasn't been set. I'm not sure if the maxZoom variable is used as a "default" zoom level though.

Another solution may be to default to a sensible map size (1000x400?) in the zoomToLayer function if the map hasn't been rendered yet. I'm assuming that the larger the map size, the further zoomed out the map would appear when rendered.

Example: map_zoom_demo (this previews an asset's location on the map after the user types. when the Geometry fieldset is collapsed, the map zooms poorly)

@mstenta
Copy link
Member

mstenta commented Jul 29, 2020

Hmm yea rendering maps in collapsed fieldsets has caused issues many times for me. FWIW, we have some code in farm_theme specifically to trigger a map size update when fieldsets containing maps are expanded:

https://github.com/farmOS/farmOS/blob/9f63c4f80bf404996ff8e99a9d503e252e8b5a20/themes/farm_theme/js/map_panel.js#L5-L17

Auto-zooming in general is tricky. If different behaviors zoomToLayer() or zoomToVectors() at the same time, it can lead to weird behavior. In farmOS, I started to work around that by adding a zoom setting to some behaviors so that they could trigger zooming in some contexts but not others.

For example:

In the areas_combined behavior (which adds the gray "All areas" layer), you can specify whether or not you want to zoom to that layer: https://github.com/farmOS/farmOS/blob/9f63c4f80bf404996ff8e99a9d503e252e8b5a20/modules/farm/farm_area/js/farmOS.map.behaviors.areas_combined.js#L15

This is used in Geofields, where you may either be drawing a new geometry (in which case we want to zoom to "All areas") or you are editing an existing geometry (in which case we want to zoom to that geometry): https://github.com/farmOS/farmOS/blob/9f63c4f80bf404996ff8e99a9d503e252e8b5a20/modules/farm/farm_area/farm_area.farm_map.inc#L73

But that never felt like a perfect solution to me. It might be worth taking a step back and looking at how zooming works in general. Maybe we can overhaul it to be more sane. It's a bit tricky because we don't know what behaviors will be added to a given map, and yet we need some way of "prioritizing" which layer(s) get zoomed to.

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

No branches or pull requests

2 participants