Skip to content

Instantly share code, notes, and snippets.

@clhenrick
Last active December 21, 2015 08:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save clhenrick/9012011 to your computer and use it in GitHub Desktop.
Save clhenrick/9012011 to your computer and use it in GitHub Desktop.
Leaflet AnimatedMarker plug-in test
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width">
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet-0.7.2/leaflet.js"></script>
<script src="http://rawgithub.com/clhenrick/Major-Studio-Two/gh-pages/map-story/html/js/pct-trail-partial.js"></script>
<script src="http://rawgithub.com/openplans/Leaflet.AnimatedMarker/master/src/AnimatedMarker.js"></script>
<script type="text/javascript" src="http://maps.stamen.com/js/tile.stamen.js?v1.2.4"></script>
<style type="text/css">
html, body {
margin: 0 auto;
padding: 0px;
width: 500px;
height: 500px;
}
#map {
width: 500px;
height: 500px;
}
</style>
</head>
<body>
<div id="map"></div>
<script type="text/javascript">
// init the map with Stamen Terrain tiles
var map = L.map('map').setView([40.3025, -121.2347], 15);
var layer = new L.StamenTileLayer("terrain");
map.addLayer(layer);
// create geojson layer
var geojson = new L.geoJson(pct);
geojson.addTo(map);
// temporary array to store geojson lat, lon
var temp = [];
var eachLayer = geojson.eachLayer(function(layer){
var coordinates = layer.feature.geometry.coordinates,
len = coordinates.length,
i = 0;
for (i; i<len; i++){
var lat = coordinates[i][1],
lon = coordinates[i][0];
// reverse order of lon, lat for L.polyline
temp.push([lat, lon]);
}
});
var line = new L.polyline(temp),
animatedMarker = L.animatedMarker(line.getLatLngs(), {
autoStart: false,
distance: 500,
interval: 1000
});
map.addLayer(animatedMarker);
// pan the map to follow the marker
setInterval(function(){
map.panTo({lon: animatedMarker['_latlng'].lng, lat: animatedMarker['_latlng'].lat});
}, 100);
// delay start so that data has time to load
setTimeout(function(){
animatedMarker.start();
}, 3000);
// function to track animatedMarker's position
// and stop it at given lat lon values
var checkLatLon = function(e) {
// lat lon values to check for
var lat = 32.608639206779,
lon = -116.49114320162762;
if (e.latlng.lng === lon && e.latlng.lat === lat){
alert("whoa!"); // shows the test works
console.log("lat lon check worked!"); // ""
animatedMarker.stop(); // doesn't seem to work?
}
}
// event listener to check animatedMarker's position
animatedMarker.on('move', checkLatLon);
</script>
</body>
</html>
@miha92
Copy link

miha92 commented Feb 18, 2015

Can you tell me how can I put multiple markers to move on the map !

Thank you!

@miha92
Copy link

miha92 commented Feb 25, 2015

I can't figure it out how to put multiple animated markers.. Please help me,

Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment