Skip to content

Instantly share code, notes, and snippets.

@hanfeisun
Last active August 29, 2015 14:27
Show Gist options
  • Save hanfeisun/70107f45bc480844702b to your computer and use it in GitHub Desktop.
Save hanfeisun/70107f45bc480844702b to your computer and use it in GitHub Desktop.
// 能旋转的Marker类
L.RotatedMarker = L.Marker.extend({
options: {angle: 0},
_setPos: function (pos) {
L.Marker.prototype._setPos.call(this, pos);
if (L.DomUtil.TRANSFORM) {
// use the CSS transform rule if available
this._icon.style[L.DomUtil.TRANSFORM] += ' rotate(' + this.options.angle + 'deg)';
} else if (L.Browser.ie) {
// fallback for IE6, IE7, IE8
// Just ignore it!
}
}
});
L.rotatedMarker = function (pos, options) {
return new L.RotatedMarker(pos, options);
};
// 将两个GPS坐标转换为角度
function ConvertPositionAngel(soucePoint, targetPoint) {
var res = (Math.atan2(targetPoint.y - soucePoint.y, targetPoint.x - soucePoint.x)) / Math.PI * 180.0;
return (res >= 0 && res <= 180) ? res += 90 : ((res < 0 && res >= -90) ? res += 90 : res += 450);
}
// 实际使用
var person = L.rotatedMarker(L.latLng(31.233, 121.465, 16), {
icon: L.icon({
iconUrl: './ic_navigation_white_24dp_2x.png',
// 图标下载:http://www.google.com/design/icons/
iconSize: [24, 24]
}),
draggable: true,
direction: 45
})
var angleDeg = ConvertPositionAngel(
{x: lastLatLng[0], y: lastLatLng[1]},
{x: latlng[0], y: latlng[1]}
)
person.options.angle = (angleDeg - 90) % 360
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment