Skip to content

Commit

Permalink
Restore zoom and lat lon from a previous visit
Browse files Browse the repository at this point in the history
  • Loading branch information
humitos committed Jan 7, 2015
1 parent ff5a217 commit 250cb1f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
31 changes: 31 additions & 0 deletions web/assets/js/leaflet.restoreview.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
var RestoreViewMixin = {
restoreView: function () {
var storage = window.localStorage || {};
if (!this.__initRestore) {
this.on('moveend', function (e) {
if (!this._loaded)
return; // Never access map bounds if view is not set.

var view = {
lat: this.getCenter().lat,
lng: this.getCenter().lng,
zoom: this.getZoom()
};
storage['mapView'] = JSON.stringify(view);
}, this);
this.__initRestore = true;
}

var view = storage['mapView'];
try {
view = JSON.parse(view || '');
this.setView(L.latLng(view.lat, view.lng), view.zoom, true);
return true;
}
catch (err) {
return false;
}
}
};

L.Map.include(RestoreViewMixin);
4 changes: 3 additions & 1 deletion web/assets/js/site.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@ var spinner = 0;

// https://github.com/Leaflet/Leaflet
var map = new L.Map('map');
map.setView([-27.4927, -58.8063], 12);
if (!map.restoreView()) {
map.setView([-27.4927, -58.8063], 12);
}

// https://github.com/Turbo87/sidebar-v2/
var sidebar = L.control.sidebar('sidebar').addTo(map);
Expand Down
1 change: 1 addition & 0 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ <h1>Enlaces de interés</h1>

<!-- Javascripts -->
<script src="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.js"></script>
<script src="assets/js/leaflet.restoreview.js"></script>
<script src="assets/js/leaflet-sidebar.js"></script>
<script src="assets/js/leaflet.awesome-markers.js"></script>

Expand Down

0 comments on commit 250cb1f

Please sign in to comment.