A CLI and Ruby Gem for generating static maps from map tile servers.
- Ruby (on linux you may need the ruby-dev package)
- Imagemagick
gem install mapstatic
There are two ways to generate a static map from the mapstatic CLI.
- Specifying a bounding box and zoom level
- Specifying a center lat, center lng, width, height and zoom level
The command below generates a map of the UK using the OpenStreetMap tileset. The width and height of the resulting image (uk.png
) are determined by the bounding box and the zoom level. If you don't know the bounding box of the area then this is a useful tool.
mapstatic map uk.png \
--zoom=5 \
--bbox=-11.29,49.78,2.45,58.78
Alternatively, you can specify a central latitude and longitude and specify the width and height.
mapstatic map silicon-roundabout.png \
--zoom=18 \
--lat=51.52567 \
--lng=-0.08750 \
--width=600 \
--height=300
Mapstatic can generate maps from any slippy map tile server. The tile provider can be specified with a URL template
mapstatic map uk.png \
--zoom=5 \
--bbox=-11.29,49.78,2.45,58.78 \
--provider=http://{s}.example.com/{z}/{x}/{y}.png
{s}
is subdomain, {z}
zoom level, {x}
and {y}
are tile coordinates.
If no tile server is specified, then the OpenStreetMap tile set will be used.
After generating a map, Mapstatic will print metadata about the map to the console. If you only want to see this metadata and don't want to actually generate the map, you can use the dryrun
flag
$ mapstatic map uk.png \
> --zoom=5 \
> --bbox=-11.29,49.78,2.45,58.78 \
> --dryrun=true
{
:bbox => "-11.29,49.78,2.45,58.78",
:width => 312,
:height => 352,
:zoom => 5,
:number_of_tiles => 6
}
This is useful for seeing how large your map will be, and how many tiles it will be using without actually generating your map.
Mapstatic can be used in your application code to generate maps and get metadata about them:
require 'mapstatic'
map = Mapstatic::Map.new(
:zoom => 12,
:bbox => "-0.218894,51.450943,0.014382,51.553755",
:provider => 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png'
)
map.render_map 'london.png'
map.metadata # Returns the map metadata
Mapstatic is licensed under the MIT license.
See LICENSE for the full license text.