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

Fixed Scale #4

Open
reyemtm opened this issue Nov 9, 2018 · 3 comments
Open

Fixed Scale #4

reyemtm opened this issue Nov 9, 2018 · 3 comments

Comments

@reyemtm
Copy link

reyemtm commented Nov 9, 2018

Is there a way to force the map to print at the current map scale (or setting a map scale) instead of having it resizing to fit the document size? I am working on getting a fixed scale (1" to x ft or mi) on my print maps. This is one of the last pieces of that I still need in order to not have to open a desktop client to print a simple map.

@ma-v
Copy link

ma-v commented Sep 11, 2019

Hi @reyemtm ,

I'm currently having the same issue. Did you find a way to fix a scale?
Cheers !

@reyemtm
Copy link
Author

reyemtm commented Sep 11, 2019

Updated. Please read new comment.

I ended up figuring out with trial and error what zoom levels result in what scale, and then having a dropdown to choose a scale for the printed map, with the map then zooming to the appropriate zoom level for the requested print scale. This is a rough estimate but good enough for my purposes.

//1" = x ft to map zoom level
var scales = {
  20:20.8,
  50:19.7,
  100:18.46,
  200:17.55
};

This only works because my maps are limited to a small geographic area. As you move around the globe the scale ratios will change. There is probably a way to figure out what the resulting scale would be regardless of geographic area and screen resolution, but I did not mess with figuring that out. I was also using a custom version of mapbox gl js with a fixed scale control. I have since stopped using this custom version and am not printing a scale control at all. For the maps printed with a known scale I just put 1" ~ x' in the attribution which gets printed with the map.

If you find a better method please share!

@reyemtm
Copy link
Author

reyemtm commented Nov 14, 2019

Fixed! Ok, sort of fixed. For proper scales (i.e. 1 inch = 200ft), the key is to adjust the scale maxWidthPercent to the percent of the target layout that would equal 1 inch. This will get the scale bar in the map to be about 1 inch, assuming you are at a proper zoom level. Then it is just a matter of playing with the zoom levels until you find one that will export the map to the proper scale. For example, in central Ohio zoom level 16.6 is equal to about 1 inch to 200 feet when printed to the letter format with quarter inch margins. However, this will change depending on your screen resolution/map container size due to this line:

renderMap.fitBounds(map.getBounds());
This line adjusts your print map to fit inside the current map container, but what we actually want is for the print map to be at the same zoom level and center of the current map, even if that means cutting off a certain percentage of area around the borders. Fitting the print map to the current map bounds will of course change the zoom, and then all your work in figuring out which zoom levels match a proper scale goes out the window. I have not found a way to use the fitBounds() method for proper scales across devices, mainly due to this issue. So the key is to delete this line and add an option to the renderMap that passes the current map zoom to the print map.

zoom: map.getZoom(),

This works well for a small area, but more work would be needed if you wanted to have proper zoom levels to print map scales for a larger area as the ratios will change depending on the longitude and latitude.

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

2 participants