forked from bbecquet/Leaflet.PolylineDecorator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
leaflet.polylineDecorator.min.js
13 lines (13 loc) · 5.88 KB
/
leaflet.polylineDecorator.min.js
1
2
3
4
5
6
7
8
9
10
11
12
13
L.GeometryUtil={computeAngle:function(a,c){return 180*Math.atan2(c.y-a.y,c.x-a.x)/Math.PI+90},getPointPathPixelLength:function(a){if(2>a.length)return 0;for(var c=0,d=a[0],e=1,g=a.length;e<g;e++)c+=d.distanceTo(d=a[e]);return c},getPixelLength:function(a,c){var d=a instanceof L.Polyline?a.getLatLngs():a;if(2>d.length)return 0;for(var e=0,g=c.latLngToLayerPoint(d[0]),f=1,h=d.length;f<h;f++)e+=g.distanceTo(g=c.latLngToLayerPoint(d[f]));return e},projectPatternOnPath:function(a,c,d,e){for(var g=[],f=
0,h=a.length;f<h;f++)g[f]=e.latLngToLayerPoint(a[f]);a=this.projectPatternOnPointPath(g,c,d);f=0;for(h=a.length;f<h;f++)a[f].latLng=e.layerPointToLatLng(a[f].pt);return a},projectPatternOnPointPath:function(a,c,d){var e=[],g=L.GeometryUtil.getPointPathPixelLength(a)*d;c=L.GeometryUtil.interpolateOnPointPath(a,c);e.push(c);if(0<d){a=a.slice(c.predecessor);a[0]=c.pt;for(d=L.GeometryUtil.getPointPathPixelLength(a,map);g<=d;)c=L.GeometryUtil.interpolateOnPointPath(a,g/d),e.push(c),a=a.slice(c.predecessor),
a[0]=c.pt,d=L.GeometryUtil.getPointPathPixelLength(a,map)}return e},interpolateOnPointPath:function(a,c){var d=a.length;if(2>d)return null;if(0>=c)return{pt:a[0],predecessor:0,heading:L.GeometryUtil.computeAngle(a[0],a[1])};if(1<=c)return{pt:a[d-1],predecessor:d-1,heading:L.GeometryUtil.computeAngle(a[d-2],a[d-1])};if(2==d)return{pt:L.GeometryUtil.interpolateBetweenPoints(a[0],a[1],c),predecessor:0,heading:L.GeometryUtil.computeAngle(a[0],a[1])};for(var e=L.GeometryUtil.getPointPathPixelLength(a),
g=b=a[0],f=ratioB=0,h=0,j=1;j<d&&ratioB<c;j++)g=b,f=ratioB,b=a[j],h+=g.distanceTo(b),ratioB=h/e;return{pt:L.GeometryUtil.interpolateBetweenPoints(g,b,(c-f)/(ratioB-f)),predecessor:j-2,heading:L.GeometryUtil.computeAngle(g,b)}},interpolateBetweenPoints:function(a,c,d){return c.x!=a.x?new L.Point(a.x*(1-d)+d*c.x,a.y*(1-d)+d*c.y):new L.Point(a.x,a.y+(c.y-a.y)*d)}};L.Symbol=L.Symbol||{};
L.Symbol.Dash=L.Class.extend({isZoomDependant:!0,options:{pixelSize:10,pathOptions:{}},initialize:function(a){L.Util.setOptions(this,a);this.options.pathOptions.clickable=!1},buildSymbol:function(a,c,d){c=this.options;if(1>=c.pixelSize)return new L.Polyline([a.latLng,a.latLng],c.pathOptions);var e=d.project(a.latLng);a=-(a.heading-90)*L.LatLng.DEG_TO_RAD;a=new L.Point(e.x+c.pixelSize*Math.cos(a+Math.PI)/2,e.y+c.pixelSize*Math.sin(a)/2);e=e.add(e.subtract(a));return new L.Polyline([d.unproject(a),d.unproject(e)],
c.pathOptions)}});
L.Symbol.ArrowHead=L.Class.extend({isZoomDependant:!0,options:{polygon:!0,pixelSize:10,headAngle:60,pathOptions:{stroke:!1,weight:2}},initialize:function(a){L.Util.setOptions(this,a);this.options.pathOptions.clickable=!1},buildSymbol:function(a,c,d){c=this.options;return c.polygon?new L.Polygon(this._buildArrowPath(a,d),c.pathOptions):new L.Polyline(this._buildArrowPath(a,d),c.pathOptions)},_buildArrowPath:function(a,c){var d=c.project(a.latLng),e=-(a.heading-90)*L.LatLng.DEG_TO_RAD,g=this.options.headAngle/
2*L.LatLng.DEG_TO_RAD,f=e+g,e=e-g,f=new L.Point(d.x-this.options.pixelSize*Math.cos(f),d.y+this.options.pixelSize*Math.sin(f)),d=new L.Point(d.x-this.options.pixelSize*Math.cos(e),d.y+this.options.pixelSize*Math.sin(e));return[c.unproject(f),a.latLng,c.unproject(d)]}});
L.Symbol.Marker=L.Class.extend({isZoomDependant:!1,options:{markerOptions:{}},initialize:function(a){L.Util.setOptions(this,a);this.options.markerOptions.clickable=!1;this.options.markerOptions.draggable=!1},buildSymbol:function(a){return new L.Marker(a.latLng,this.options.markerOptions)}});L.PolylineDecorator=L.LayerGroup.extend({options:{patterns:[]},initialize:function(a,c){L.LayerGroup.prototype.initialize.call(this);L.Util.setOptions(this,c);this._polyline=a;this._directionPointCache=[];this._initPatterns()},_initPatterns:function(){this._directionPointCache=[];this._isZoomDependant=!1;this._patterns=[];for(var a,c=0;c<this.options.patterns.length;c++)a=this._parsePatternDef(this.options.patterns[c]),this._patterns.push(a),this._isZoomDependant=this._isZoomDependant||a.isOffsetInPixels||
a.isRepeatInPixels||a.symbolFactory.isZoomDependant},setPatterns:function(a){this.options.patterns=a;this._initPatterns();this._softRedraw()},_parsePatternDef:function(a){var c={cache:[],symbolFactory:a.symbol,isOffsetInPixels:!1,isRepeatInPixels:!1};"string"===typeof a.offset&&-1!=a.offset.indexOf("%")?c.offset=parseFloat(a.offset)/100:(c.offset=parseFloat(a.offset),c.isOffsetInPixels=0<c.offset);"string"===typeof a.repeat&&-1!=a.repeat.indexOf("%")?c.repeat=parseFloat(a.repeat)/100:(c.repeat=parseFloat(a.repeat),
c.isRepeatInPixels=0<c.repeat);return c},onAdd:function(a){this._map=a;this._draw();if(this._isZoomDependant)this._map.on("zoomend",this._softRedraw,this)},onRemove:function(a){this._map.off("zoomend",this._softRedraw,this);L.LayerGroup.prototype.onRemove.call(this,a)},_buildSymbols:function(a,c){for(var d=[],e=0,g=c.length;e<g;e++)d.push(a.buildSymbol(c[e],this._latLngs,this._map,e,g));return d},_getDirectionPoints:function(a){var c=a.cache[this._map.getZoom()];if("undefined"!=typeof c)return c;
this._latLngs=this._polyline instanceof L.Polyline?this._polyline.getLatLngs():this._polyline;if(2>this._latLngs.length)return[];var d;d=null;a.isOffsetInPixels?(d=L.GeometryUtil.getPixelLength(this._latLngs,this._map),c=a.offset/d):c=a.offset;a.isRepeatInPixels?(d=null!=d?d:L.GeometryUtil.getPixelLength(this._latLngs,this._map),d=a.repeat/d):d=a.repeat;c=L.GeometryUtil.projectPatternOnPath(this._latLngs,c,d,this._map);return a.cache[this._map.getZoom()]=c},redraw:function(){this._redraw(!0)},_softRedraw:function(){this._redraw(!1)},
_redraw:function(a){this.clearLayers();if(a)for(a=0;a<this._patterns.length;a++)this._patterns[a].cache=[];this._draw()},_drawPattern:function(a){var c=this._getDirectionPoints(a);a=this._buildSymbols(a.symbolFactory,c);for(c=0;c<a.length;c++)this.addLayer(a[c])},_draw:function(){for(var a=0;a<this._patterns.length;a++)this._drawPattern(this._patterns[a])}});L.polylineDecorator=function(a,c){return new L.PolylineDecorator(a,c)};