@2gis/mapgl-clusterer
Advanced tools
Comparing version
@@ -16,2 +16,2 @@ !function(t,e){if("object"==typeof exports&&"object"==typeof module)module.exports=e();else if("function"==typeof define&&define.amd)define([],e);else{var o=e();for(var n in o)("object"==typeof exports?exports:t)[n]=o[n]}}(window,(function(){return function(t){var e={};function o(n){if(e[n])return e[n].exports;var r=e[n]={i:n,l:!1,exports:{}};return t[n].call(r.exports,r,r.exports,o),r.l=!0,r.exports}return o.m=t,o.c=e,o.d=function(t,e,n){o.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:n})},o.r=function(t){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(t,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(t,"__esModule",{value:!0})},o.t=function(t,e){if(1&e&&(t=o(t)),8&e)return t;if(4&e&&"object"==typeof t&&t&&t.__esModule)return t;var n=Object.create(null);if(o.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var r in t)o.d(n,r,function(e){return t[e]}.bind(null,r));return n},o.n=function(t){var e=t&&t.__esModule?function(){return t.default}:function(){return t};return o.d(e,"a",e),e},o.o=function(t,e){return Object.prototype.hasOwnProperty.call(t,e)},o.p="/",o(o.s=0)}([function(t,e,o){"use strict";o.r(e),o.d(e,"Clusterer",(function(){return j})); | ||
***************************************************************************** */ | ||
var n=function(t,e){return(n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var o in e)e.hasOwnProperty(o)&&(t[o]=e[o])})(t,e)};var r=function(){return(r=Object.assign||function(t){for(var e,o=1,n=arguments.length;o<n;o++)for(var r in e=arguments[o])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t}).apply(this,arguments)};function i(t,e,o,n,r,a){if(r-n<=o)return;const u=n+r>>1;!function t(e,o,n,r,i,a){for(;i>r;){if(i-r>600){const s=i-r+1,u=n-r+1,c=Math.log(s),p=.5*Math.exp(2*c/3),l=.5*Math.sqrt(c*p*(s-p)/s)*(u-s/2<0?-1:1),h=Math.max(r,Math.floor(n-u*p/s+l)),f=Math.min(i,Math.floor(n+(s-u)*p/s+l));t(e,o,n,h,f,a)}const u=o[2*n+a];let c=r,p=i;for(s(e,o,r,n),o[2*i+a]>u&&s(e,o,r,i);c<p;){for(s(e,o,c,p),c++,p--;o[2*c+a]<u;)c++;for(;o[2*p+a]>u;)p--}o[2*r+a]===u?s(e,o,r,p):(p++,s(e,o,p,i)),p<=n&&(r=p+1),n<=p&&(i=p-1)}}(t,e,u,n,r,a%2),i(t,e,o,n,u-1,a+1),i(t,e,o,u+1,r,a+1)}function s(t,e,o,n){a(t,o,n),a(e,2*o,2*n),a(e,2*o+1,2*n+1)}function a(t,e,o){const n=t[e];t[e]=t[o],t[o]=n}function u(t,e,o,n){const r=t-o,i=e-n;return r*r+i*i}const c=t=>t[0],p=t=>t[1];class l{constructor(t,e=c,o=p,n=64,r=Float64Array){this.nodeSize=n,this.points=t;const s=t.length<65536?Uint16Array:Uint32Array,a=this.ids=new s(t.length),u=this.coords=new r(2*t.length);for(let n=0;n<t.length;n++)a[n]=n,u[2*n]=e(t[n]),u[2*n+1]=o(t[n]);i(a,u,n,0,a.length-1,0)}range(t,e,o,n){return function(t,e,o,n,r,i,s){const a=[0,t.length-1,0],u=[];let c,p;for(;a.length;){const l=a.pop(),h=a.pop(),f=a.pop();if(h-f<=s){for(let s=f;s<=h;s++)c=e[2*s],p=e[2*s+1],c>=o&&c<=r&&p>=n&&p<=i&&u.push(t[s]);continue}const d=Math.floor((f+h)/2);c=e[2*d],p=e[2*d+1],c>=o&&c<=r&&p>=n&&p<=i&&u.push(t[d]);const m=(l+1)%2;(0===l?o<=c:n<=p)&&(a.push(f),a.push(d-1),a.push(m)),(0===l?r>=c:i>=p)&&(a.push(d+1),a.push(h),a.push(m))}return u}(this.ids,this.coords,t,e,o,n,this.nodeSize)}within(t,e,o){return function(t,e,o,n,r,i){const s=[0,t.length-1,0],a=[],c=r*r;for(;s.length;){const p=s.pop(),l=s.pop(),h=s.pop();if(l-h<=i){for(let r=h;r<=l;r++)u(e[2*r],e[2*r+1],o,n)<=c&&a.push(t[r]);continue}const f=Math.floor((h+l)/2),d=e[2*f],m=e[2*f+1];u(d,m,o,n)<=c&&a.push(t[f]);const g=(p+1)%2;(0===p?o-r<=d:n-r<=m)&&(s.push(h),s.push(f-1),s.push(g)),(0===p?o+r>=d:n+r>=m)&&(s.push(f+1),s.push(l),s.push(g))}return a}(this.ids,this.coords,t,e,o,this.nodeSize)}}const h={minZoom:0,maxZoom:16,radius:40,extent:512,nodeSize:64,log:!1,generateId:!1,reduce:null,map:t=>t};class f{constructor(t){this.options=_(Object.create(h),t),this.trees=new Array(this.options.maxZoom+1)}load(t){const{log:e,minZoom:o,maxZoom:n,nodeSize:r}=this.options;e&&console.time("total time");const i=`prepare ${t.length} points`;e&&console.time(i),this.points=t;let s=[];for(let e=0;e<t.length;e++)t[e].geometry&&s.push(m(t[e],e));this.trees[n+1]=new l(s,M,w,r,Float32Array),e&&console.timeEnd(i);for(let t=n;t>=o;t--){const o=+Date.now();s=this._cluster(s,t),this.trees[t]=new l(s,M,w,r,Float32Array),e&&console.log("z%d: %d clusters in %dms",t,s.length,+Date.now()-o)}return e&&console.timeEnd("total time"),this}getClusters(t,e){let o=((t[0]+180)%360+360)%360-180;const n=Math.max(-90,Math.min(90,t[1]));let r=180===t[2]?180:((t[2]+180)%360+360)%360-180;const i=Math.max(-90,Math.min(90,t[3]));if(t[2]-t[0]>=360)o=-180,r=180;else if(o>r){const t=this.getClusters([o,n,180,i],e),s=this.getClusters([-180,n,r,i],e);return t.concat(s)}const s=this.trees[this._limitZoom(e)],a=s.range(v(o),b(i),v(r),b(n)),u=[];for(const t of a){const e=s.points[t];u.push(e.numPoints?g(e):this.points[e.index])}return u}getChildren(t){const e=this._getOriginId(t),o=this._getOriginZoom(t),n="No cluster with the specified id.",r=this.trees[o];if(!r)throw new Error(n);const i=r.points[e];if(!i)throw new Error(n);const s=this.options.radius/(this.options.extent*Math.pow(2,o-1)),a=r.within(i.x,i.y,s),u=[];for(const e of a){const o=r.points[e];o.parentId===t&&u.push(o.numPoints?g(o):this.points[o.index])}if(0===u.length)throw new Error(n);return u}getLeaves(t,e,o){e=e||10,o=o||0;const n=[];return this._appendLeaves(n,t,e,o,0),n}getTile(t,e,o){const n=this.trees[this._limitZoom(t)],r=Math.pow(2,t),{extent:i,radius:s}=this.options,a=s/i,u=(o-a)/r,c=(o+1+a)/r,p={features:[]};return this._addTileFeatures(n.range((e-a)/r,u,(e+1+a)/r,c),n.points,e,o,r,p),0===e&&this._addTileFeatures(n.range(1-a/r,u,1,c),n.points,r,o,r,p),e===r-1&&this._addTileFeatures(n.range(0,u,a/r,c),n.points,-1,o,r,p),p.features.length?p:null}getClusterExpansionZoom(t){let e=this._getOriginZoom(t)-1;for(;e<=this.options.maxZoom;){const o=this.getChildren(t);if(e++,1!==o.length)break;t=o[0].properties.cluster_id}return e}_appendLeaves(t,e,o,n,r){const i=this.getChildren(e);for(const e of i){const i=e.properties;if(i&&i.cluster?r+i.point_count<=n?r+=i.point_count:r=this._appendLeaves(t,i.cluster_id,o,n,r):r<n?r++:t.push(e),t.length===o)break}return r}_addTileFeatures(t,e,o,n,r,i){for(const s of t){const t=e[s],a=t.numPoints,u={type:1,geometry:[[Math.round(this.options.extent*(t.x*r-o)),Math.round(this.options.extent*(t.y*r-n))]],tags:a?y(t):this.points[t.index].properties};let c;a?c=t.id:this.options.generateId?c=t.index:this.points[t.index].id&&(c=this.points[t.index].id),void 0!==c&&(u.id=c),i.features.push(u)}}_limitZoom(t){return Math.max(this.options.minZoom,Math.min(t,this.options.maxZoom+1))}_cluster(t,e){const o=[],{radius:n,extent:r,reduce:i}=this.options,s=n/(r*Math.pow(2,e));for(let n=0;n<t.length;n++){const r=t[n];if(r.zoom<=e)continue;r.zoom=e;const a=this.trees[e+1],u=a.within(r.x,r.y,s);let c=r.numPoints||1,p=r.x*c,l=r.y*c,h=i&&c>1?this._map(r,!0):null;const f=(n<<5)+(e+1)+this.points.length;for(const t of u){const o=a.points[t];if(o.zoom<=e)continue;o.zoom=e;const n=o.numPoints||1;p+=o.x*n,l+=o.y*n,c+=n,o.parentId=f,i&&(h||(h=this._map(r,!0)),i(h,this._map(o)))}1===c?o.push(r):(r.parentId=f,o.push(d(p/c,l/c,f,c,h)))}return o}_getOriginId(t){return t-this.points.length>>5}_getOriginZoom(t){return(t-this.points.length)%32}_map(t,e){if(t.numPoints)return e?_({},t.properties):t.properties;const o=this.points[t.index].properties,n=this.options.map(o);return e&&n===o?_({},n):n}}function d(t,e,o,n,r){return{x:t,y:e,zoom:1/0,id:o,parentId:-1,numPoints:n,properties:r}}function m(t,e){const[o,n]=t.geometry.coordinates;return{x:v(o),y:b(n),zoom:1/0,index:e,parentId:-1}}function g(t){return{type:"Feature",id:t.id,properties:y(t),geometry:{type:"Point",coordinates:[(e=t.x,360*(e-.5)),x(t.y)]}};var e}function y(t){const e=t.numPoints,o=e>=1e4?`${Math.round(e/1e3)}k`:e>=1e3?`${Math.round(e/100)/10}k`:e;return _(_({},t.properties),{cluster:!0,cluster_id:t.id,point_count:e,point_count_abbreviated:o})}function v(t){return t/360+.5}function b(t){const e=Math.sin(t*Math.PI/180),o=.5-.25*Math.log((1+e)/(1-e))/Math.PI;return o<0?0:o>1?1:o}function x(t){const e=(180-360*t)*Math.PI/180;return 360*Math.atan(Math.exp(e))/Math.PI-90}function _(t,e){for(const o in e)t[o]=e[o];return t}function M(t){return t.x}function w(t){return t.y}var S=function(){function t(){this.events={}}return t.prototype.on=function(t,e){var o=this.events[t];return o||(o=this.events[t]=[]),o.push(e),this},t.prototype.once=function(t,e){var o=this,n=function(r){o.off(t,n),e.call(o,r)};return this.on(t,n),this},t.prototype.off=function(t,e){var o=this.events[t];if(!o)return this;var n=o.indexOf(e);return-1!==n&&o.splice(n,1),this},t.prototype.emit=function(t,e){var o=this.events[t];if(!o)return this;for(var n=o.slice(),r=0;r<n.length;r++)n[r].call(this,e);return this},t}(),I=["click","dblclick","mousemove","mouseover","mouseout","mousedown","mouseup","touchstart","touchend"];var j=function(t){function e(o,n){var i;void 0===n&&(n={});var s=t.call(this)||this;s._updateVisibleObjects=function(){s.markers.forEach((function(t){t.hide()})),s.clusters.forEach((function(t){var e=t.marker,o=t.label;e.hide(),o.hide()})),s._getVisibleObjects().forEach((function(t){if(function(t){return t.properties.cluster}(t)){var e=t.properties.cluster_id,o=s.clusters.get(e);o?(o.marker.show(),o.label.show()):s._createCluster(e,t.geometry.coordinates,t.properties.point_count)}else{var n=t.properties.index,r=s.markers.get(n);r?r.show():s._createMarker(n,t.geometry.coordinates)}}))};var a=null!==(i=n.radius)&&void 0!==i?i:e.options.radius,u=e.options.clusterStyle;return n.clusterStyle&&(u="function"==typeof n.clusterStyle?n.clusterStyle:r(r({},u),n.clusterStyle)),s.options={radius:a,clusterStyle:u},s.map=o,s.inputMarkers=[],s.supercluster=new f({minZoom:0,maxZoom:20,radius:s.options.radius}),s.markers=new Map,s.clusters=new Map,s.map.on("move",s._updateVisibleObjects),s.map.on("resize",s._updateVisibleObjects),s}return function(t,e){function o(){this.constructor=t}n(t,e),t.prototype=null===e?Object.create(e):(o.prototype=e.prototype,new o)}(e,t),e.prototype.load=function(t){this._clear(),this.inputMarkers=t,this.supercluster.load(function(t){return t.map((function(t,e){return{type:"Feature",geometry:{type:"Point",coordinates:t.coordinates},properties:{index:e,cluster:!1}}}))}(t)),this._updateVisibleObjects()},e.prototype.destroy=function(){this._clear(),this.map.off("move",this._updateVisibleObjects),this.map.off("resize",this._updateVisibleObjects)},e.prototype._clear=function(){this.markers.forEach((function(t){t.destroy()})),this.clusters.forEach((function(t){var e=t.marker,o=t.label;e.destroy(),o.destroy()})),this.markers.clear(),this.clusters.clear(),this.supercluster.load([]),this.inputMarkers=[]},e.prototype._getVisibleObjects=function(){var t=Math.floor(this.map.getZoom()),e=this._getBounds();return this.supercluster.getClusters(e,t)},e.prototype._getBounds=function(){var t=this.map.getSize(),e=.06*t[0],o=.06*t[1],n=this.map.unproject([-e,t[1]+o]),r=this.map.unproject([t[0]+e,t[1]+o]),i=this.map.unproject([t[0]+e,-o]),s=this.map.unproject([-e,-o]);return[Math.min(n[0],r[0],i[0],s[0]),Math.min(n[1],r[1],i[1],s[1]),Math.max(n[0],r[0],i[0],s[0]),Math.max(n[1],r[1],i[1],s[1])]},e.prototype._createMarker=function(t,e){var o=this,n=this.inputMarkers[t],r=n.icon,i=n.size,s=n.anchor,a=n.hoverIcon,u=n.hoverSize,c=n.hoverAnchor,p=new mapgl.Marker(this.map,{coordinates:e,icon:r,size:i,anchor:s,hoverIcon:a,hoverSize:u,hoverAnchor:c});I.forEach((function(t){return p.on(t,(function(e){var r={originalEvent:e.originalEvent,lngLat:e.lngLat,point:e.point,target:{type:"marker",data:n}};o.emit(t,r)}))})),this.markers.set(t,p)},e.prototype._createCluster=function(t,o,n){var i=this,s="function"==typeof this.options.clusterStyle?r(r({},e.options.clusterStyle),this.options.clusterStyle(n)):this.options.clusterStyle;void 0===s.labelText&&(s.labelText=String(n));var a=new mapgl.Marker(this.map,{coordinates:o,zIndex:0,icon:s.icon,size:s.size,anchor:s.anchor,hoverIcon:s.hoverIcon,hoverSize:s.hoverSize,hoverAnchor:s.hoverAnchor}),u=this.supercluster.getLeaves(t,1/0).map((function(t){return i.inputMarkers[t.properties.index]}));I.forEach((function(t){return a.on(t,(function(e){var o={originalEvent:e.originalEvent,lngLat:e.lngLat,point:e.point,target:{type:"cluster",data:u}};i.emit(t,o)}))}));var c=new mapgl.Label(this.map,{coordinates:o,text:s.labelText,color:s.labelColor,fontSize:s.labelFontSize,haloRadius:s.labelHaloRadius,haloColor:s.labelHaloColor,letterSpacing:s.labelLetterSpacing,anchor:s.labelAnchor});this.clusters.set(t,{marker:a,label:c})},e.options={radius:80,clusterStyle:{icon:"data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIzMCIgaGVpZ2h0PSIzMCIgdmlld0JveD0iMCAwIDMwIDMwIj48Y2lyY2xlIGZpbGw9IiM0OGQ5NWQiIGN4PSIxNSIgY3k9IjE1IiByPSIxNSIvPjwvc3ZnPg==",labelColor:"#000000",labelFontSize:14,labelText:void 0}},e}(S);"mapgl"in window?mapgl.Clusterer=j:console.error("Looks like you did not load MapGL API correctly")}])})); | ||
var n=function(t,e){return(n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(t,e){t.__proto__=e}||function(t,e){for(var o in e)e.hasOwnProperty(o)&&(t[o]=e[o])})(t,e)};var r=function(){return(r=Object.assign||function(t){for(var e,o=1,n=arguments.length;o<n;o++)for(var r in e=arguments[o])Object.prototype.hasOwnProperty.call(e,r)&&(t[r]=e[r]);return t}).apply(this,arguments)};function i(t,e,o,n,r,a){if(r-n<=o)return;const u=n+r>>1;!function t(e,o,n,r,i,a){for(;i>r;){if(i-r>600){const s=i-r+1,u=n-r+1,c=Math.log(s),p=.5*Math.exp(2*c/3),l=.5*Math.sqrt(c*p*(s-p)/s)*(u-s/2<0?-1:1),h=Math.max(r,Math.floor(n-u*p/s+l)),f=Math.min(i,Math.floor(n+(s-u)*p/s+l));t(e,o,n,h,f,a)}const u=o[2*n+a];let c=r,p=i;for(s(e,o,r,n),o[2*i+a]>u&&s(e,o,r,i);c<p;){for(s(e,o,c,p),c++,p--;o[2*c+a]<u;)c++;for(;o[2*p+a]>u;)p--}o[2*r+a]===u?s(e,o,r,p):(p++,s(e,o,p,i)),p<=n&&(r=p+1),n<=p&&(i=p-1)}}(t,e,u,n,r,a%2),i(t,e,o,n,u-1,a+1),i(t,e,o,u+1,r,a+1)}function s(t,e,o,n){a(t,o,n),a(e,2*o,2*n),a(e,2*o+1,2*n+1)}function a(t,e,o){const n=t[e];t[e]=t[o],t[o]=n}function u(t,e,o,n){const r=t-o,i=e-n;return r*r+i*i}const c=t=>t[0],p=t=>t[1];class l{constructor(t,e=c,o=p,n=64,r=Float64Array){this.nodeSize=n,this.points=t;const s=t.length<65536?Uint16Array:Uint32Array,a=this.ids=new s(t.length),u=this.coords=new r(2*t.length);for(let n=0;n<t.length;n++)a[n]=n,u[2*n]=e(t[n]),u[2*n+1]=o(t[n]);i(a,u,n,0,a.length-1,0)}range(t,e,o,n){return function(t,e,o,n,r,i,s){const a=[0,t.length-1,0],u=[];let c,p;for(;a.length;){const l=a.pop(),h=a.pop(),f=a.pop();if(h-f<=s){for(let s=f;s<=h;s++)c=e[2*s],p=e[2*s+1],c>=o&&c<=r&&p>=n&&p<=i&&u.push(t[s]);continue}const d=Math.floor((f+h)/2);c=e[2*d],p=e[2*d+1],c>=o&&c<=r&&p>=n&&p<=i&&u.push(t[d]);const m=(l+1)%2;(0===l?o<=c:n<=p)&&(a.push(f),a.push(d-1),a.push(m)),(0===l?r>=c:i>=p)&&(a.push(d+1),a.push(h),a.push(m))}return u}(this.ids,this.coords,t,e,o,n,this.nodeSize)}within(t,e,o){return function(t,e,o,n,r,i){const s=[0,t.length-1,0],a=[],c=r*r;for(;s.length;){const p=s.pop(),l=s.pop(),h=s.pop();if(l-h<=i){for(let r=h;r<=l;r++)u(e[2*r],e[2*r+1],o,n)<=c&&a.push(t[r]);continue}const f=Math.floor((h+l)/2),d=e[2*f],m=e[2*f+1];u(d,m,o,n)<=c&&a.push(t[f]);const g=(p+1)%2;(0===p?o-r<=d:n-r<=m)&&(s.push(h),s.push(f-1),s.push(g)),(0===p?o+r>=d:n+r>=m)&&(s.push(f+1),s.push(l),s.push(g))}return a}(this.ids,this.coords,t,e,o,this.nodeSize)}}const h={minZoom:0,maxZoom:16,radius:40,extent:512,nodeSize:64,log:!1,generateId:!1,reduce:null,map:t=>t};class f{constructor(t){this.options=x(Object.create(h),t),this.trees=new Array(this.options.maxZoom+1)}load(t){const{log:e,minZoom:o,maxZoom:n,nodeSize:r}=this.options;e&&console.time("total time");const i=`prepare ${t.length} points`;e&&console.time(i),this.points=t;let s=[];for(let e=0;e<t.length;e++)t[e].geometry&&s.push(m(t[e],e));this.trees[n+1]=new l(s,M,w,r,Float32Array),e&&console.timeEnd(i);for(let t=n;t>=o;t--){const o=+Date.now();s=this._cluster(s,t),this.trees[t]=new l(s,M,w,r,Float32Array),e&&console.log("z%d: %d clusters in %dms",t,s.length,+Date.now()-o)}return e&&console.timeEnd("total time"),this}getClusters(t,e){let o=((t[0]+180)%360+360)%360-180;const n=Math.max(-90,Math.min(90,t[1]));let r=180===t[2]?180:((t[2]+180)%360+360)%360-180;const i=Math.max(-90,Math.min(90,t[3]));if(t[2]-t[0]>=360)o=-180,r=180;else if(o>r){const t=this.getClusters([o,n,180,i],e),s=this.getClusters([-180,n,r,i],e);return t.concat(s)}const s=this.trees[this._limitZoom(e)],a=s.range(v(o),b(i),v(r),b(n)),u=[];for(const t of a){const e=s.points[t];u.push(e.numPoints?g(e):this.points[e.index])}return u}getChildren(t){const e=this._getOriginId(t),o=this._getOriginZoom(t),n="No cluster with the specified id.",r=this.trees[o];if(!r)throw new Error(n);const i=r.points[e];if(!i)throw new Error(n);const s=this.options.radius/(this.options.extent*Math.pow(2,o-1)),a=r.within(i.x,i.y,s),u=[];for(const e of a){const o=r.points[e];o.parentId===t&&u.push(o.numPoints?g(o):this.points[o.index])}if(0===u.length)throw new Error(n);return u}getLeaves(t,e,o){e=e||10,o=o||0;const n=[];return this._appendLeaves(n,t,e,o,0),n}getTile(t,e,o){const n=this.trees[this._limitZoom(t)],r=Math.pow(2,t),{extent:i,radius:s}=this.options,a=s/i,u=(o-a)/r,c=(o+1+a)/r,p={features:[]};return this._addTileFeatures(n.range((e-a)/r,u,(e+1+a)/r,c),n.points,e,o,r,p),0===e&&this._addTileFeatures(n.range(1-a/r,u,1,c),n.points,r,o,r,p),e===r-1&&this._addTileFeatures(n.range(0,u,a/r,c),n.points,-1,o,r,p),p.features.length?p:null}getClusterExpansionZoom(t){let e=this._getOriginZoom(t)-1;for(;e<=this.options.maxZoom;){const o=this.getChildren(t);if(e++,1!==o.length)break;t=o[0].properties.cluster_id}return e}_appendLeaves(t,e,o,n,r){const i=this.getChildren(e);for(const e of i){const i=e.properties;if(i&&i.cluster?r+i.point_count<=n?r+=i.point_count:r=this._appendLeaves(t,i.cluster_id,o,n,r):r<n?r++:t.push(e),t.length===o)break}return r}_addTileFeatures(t,e,o,n,r,i){for(const s of t){const t=e[s],a=t.numPoints,u={type:1,geometry:[[Math.round(this.options.extent*(t.x*r-o)),Math.round(this.options.extent*(t.y*r-n))]],tags:a?y(t):this.points[t.index].properties};let c;a?c=t.id:this.options.generateId?c=t.index:this.points[t.index].id&&(c=this.points[t.index].id),void 0!==c&&(u.id=c),i.features.push(u)}}_limitZoom(t){return Math.max(this.options.minZoom,Math.min(t,this.options.maxZoom+1))}_cluster(t,e){const o=[],{radius:n,extent:r,reduce:i}=this.options,s=n/(r*Math.pow(2,e));for(let n=0;n<t.length;n++){const r=t[n];if(r.zoom<=e)continue;r.zoom=e;const a=this.trees[e+1],u=a.within(r.x,r.y,s);let c=r.numPoints||1,p=r.x*c,l=r.y*c,h=i&&c>1?this._map(r,!0):null;const f=(n<<5)+(e+1)+this.points.length;for(const t of u){const o=a.points[t];if(o.zoom<=e)continue;o.zoom=e;const n=o.numPoints||1;p+=o.x*n,l+=o.y*n,c+=n,o.parentId=f,i&&(h||(h=this._map(r,!0)),i(h,this._map(o)))}1===c?o.push(r):(r.parentId=f,o.push(d(p/c,l/c,f,c,h)))}return o}_getOriginId(t){return t-this.points.length>>5}_getOriginZoom(t){return(t-this.points.length)%32}_map(t,e){if(t.numPoints)return e?x({},t.properties):t.properties;const o=this.points[t.index].properties,n=this.options.map(o);return e&&n===o?x({},n):n}}function d(t,e,o,n,r){return{x:t,y:e,zoom:1/0,id:o,parentId:-1,numPoints:n,properties:r}}function m(t,e){const[o,n]=t.geometry.coordinates;return{x:v(o),y:b(n),zoom:1/0,index:e,parentId:-1}}function g(t){return{type:"Feature",id:t.id,properties:y(t),geometry:{type:"Point",coordinates:[(e=t.x,360*(e-.5)),_(t.y)]}};var e}function y(t){const e=t.numPoints,o=e>=1e4?`${Math.round(e/1e3)}k`:e>=1e3?`${Math.round(e/100)/10}k`:e;return x(x({},t.properties),{cluster:!0,cluster_id:t.id,point_count:e,point_count_abbreviated:o})}function v(t){return t/360+.5}function b(t){const e=Math.sin(t*Math.PI/180),o=.5-.25*Math.log((1+e)/(1-e))/Math.PI;return o<0?0:o>1?1:o}function _(t){const e=(180-360*t)*Math.PI/180;return 360*Math.atan(Math.exp(e))/Math.PI-90}function x(t,e){for(const o in e)t[o]=e[o];return t}function M(t){return t.x}function w(t){return t.y}var S=function(){function t(){this.events={}}return t.prototype.on=function(t,e){var o=this.events[t];return o||(o=this.events[t]=[]),o.push(e),this},t.prototype.once=function(t,e){var o=this,n=function(r){o.off(t,n),e.call(o,r)};return this.on(t,n),this},t.prototype.off=function(t,e){var o=this.events[t];if(!o)return this;var n=o.indexOf(e);return-1!==n&&o.splice(n,1),this},t.prototype.emit=function(t,e){var o=this.events[t];if(!o)return this;for(var n=o.slice(),r=0;r<n.length;r++)n[r].call(this,e);return this},t}(),I=["click","dblclick","mousemove","mouseover","mouseout","mousedown","mouseup","touchstart","touchend"];var j=function(t){function e(o,n){var i;void 0===n&&(n={});var s=t.call(this)||this;s._updateVisibleObjects=function(){var t=s._getVisibleObjects();s.markers.forEach((function(t){t.hide()})),s.clusters.forEach((function(t){var e=t.marker,o=t.label;e.hide(),o.hide()})),t.forEach((function(t){if(function(t){return t.properties.cluster}(t)){var e=t.properties.cluster_id,o=s.clusters.get(e);o?(o.marker.show(),o.label.show()):s._createCluster(e,t.geometry.coordinates,t.properties.point_count)}else{var n=t.properties.index,r=s.markers.get(n);r?r.show():s._createMarker(n,t.geometry.coordinates)}}))},s._updateVisibleObjects=function(t,e){var o,n;function r(){o=!1,n&&(i.apply(void 0,n),n=!1)}function i(){for(var i=[],s=0;s<arguments.length;s++)i[s]=arguments[s];o?n=i:(t.apply(void 0,i),setTimeout(r,e),o=!0)}return i}(s._updateVisibleObjects,100);var a=null!==(i=n.radius)&&void 0!==i?i:e.options.radius,u=e.options.clusterStyle;return n.clusterStyle&&(u="function"==typeof n.clusterStyle?n.clusterStyle:r(r({},u),n.clusterStyle)),s.options={radius:a,clusterStyle:u},s.map=o,s.inputMarkers=[],s.supercluster=new f({minZoom:0,maxZoom:20,radius:s.options.radius}),s.markers=new Map,s.clusters=new Map,s.map.on("move",s._updateVisibleObjects),s.map.on("resize",s._updateVisibleObjects),s}return function(t,e){function o(){this.constructor=t}n(t,e),t.prototype=null===e?Object.create(e):(o.prototype=e.prototype,new o)}(e,t),e.prototype.load=function(t){this._clear(),this.inputMarkers=t,this.supercluster.load(function(t){return t.map((function(t,e){return{type:"Feature",geometry:{type:"Point",coordinates:t.coordinates},properties:{index:e,cluster:!1}}}))}(t)),this._updateVisibleObjects()},e.prototype.destroy=function(){this._clear(),this.map.off("move",this._updateVisibleObjects),this.map.off("resize",this._updateVisibleObjects)},e.prototype._clear=function(){this.markers.forEach((function(t){t.destroy()})),this.clusters.forEach((function(t){var e=t.marker,o=t.label;e.destroy(),o.destroy()})),this.markers.clear(),this.clusters.clear(),this.supercluster.load([]),this.inputMarkers=[]},e.prototype._getVisibleObjects=function(){var t=Math.floor(this.map.getZoom()),e=this._getBounds();return this.supercluster.getClusters(e,t)},e.prototype._getBounds=function(){var t=this.map.getSize(),e=.06*t[0],o=.06*t[1],n=this.map.unproject([-e,t[1]+o]),r=this.map.unproject([t[0]+e,t[1]+o]),i=this.map.unproject([t[0]+e,-o]),s=this.map.unproject([-e,-o]);return[Math.min(n[0],r[0],i[0],s[0]),Math.min(n[1],r[1],i[1],s[1]),Math.max(n[0],r[0],i[0],s[0]),Math.max(n[1],r[1],i[1],s[1])]},e.prototype._createMarker=function(t,e){var o=this,n=this.inputMarkers[t],r=n.icon,i=n.size,s=n.anchor,a=n.hoverIcon,u=n.hoverSize,c=n.hoverAnchor,p=new mapgl.Marker(this.map,{coordinates:e,icon:r,size:i,anchor:s,hoverIcon:a,hoverSize:u,hoverAnchor:c});I.forEach((function(t){return p.on(t,(function(e){var r={originalEvent:e.originalEvent,lngLat:e.lngLat,point:e.point,target:{type:"marker",data:n}};o.emit(t,r)}))})),this.markers.set(t,p)},e.prototype._createCluster=function(t,o,n){var i=this,s="function"==typeof this.options.clusterStyle?r(r({},e.options.clusterStyle),this.options.clusterStyle(n)):this.options.clusterStyle;void 0===s.labelText&&(s.labelText=String(n));var a=new mapgl.Marker(this.map,{coordinates:o,zIndex:0,icon:s.icon,size:s.size,anchor:s.anchor,hoverIcon:s.hoverIcon,hoverSize:s.hoverSize,hoverAnchor:s.hoverAnchor}),u=this.supercluster.getLeaves(t,1/0).map((function(t){return i.inputMarkers[t.properties.index]}));I.forEach((function(t){return a.on(t,(function(e){var o={originalEvent:e.originalEvent,lngLat:e.lngLat,point:e.point,target:{type:"cluster",data:u}};i.emit(t,o)}))}));var c=new mapgl.Label(this.map,{coordinates:o,text:s.labelText,color:s.labelColor,fontSize:s.labelFontSize,haloRadius:s.labelHaloRadius,haloColor:s.labelHaloColor,letterSpacing:s.labelLetterSpacing,anchor:s.labelAnchor});this.clusters.set(t,{marker:a,label:c})},e.options={radius:80,clusterStyle:{icon:"data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIzMCIgaGVpZ2h0PSIzMCIgdmlld0JveD0iMCAwIDMwIDMwIj48Y2lyY2xlIGZpbGw9IiM0OGQ5NWQiIGN4PSIxNSIgY3k9IjE1IiByPSIxNSIvPjwvc3ZnPg==",labelColor:"#000000",labelFontSize:14,labelText:void 0}},e}(S);"mapgl"in window?mapgl.Clusterer=j:console.error("Looks like you did not load MapGL API correctly")}])})); |
@@ -9,2 +9,3 @@ import { ClustererEventTable, InputMarker, ClustererOptions } from './types'; | ||
* @hidden | ||
* @internal | ||
*/ | ||
@@ -14,2 +15,3 @@ private static options; | ||
* @hidden | ||
* @internal | ||
*/ | ||
@@ -19,2 +21,3 @@ private map; | ||
* @hidden | ||
* @internal | ||
*/ | ||
@@ -24,2 +27,3 @@ private options; | ||
* @hidden | ||
* @internal | ||
*/ | ||
@@ -29,2 +33,3 @@ private supercluster; | ||
* @hidden | ||
* @internal | ||
* Массив маркеров, которые передал пользователь | ||
@@ -35,4 +40,5 @@ */ | ||
* @hidden | ||
* @internal | ||
* Хранилище для маркеров, которые уже были созданы, т.е. показаны на экране | ||
* Ключ маркера соответсвует индексу этого маркера в массиве, который передал пользователь | ||
* Ключ маркера соответствует индексу этого маркера в массиве, который передал пользователь | ||
*/ | ||
@@ -42,4 +48,5 @@ private markers; | ||
* @hidden | ||
* @internal | ||
* Хранилище для кластеров, которые уже были созданы, т.е. показаны на экране | ||
* Ключ кластер соответвует ID, которые ему присваевается в процессе работы supercluster | ||
* Ключ кластер соответствует ID, которые ему присваивается в процессе работы supercluster | ||
*/ | ||
@@ -46,0 +53,0 @@ private clusters; |
@@ -106,2 +106,3 @@ import Supercluster from 'supercluster'; | ||
* @hidden | ||
* @internal | ||
*/ | ||
@@ -114,2 +115,3 @@ export interface Cluster { | ||
* @hidden | ||
* @internal | ||
*/ | ||
@@ -122,2 +124,3 @@ export interface MarkerGeoJsonProperties { | ||
* @hidden | ||
* @internal | ||
*/ | ||
@@ -127,2 +130,3 @@ export declare type PointFeature = Supercluster.PointFeature<MarkerGeoJsonProperties>; | ||
* @hidden | ||
* @internal | ||
*/ | ||
@@ -129,0 +133,0 @@ export declare type ClusterFeature = Supercluster.ClusterFeature<Supercluster.AnyProps>; |
import { InputMarker, PointFeature, ClusterFeature, ClustererEventTable } from './types'; | ||
/** | ||
* @hidden | ||
* @internal | ||
*/ | ||
@@ -8,2 +9,3 @@ export declare const defaultClusterIcon = "data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIzMCIgaGVpZ2h0PSIzMCIgdmlld0JveD0iMCAwIDMwIDMwIj48Y2lyY2xlIGZpbGw9IiM0OGQ5NWQiIGN4PSIxNSIgY3k9IjE1IiByPSIxNSIvPjwvc3ZnPg=="; | ||
* @hidden | ||
* @internal | ||
*/ | ||
@@ -13,2 +15,3 @@ export declare const markerEventNames: Array<keyof ClustererEventTable>; | ||
* @hidden | ||
* @internal | ||
*/ | ||
@@ -18,3 +21,11 @@ export declare function prepareSuperclusterInput(input: InputMarker[]): PointFeature[]; | ||
* @hidden | ||
* @internal | ||
*/ | ||
export declare function isCluster(object: PointFeature | ClusterFeature): object is ClusterFeature; | ||
/** | ||
* @hidden | ||
* @internal | ||
* Эта ф-я скопирована из джакарты полностью. | ||
* TODO: Нужно добавить ф-ю в mapgl.utils и брать ее оттуда, когда вообще эти утилиты появятся. | ||
*/ | ||
export declare function throttle(fn: (...args: any[]) => void, time: number): (...args: any[]) => void; |
{ | ||
"name": "@2gis/mapgl-clusterer", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Marker clustering plugin for Mapgl", | ||
@@ -5,0 +5,0 @@ "main": "dist/clustering.js", |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
64628
1.35%382
6.11%0
-100%