-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathleaflet-canvas-markers.js
73 lines (68 loc) · 2.56 KB
/
leaflet-canvas-markers.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
'use strict';
(function (root, factory) {
if (typeof define === 'function' && define.amd) {
define(['leaflet'], factory);
} else if (typeof exports === 'object') {
module.exports = factory(require('leaflet'));
} else {
factory(window.L);
}
}(this, function (L) {
L.Canvas.include({
_updateImg(layer) {
const { img } = layer.options;
const p = layer._point.round();
p.x += img.offset.x; p.y += img.offset.y;
if (img.rotate) {
this._ctx.save();
this._ctx.translate(p.x, p.y);
this._ctx.rotate(img.rotate * Math.PI / 180);
this._ctx.drawImage(img.el, -img.size[0] / 2, -img.size[1] / 2, img.size[0], img.size[1]);
this._ctx.restore();
} else {
this._ctx.drawImage(img.el, p.x - img.size[0] / 2, p.y - img.size[1] / 2, img.size[0], img.size[1]);
}
},
});
const angleCrds = (map, prevLatlng, latlng) => {
if (!latlng || !prevLatlng) return 0;
const pxStart = map.project(prevLatlng);
const pxEnd = map.project(latlng);
return Math.atan2(pxStart.y - pxEnd.y, pxStart.x - pxEnd.x) / Math.PI * 180 - 90;
};
const defaultImgOptions = {
rotate: 0,
size: [40, 40],
offset: { x: 0, y: 0 },
};
const CanvasMarker = L.CircleMarker.extend({
_updatePath() {
if (!this.options.img || !this.options.img.url) return;
if (!this.options.img.el) {
this.options.img = {...defaultImgOptions, ...this.options.img};
this.options.img.rotate += angleCrds(this._map, this.options.prevLatlng, this._latlng);
const img = document.createElement('img');
img.src = this.options.img.url;
this.options.img.el = img;
img.onload = () => {
this.redraw();
};
img.onerror = () => {
this.options.img = null;
};
} else {
this._renderer._updateImg(this);
}
},
});
L.canvasMarker = function (...opt) {
try {
const i = opt.findIndex(o => typeof o === 'object' && o.img);
if (i+1) {
if (!opt[i].radius && opt[i].img && opt[i].img.size) opt[i].radius = Math.ceil(Math.max(...opt[i].img.size)/2);
if (opt[i].pane) delete opt[i].pane;
}
} catch(e) {}
return new CanvasMarker(...opt);
};
}));