Skip to content

Instantly share code, notes, and snippets.

@mourner
Last active March 28, 2016 23:27
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mourner/4284949 to your computer and use it in GitHub Desktop.
Save mourner/4284949 to your computer and use it in GitHub Desktop.
Leaflet animated markers dragging
<!doctype html>
<html>
<head>
<title>Leaflet animated markers dragging</title>
<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>
<style>
html, body, #map {
margin: 0;
height: 100%;
}
.leaflet-marker-icon,
.leaflet-marker-shadow {
-webkit-transition: margin 0.2s;
-moz-transition: margin 0.2s;
-o-transition: margin 0.2s;
transition: margin 0.2s;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
L.Marker.prototype.animateDragging = function () {
var iconMargin, shadowMargin;
this.on('dragstart', function () {
if (!iconMargin) {
iconMargin = parseInt(L.DomUtil.getStyle(this._icon, 'marginTop'));
shadowMargin = parseInt(L.DomUtil.getStyle(this._shadow, 'marginLeft'));
}
this._icon.style.marginTop = (iconMargin - 15) + 'px';
this._shadow.style.marginLeft = (shadowMargin + 8) + 'px';
});
return this.on('dragend', function () {
this._icon.style.marginTop = iconMargin + 'px';
this._shadow.style.marginLeft = shadowMargin + 'px';
});
};
var map = L.map('map').setView([50.5, 30.51], 15);
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution: '&copy; OpenStreetMap contributors'
}).addTo(map);
L.marker([50.5, 30.51], {draggable: true})
.animateDragging()
.addTo(map);
</script>
</body>
</html>
Copyright (c) 2016, Vladimir Agafonkin
Permission to use, copy, modify, and/or distribute this software for any purpose with or without fee is hereby granted, provided that the above copyright notice and this permission notice appear in all copies.
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment