Skip to content

Instantly share code, notes, and snippets.

@ptbrowne
Created February 5, 2015 17:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ptbrowne/5203a4060b57a1146b39 to your computer and use it in GitHub Desktop.
Save ptbrowne/5203a4060b57a1146b39 to your computer and use it in GitHub Desktop.
Leaflet Path Seeker// source http://jsbin.com/tiyuqejuso
<!DOCTYPE html>
<html>
<head>
<meta name="description" content="Leaflet Path Seeker" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<script src="https://rawgit.com/lodash/lodash/3.0.1/lodash.min.js"></script>
<script src="http://code.jquery.com/jquery-2.1.1.min.js"></script>
<meta charset="utf-8">
<title>JS Bin</title>
<link rel="stylesheet" type='text/css' href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css">
<link rel="stylesheet" type='text/css'
href='http://refreshless.com/nouislider/source/distribute/jquery.nouislider.min.css'>
<style id="jsbin-css">
body {
padding: 0;
margin: 0;
}
html, body, #map {
height: 100%;
}
.leaflet-path-seeker .noUi-handle {
border: 2px rgba(0,0,0,0.4) solid;
background: white;
border-color: ;
border-radius: 100%;
width: 40px;
height: 40px;
position: relative;
top: -15px;
}
.leaflet-path-seeker {
height: 10px;
}
/* increase touch zone */
.leaflet-path-seeker .noUi-base::before {
height: 50px;
position: relative;
top: -21px;
width: 100%;
background: transparent;
z-index: 0;
position: absolute;
content: ' ';
}
.leaflet-path-seeker.inactive {
opacity: 0.5;
}
.leaflet-path-seeker {
background: white;
border: 0;
}
.leaflet-path-seeker .noUi-handle::before, .leaflet-path-seeker .noUi-handle::after {
width: 0px;
}
</style>
</head>
<body>
<div id='map'></div>
<div id='range' type='text'/></div>
<script src='http://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js'></script>
<script src='http://cdnjs.cloudflare.com/ajax/libs/noUiSlider/6.2.0/jquery.nouislider.min.js'></script>
<script id="jsbin-javascript">
// utils
var randomWalk = function (start, n, amount) {
var cur = start;
amount = amount || 0.001;
var res = [cur];
var next;
var ldx = 0;
var ldy = 0;
for (var i=0; i<n; i++) {
next = cur.slice();
ldx = ldx + (Math.random() - 0.5) * amount;
ldy = ldy + (Math.random() - 0.5) * amount;
next[0] = next[0] + ldx;
next[1] = next[1] + ldy;
res.push(next);
cur = next;
}
return res;
};
// PathSeeker
var PathSeeker = L.Control.extend({
initialize: function (options) {
L.Util.setOptions(this, options);
this.path = options.path;
this.cur = 0;
},
onAdd: function (map) {
var container = L.DomUtil.create('div', 'leaflet-path-seeker leaflet-bar');
var pathLength = this.path.getLatLngs().length;
var $container = this.$container = $(container).noUiSlider({
start: [ 0 ],
step: 1,
range: {
'min': [ 0 ],
'max': [ pathLength - 1 ]
}
}).on('slide', _.bind(this._onSlide, this));
this._resize();
this.map = map;
map.on('dragstart', function () {
$container.addClass('inactive');
});
map.on('resize', _.bind(this._resize, this));
return container;
},
_resize: function () {
this.$container.width(map.getSize().x - 20);
},
_panTo: function (vals, map) {
var duration = 250;
var animationOptions = {
animate: true,
easeLinearity: 1,
duration: duration / 1000
};
var d = 0;
_.each(vals, function (val, i) {
_.delay(function () {
this.map.panTo(walk[val], animationOptions);
}, i * (duration + 10));
});
},
_diff: function (m,n) {
if (m > n) return m - n;
return - (n - m);
},
_onSlide: _.throttle(function (ev) {
this.$container.removeClass('inactive');
var big_step = 20;
var cur = this.cur;
var val = parseInt(this.$container.val());
var d = this._diff(val, cur);
var s = d < 0 ? -1 : 1;
if (Math.abs(d) > big_step) {
var steps = _.range(cur, val, big_step * s).slice(1).concat(val);
this._panTo(steps);
} else {
this._panTo([val]);
}
this.cur = val;
}, 200, true)
});
var map = L.map('map').setView([51.505, -0.09], 11);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
var start = [51.5, -0.09];
L.marker(start).addTo(map);
var walk = randomWalk(start, 100, 0.01);
var polyline = new L.Polyline(walk, {
color: 'red',
weight: 3,
opacity: 0.5,
smoothFactor: 5
});
polyline.addTo(map);
var c = new PathSeeker({
path: polyline,
position: 'bottomright'
}).addTo(map);
</script>
<script id="jsbin-source-html" type="text/html"><!DOCTYPE html>
<html>
<head>
<meta name="description" content="Leaflet Path Seeker" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<script src="https://rawgit.com/lodash/lodash/3.0.1/lodash.min.js"><\/script>
<script src="//code.jquery.com/jquery-2.1.1.min.js"><\/script>
<meta charset="utf-8">
<title>JS Bin</title>
<link rel="stylesheet" type='text/css' href="//cdn.leafletjs.com/leaflet-0.7.3/leaflet.css">
<link rel="stylesheet" type='text/css'
href='http://refreshless.com/nouislider/source/distribute/jquery.nouislider.min.css'>
</head>
<body>
<div id='map'></div>
<div id='range' type='text'/></div>
<script src='//cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.3/leaflet.js'><\/script>
<script src='//cdnjs.cloudflare.com/ajax/libs/noUiSlider/6.2.0/jquery.nouislider.min.js'><\/script>
</body>
</html></script>
<script id="jsbin-source-css" type="text/css">body {
padding: 0;
margin: 0;
}
html, body, #map {
height: 100%;
}
.leaflet-path-seeker .noUi-handle {
border: 2px rgba(0,0,0,0.4) solid;
background: white;
border-color: ;
border-radius: 100%;
width: 40px;
height: 40px;
position: relative;
top: -15px;
}
.leaflet-path-seeker {
height: 10px;
}
/* increase touch zone */
.leaflet-path-seeker .noUi-base::before {
height: 50px;
position: relative;
top: -21px;
width: 100%;
background: transparent;
z-index: 0;
position: absolute;
content: ' ';
}
.leaflet-path-seeker.inactive {
opacity: 0.5;
}
.leaflet-path-seeker {
background: white;
border: 0;
}
.leaflet-path-seeker .noUi-handle::before, .leaflet-path-seeker .noUi-handle::after {
width: 0px;
}</script>
<script id="jsbin-source-javascript" type="text/javascript">// utils
var randomWalk = function (start, n, amount) {
var cur = start;
amount = amount || 0.001;
var res = [cur];
var next;
var ldx = 0;
var ldy = 0;
for (var i=0; i<n; i++) {
next = cur.slice();
ldx = ldx + (Math.random() - 0.5) * amount;
ldy = ldy + (Math.random() - 0.5) * amount;
next[0] = next[0] + ldx;
next[1] = next[1] + ldy;
res.push(next);
cur = next;
}
return res;
};
// PathSeeker
var PathSeeker = L.Control.extend({
initialize: function (options) {
L.Util.setOptions(this, options);
this.path = options.path;
this.cur = 0;
},
onAdd: function (map) {
var container = L.DomUtil.create('div', 'leaflet-path-seeker leaflet-bar');
var pathLength = this.path.getLatLngs().length;
var $container = this.$container = $(container).noUiSlider({
start: [ 0 ],
step: 1,
range: {
'min': [ 0 ],
'max': [ pathLength - 1 ]
}
}).on('slide', _.bind(this._onSlide, this));
this._resize();
this.map = map;
map.on('dragstart', function () {
$container.addClass('inactive');
});
map.on('resize', _.bind(this._resize, this));
return container;
},
_resize: function () {
this.$container.width(map.getSize().x - 20);
},
_panTo: function (vals, map) {
var duration = 250;
var animationOptions = {
animate: true,
easeLinearity: 1,
duration: duration / 1000
};
var d = 0;
_.each(vals, function (val, i) {
_.delay(function () {
this.map.panTo(walk[val], animationOptions);
}, i * (duration + 10));
});
},
_diff: function (m,n) {
if (m > n) return m - n;
return - (n - m);
},
_onSlide: _.throttle(function (ev) {
this.$container.removeClass('inactive');
var big_step = 20;
var cur = this.cur;
var val = parseInt(this.$container.val());
var d = this._diff(val, cur);
var s = d < 0 ? -1 : 1;
if (Math.abs(d) > big_step) {
var steps = _.range(cur, val, big_step * s).slice(1).concat(val);
this._panTo(steps);
} else {
this._panTo([val]);
}
this.cur = val;
}, 200, true)
});
var map = L.map('map').setView([51.505, -0.09], 11);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
var start = [51.5, -0.09];
L.marker(start).addTo(map);
var walk = randomWalk(start, 100, 0.01);
var polyline = new L.Polyline(walk, {
color: 'red',
weight: 3,
opacity: 0.5,
smoothFactor: 5
});
polyline.addTo(map);
var c = new PathSeeker({
path: polyline,
position: 'bottomright'
}).addTo(map);
</script></body>
</html>
body {
padding: 0;
margin: 0;
}
html, body, #map {
height: 100%;
}
.leaflet-path-seeker .noUi-handle {
border: 2px rgba(0,0,0,0.4) solid;
background: white;
border-color: ;
border-radius: 100%;
width: 40px;
height: 40px;
position: relative;
top: -15px;
}
.leaflet-path-seeker {
height: 10px;
}
/* increase touch zone */
.leaflet-path-seeker .noUi-base::before {
height: 50px;
position: relative;
top: -21px;
width: 100%;
background: transparent;
z-index: 0;
position: absolute;
content: ' ';
}
.leaflet-path-seeker.inactive {
opacity: 0.5;
}
.leaflet-path-seeker {
background: white;
border: 0;
}
.leaflet-path-seeker .noUi-handle::before, .leaflet-path-seeker .noUi-handle::after {
width: 0px;
}
// utils
var randomWalk = function (start, n, amount) {
var cur = start;
amount = amount || 0.001;
var res = [cur];
var next;
var ldx = 0;
var ldy = 0;
for (var i=0; i<n; i++) {
next = cur.slice();
ldx = ldx + (Math.random() - 0.5) * amount;
ldy = ldy + (Math.random() - 0.5) * amount;
next[0] = next[0] + ldx;
next[1] = next[1] + ldy;
res.push(next);
cur = next;
}
return res;
};
// PathSeeker
var PathSeeker = L.Control.extend({
initialize: function (options) {
L.Util.setOptions(this, options);
this.path = options.path;
this.cur = 0;
},
onAdd: function (map) {
var container = L.DomUtil.create('div', 'leaflet-path-seeker leaflet-bar');
var pathLength = this.path.getLatLngs().length;
var $container = this.$container = $(container).noUiSlider({
start: [ 0 ],
step: 1,
range: {
'min': [ 0 ],
'max': [ pathLength - 1 ]
}
}).on('slide', _.bind(this._onSlide, this));
this._resize();
this.map = map;
map.on('dragstart', function () {
$container.addClass('inactive');
});
map.on('resize', _.bind(this._resize, this));
return container;
},
_resize: function () {
this.$container.width(map.getSize().x - 20);
},
_panTo: function (vals, map) {
var duration = 250;
var animationOptions = {
animate: true,
easeLinearity: 1,
duration: duration / 1000
};
var d = 0;
_.each(vals, function (val, i) {
_.delay(function () {
this.map.panTo(walk[val], animationOptions);
}, i * (duration + 10));
});
},
_diff: function (m,n) {
if (m > n) return m - n;
return - (n - m);
},
_onSlide: _.throttle(function (ev) {
this.$container.removeClass('inactive');
var big_step = 20;
var cur = this.cur;
var val = parseInt(this.$container.val());
var d = this._diff(val, cur);
var s = d < 0 ? -1 : 1;
if (Math.abs(d) > big_step) {
var steps = _.range(cur, val, big_step * s).slice(1).concat(val);
this._panTo(steps);
} else {
this._panTo([val]);
}
this.cur = val;
}, 200, true)
});
var map = L.map('map').setView([51.505, -0.09], 11);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
var start = [51.5, -0.09];
L.marker(start).addTo(map);
var walk = randomWalk(start, 100, 0.01);
var polyline = new L.Polyline(walk, {
color: 'red',
weight: 3,
opacity: 0.5,
smoothFactor: 5
});
polyline.addTo(map);
var c = new PathSeeker({
path: polyline,
position: 'bottomright'
}).addTo(map);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment