leaflet-simplestyle
Version:
A small module for incorporating incorporating simple style on geojson layers in leafletjs
2 lines (1 loc) • 2.49 kB
JavaScript
!function(e){"function"==typeof define&&define.amd?define(e):e()}((function(){"use strict";const e={stroke:"#555555","stroke-width":2,"stroke-opacity":1,fill:"#555555","fill-opacity":.5},o=[["stroke","color"],["stroke-width","weight"],["stroke-opacity","opacity"],["fill","fillColor"],["fill-opacity","fillOpacity"]];function t(t){return function(e){const t={};for(let i=0;i<o.length;i++)t[o[i][1]]=e[o[i][0]];return t}(function(e,o){const t={};for(const i in o)void 0===e[i]?t[i]=o[i]:t[i]=e[i];return t}(t.properties||{},e))}L.GeoJSON.include({options:{useSimpleStyle:!1,useMakiMarkers:!1},_useSimpleStyle:function(){this.options.useSimpleStyle&&this.useSimpleStyle()},toggleMakiMarkers:function(){this.options.useMakiMarkers=!this.options.useMakiMarkers,this._useSimpleStyle()},useSimpleStyle:function(){this.options.useSimpleStyle=!0;const e=this;this.eachLayer((function(o){"icon"in o.options&&o.setIcon(function(e,o){const t=(e=e||{})["marker-size"]||"medium",i="marker-symbol"in e&&""!==e["marker-symbol"]?`-${e["marker-symbol"]}`:"",n=(e["marker-color"]||"7e7e7e").replace("#",""),s={small:[23,23],medium:[30,30],large:[37,37]},r={iconUrl:encodeURI(`data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' width='15' height='15'><path d='M7.5 0C5.1 0 2.2 1.5 2.2 5.3c0 2.5 4 8.2 5.3 9.7 1-1.5 5.3-7 5.3-9.7C12.8 1.5 9.9 0 7.5 0z' fill='#${n}'/></svg>`).replace("#","%23"),iconSize:s[t],shadowUrl:"data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACMAAABaBAMAAADA2vJjAAAAGFBMVEUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAABWNxwqAAAACHRSTlMACRcjKzJAOtxk//MAAABzSURBVDjL7ZDRDYAwCER1A3EDcQNxA3EDGzeoE2jX91JNTYoLaPr+eDkIUBUK/6TOarpongC1HCFKhrkXwNxRMiKqOslwO3TJODugcHEecT/Oa/BbgOMOqkZI3eHBvkyIvSrbaMfbJeyq9iB7dv6cQuFrnJu2IxWE6etQAAAAAElFTkSuQmCC",shadowSize:s[t],shadowAnchor:[s[t][0]/2,s[t][1]/2],iconAnchor:[s[t][0]/2,s[t][1]],popupAnchor:[0,-s[t][1]/2]};if(o){const e={small:[20,50],medium:[30,70],large:[35,90]};let o=window.document.location.protocol;-1===o.indexOf("http")&&(o="https:"),r.iconUrl=`${o}//a.tiles.mapbox.com/v3/marker/pin-${t.charAt(0)}${i.toLowerCase()}+${n}${L.Browser.retina?"@2x":""}.png`,r.iconSize=e[t],r.iconAnchor=[e[t][0]/2,e[t][1]/2],r.popupAnchor=[0,-e[t][1]/2]}return L.icon(r)}(o.feature.properties,e.options.useMakiMarkers))})),this.setStyle(t)},discardSimpleStyle:function(){this.options.useSimpleStyle=!1,this.eachLayer((function(e){void 0!==e.options.icon&&e.setIcon(L.Icon.Default.prototype)})),this.resetStyle()}}),L.GeoJSON.addInitHook("_useSimpleStyle")}));