1#!/usr/bin/env python
2
3# To run this example, you need to set the GI_TYPELIB_PATH environment
4# variable to point to the gir directory:
5#
6# export GI_TYPELIB_PATH=$GI_TYPELIB_PATH:/usr/local/lib/girepository-1.0/
7
8import gi
9gi.require_version('Champlain', '0.12')
10gi.require_version('GtkChamplain', '0.12')
11gi.require_version('GtkClutter', '1.0')
12from gi.repository import GtkClutter
13from gi.repository import Gtk, Champlain, GtkChamplain
14
15ACCESS_TOKEN = "PUT YOUR ACCESS TOKEN HERE!!!"
16
17CACHE_SIZE = 100000000  # size of cache stored on disk
18MEMORY_CACHE_SIZE = 100 # in-memory cache size (tiles stored in memory)
19MIN_ZOOM = 0
20MAX_ZOOM = 19
21TILE_SIZE = 256
22LICENSE_TEXT = ""
23LICENSE_URI = "https://www.mapbox.com/tos/"
24
25
26def create_cached_source():
27    factory = Champlain.MapSourceFactory.dup_default()
28
29    tile_source = Champlain.NetworkTileSource.new_full(
30        "mapbox",
31        "mapbox",
32        LICENSE_TEXT,
33        LICENSE_URI,
34        MIN_ZOOM,
35        MAX_ZOOM,
36        TILE_SIZE,
37        Champlain.MapProjection.MERCATOR,
38        "https://a.tiles.mapbox.com/v4/mapbox.streets/#Z#/#X#/#Y#.png?access_token=" + ACCESS_TOKEN,
39        Champlain.ImageRenderer())
40
41    tile_size = tile_source.get_tile_size()
42
43    error_source = factory.create_error_source(tile_size)
44    file_cache = Champlain.FileCache.new_full(CACHE_SIZE, None, Champlain.ImageRenderer())
45    memory_cache = Champlain.MemoryCache.new_full(MEMORY_CACHE_SIZE, Champlain.ImageRenderer())
46
47    source_chain = Champlain.MapSourceChain()
48    # tile is retrieved in this order:
49    # memory_cache -> file_cache -> tile_source -> error_source
50    # the first source that contains the tile returns it
51    source_chain.push(error_source)
52    source_chain.push(tile_source)
53    source_chain.push(file_cache)
54    source_chain.push(memory_cache)
55
56    return source_chain
57
58
59GtkClutter.init([])
60
61window = Gtk.Window(type=Gtk.WindowType.TOPLEVEL)
62window.connect("destroy", Gtk.main_quit)
63
64widget = GtkChamplain.Embed()
65widget.set_size_request(640, 480)
66
67view = widget.get_view()
68
69view.set_map_source(create_cached_source())
70
71window.add(widget)
72window.show_all()
73
74Gtk.main()
75