Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Raster tiles

OpenStreetMap’s raster tiles, the shortest way to a map.

Raster tiles are pictures, drawn as they come. HttpTiles fetches them from a tile server and keeps them, MapMemory keeps the map’s position and zoom between frames, and Map is the widget itself, built anew in every frame.

use eframe::{App, CreationContext, Frame};
use egui::Ui;
use walkers::{HttpTiles, Map, MapMemory, Tiles, lon_lat, sources::OpenStreetMap};

pub struct RasterMap {
    tiles: HttpTiles,
    map_memory: MapMemory,
}

impl RasterMap {
    pub fn new(cc: &CreationContext<'_>) -> Self {
        Self {
            tiles: HttpTiles::new(OpenStreetMap, cc.egui_ctx.clone()),
            map_memory: MapMemory::default(),
        }
    }
}

impl App for RasterMap {
    fn ui(&mut self, ui: &mut Ui, _frame: &mut Frame) {
        let attribution = self.tiles.attribution();
        let wroclaw = lon_lat(17.03664, 51.09916);

        Map::new(Some(&mut self.tiles), &mut self.map_memory, wroclaw).show(ui, |ui, _, _, _| {
            ui.hyperlink_to(attribution.text, attribution.url);
        });
    }
}

Tile servers ask to be credited, so the closure passed to show puts a link to the attribution on top of the map. Before shipping an app using OpenStreetMap’s tiles, read its tile usage policy.