[gnome-maps/wip/jonasdn/local-tiles: 21/21] Add --local switch to open local tile directory



commit 54e4821ec9481f30ec59968a258d77c051fb75c5
Author: Jonas Danielsson <jonas threetimestwo org>
Date:   Wed Aug 12 15:26:06 2015 +0200

    Add --local switch to open local tile directory
    
    If one have access to a directory structure such as:
      my_tiles//zoom-level/x/y.extension
    
    Then one can point maps to it as:
      $ gnome-maps --offline /path/to/my_tiles
    
    And view them, online or offline.

 src/application.js |   14 ++++++++++++++
 src/mainWindow.js  |   12 +++++++++---
 src/mapView.js     |   34 ++++++++++++++++++++++++++++++----
 3 files changed, 53 insertions(+), 7 deletions(-)
---
diff --git a/src/application.js b/src/application.js
index 1903336..ab2a261 100644
--- a/src/application.js
+++ b/src/application.js
@@ -78,6 +78,20 @@ const Application = new Lang.Class({
 
         this.parent({ application_id: 'org.gnome.Maps' });
         this._connected = false;
+
+        this.add_main_option('local',
+                             0,
+                             GLib.OptionFlags.NONE,
+                             GLib.OptionArg.FILENAME,
+                             'A path to a local tiles directory structure',
+                             null);
+
+        this.connect('handle-local-options', (function(app, options) {
+            if (options.contains('local')) {
+                let variant = options.lookup_value('local', null);
+                this.local_tile_path = variant.deep_unpack();
+            }
+        }).bind(this));
     },
 
     _checkNetwork: function() {
diff --git a/src/mainWindow.js b/src/mainWindow.js
index 02ff04c..3ad690b 100644
--- a/src/mainWindow.js
+++ b/src/mainWindow.js
@@ -70,7 +70,11 @@ const MainWindow = new Lang.Class({
 
         this._configureId = 0;
 
-        this._mapView = new MapView.MapView();
+        this._mapView = new MapView.MapView({
+            mapType: this.application.local_tile_path ?
+                MapView.MapType.LOCAL : MapView.MapType.STREET
+        });
+
         this._overlay.add(this._mapView);
 
         this._mapView.gotoUserLocation(false);
@@ -179,6 +183,7 @@ const MainWindow = new Lang.Class({
         this.connect('delete-event', this._quit.bind(this));
         this.connect('configure-event',
                      this._onConfigureEvent.bind(this));
+
         this.connect('window-state-event',
                      this._onWindowStateEvent.bind(this));
         this._mapView.view.connect('button-press-event', (function() {
@@ -190,7 +195,7 @@ const MainWindow = new Lang.Class({
         }).bind(this));
 
         this.application.connect('notify::connected', (function() {
-            if (this.application.connected)
+            if (this.application.connected || this.application.local_tile_path)
                 this._mainStack.visible_child = this._overlay;
             else
                 this._mainStack.visible_child = this._noNetworkView;
@@ -199,7 +204,8 @@ const MainWindow = new Lang.Class({
 
     _updateLocationSensitivity: function() {
         let sensitive = (Application.geoclue.state !== Geoclue.State.INITIAL &&
-                         this.application.connected);
+                         (this.application.connected ||
+                          this.application.local_tile_path));
 
         this._gotoUserLocationButton.sensitive = sensitive;
     },
diff --git a/src/mapView.js b/src/mapView.js
index c4f4675..c37023e 100644
--- a/src/mapView.js
+++ b/src/mapView.js
@@ -30,6 +30,7 @@ const Application = imports.application;
 const ContactPlace = imports.contactPlace;
 const Geoclue = imports.geoclue;
 const Location = imports.location;
+const Maps = imports.gi.GnomeMaps;
 const MapWalker = imports.mapWalker;
 const Place = imports.place;
 const PlaceMarker = imports.placeMarker;
@@ -39,6 +40,7 @@ const UserLocationMarker = imports.userLocationMarker;
 const Utils = imports.utils;
 
 const MapType = {
+    LOCAL: 'MapsLocalSource',
     STREET:  Champlain.MAP_SOURCE_OSM_MAPQUEST,
     AERIAL:  Champlain.MAP_SOURCE_OSM_AERIAL_MAP,
     CYCLING: Champlain.MAP_SOURCE_OSM_CYCLE_MAP,
@@ -71,14 +73,17 @@ const MapView = new Lang.Class({
         this.notify('routeVisible');
     },
 
-    _init: function() {
+    _init: function(params) {
         this.parent();
 
+        let mapType = params.mapType || MapType.STREET;
+        delete params.mapType;
+
         this.view = this._initView();
         this._initLayers();
 
         this._factory = Champlain.MapSourceFactory.dup_default();
-        this.setMapType(MapType.STREET);
+        this.setMapType(mapType);
 
         this._updateUserLocation();
         Application.geoclue.connect('location-changed',
@@ -150,8 +155,29 @@ const MapView = new Lang.Class({
         if (this.view.map_source.id === mapType)
             return;
 
-        let source = this._factory.create_cached_source(mapType);
-        this.view.map_source = source;
+        if (mapType !== MapType.LOCAL) {
+            let source = this._factory.create_cached_source(mapType);
+            this.view.map_source = source;
+        } else {
+            let renderer = new Champlain.ImageRenderer();
+            let source = new Maps.FileTileSource({
+                path: Application.application.local_tile_path.toString(),
+                renderer: renderer
+            });
+            try {
+                source.prepare();
+
+                this.view.map_source = source;
+                this.view.world = source.world;
+                let [lat, lon] = this.view.world.get_center();
+                this.view.center_on(lat, lon);
+            } catch(e) {
+                this.setMapType(MapType.STREET);
+                Application.application.local_tile_path = false;
+                Application.application.notify('connected');
+                Application.notificationManager.showMessage(e.message);
+            }
+        }
     },
 
     gotoUserLocation: function(animate) {


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]