@2gis/mapgl-clusterer
Advanced tools
Comparing version
@@ -1,2 +0,2 @@ | ||
!function(t){var e={};function o(r){if(e[r])return e[r].exports;var n=e[r]={i:r,l:!1,exports:{}};return t[r].call(n.exports,n,n.exports,o),n.l=!0,n.exports}o.m=t,o.c=e,o.d=function(t,e,r){o.o(t,e)||Object.defineProperty(t,e,{enumerable:!0,get:r})},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 r=Object.create(null);if(o.r(r),Object.defineProperty(r,"default",{enumerable:!0,value:t}),2&e&&"string"!=typeof t)for(var n in t)o.d(r,n,function(e){return t[e]}.bind(null,n));return r},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=2)}([function(t,e,o){"use strict";o.d(e,"b",(function(){return n})),o.d(e,"a",(function(){return i})),o.d(e,"c",(function(){return s})); | ||
!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}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=2)}([function(t,e,o){"use strict";o.d(e,"b",(function(){return r})),o.d(e,"a",(function(){return i})),o.d(e,"c",(function(){return s})); | ||
/*! ***************************************************************************** | ||
@@ -16,2 +16,2 @@ Copyright (c) Microsoft Corporation. All rights reserved. | ||
***************************************************************************** */ | ||
var r=function(t,e){return(r=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)};function n(t,e){function o(){this.constructor=t}r(t,e),t.prototype=null===e?Object.create(e):(o.prototype=e.prototype,new o)}var i=function(){return(i=Object.assign||function(t){for(var e,o=1,r=arguments.length;o<r;o++)for(var n in e=arguments[o])Object.prototype.hasOwnProperty.call(e,n)&&(t[n]=e[n]);return t}).apply(this,arguments)};function s(){for(var t=0,e=0,o=arguments.length;e<o;e++)t+=arguments[e].length;var r=Array(t),n=0;for(e=0;e<o;e++)for(var i=arguments[e],s=0,a=i.length;s<a;s++,n++)r[n]=i[s];return r}},,function(t,e,o){"use strict";o.r(e);var r=o(0);function n(t,e,o,r,s,a){if(s-r<=o)return;const u=r+s>>1;!function t(e,o,r,n,s,a){for(;s>n;){if(s-n>600){const i=s-n+1,u=r-n+1,c=Math.log(i),p=.5*Math.exp(2*c/3),h=.5*Math.sqrt(c*p*(i-p)/i)*(u-i/2<0?-1:1),l=Math.max(n,Math.floor(r-u*p/i+h)),d=Math.min(s,Math.floor(r+(i-u)*p/i+h));t(e,o,r,l,d,a)}const u=o[2*r+a];let c=n,p=s;for(i(e,o,n,r),o[2*s+a]>u&&i(e,o,n,s);c<p;){for(i(e,o,c,p),c++,p--;o[2*c+a]<u;)c++;for(;o[2*p+a]>u;)p--}o[2*n+a]===u?i(e,o,n,p):(p++,i(e,o,p,s)),p<=r&&(n=p+1),r<=p&&(s=p-1)}}(t,e,u,r,s,a%2),n(t,e,o,r,u-1,a+1),n(t,e,o,u+1,s,a+1)}function i(t,e,o,r){s(t,o,r),s(e,2*o,2*r),s(e,2*o+1,2*r+1)}function s(t,e,o){const r=t[e];t[e]=t[o],t[o]=r}function a(t,e,o,r){const n=t-o,i=e-r;return n*n+i*i}const u=t=>t[0],c=t=>t[1];class p{constructor(t,e=u,o=c,r=64,i=Float64Array){this.nodeSize=r,this.points=t;const s=t.length<65536?Uint16Array:Uint32Array,a=this.ids=new s(t.length),p=this.coords=new i(2*t.length);for(let r=0;r<t.length;r++)a[r]=r,p[2*r]=e(t[r]),p[2*r+1]=o(t[r]);n(a,p,r,0,a.length-1,0)}range(t,e,o,r){return function(t,e,o,r,n,i,s){const a=[0,t.length-1,0],u=[];let c,p;for(;a.length;){const h=a.pop(),l=a.pop(),d=a.pop();if(l-d<=s){for(let s=d;s<=l;s++)c=e[2*s],p=e[2*s+1],c>=o&&c<=n&&p>=r&&p<=i&&u.push(t[s]);continue}const f=Math.floor((d+l)/2);c=e[2*f],p=e[2*f+1],c>=o&&c<=n&&p>=r&&p<=i&&u.push(t[f]);const m=(h+1)%2;(0===h?o<=c:r<=p)&&(a.push(d),a.push(f-1),a.push(m)),(0===h?n>=c:i>=p)&&(a.push(f+1),a.push(l),a.push(m))}return u}(this.ids,this.coords,t,e,o,r,this.nodeSize)}within(t,e,o){return function(t,e,o,r,n,i){const s=[0,t.length-1,0],u=[],c=n*n;for(;s.length;){const p=s.pop(),h=s.pop(),l=s.pop();if(h-l<=i){for(let n=l;n<=h;n++)a(e[2*n],e[2*n+1],o,r)<=c&&u.push(t[n]);continue}const d=Math.floor((l+h)/2),f=e[2*d],m=e[2*d+1];a(f,m,o,r)<=c&&u.push(t[d]);const g=(p+1)%2;(0===p?o-n<=f:r-n<=m)&&(s.push(l),s.push(d-1),s.push(g)),(0===p?o+n>=f:r+n>=m)&&(s.push(d+1),s.push(h),s.push(g))}return u}(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 l{constructor(t){this.options=b(Object.create(h),t),this.trees=new Array(this.options.maxZoom+1)}load(t){const{log:e,minZoom:o,maxZoom:r,nodeSize:n}=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(f(t[e],e));this.trees[r+1]=new p(s,M,x,n,Float32Array),e&&console.timeEnd(i);for(let t=r;t>=o;t--){const o=+Date.now();s=this._cluster(s,t),this.trees[t]=new p(s,M,x,n,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 r=Math.max(-90,Math.min(90,t[1]));let n=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,n=180;else if(o>n){const t=this.getClusters([o,r,180,i],e),s=this.getClusters([-180,r,n,i],e);return t.concat(s)}const s=this.trees[this._limitZoom(e)],a=s.range(v(o),y(i),v(n),y(r)),u=[];for(const t of a){const e=s.points[t];u.push(e.numPoints?m(e):this.points[e.index])}return u}getChildren(t){const e=this._getOriginId(t),o=this._getOriginZoom(t),r="No cluster with the specified id.",n=this.trees[o];if(!n)throw new Error(r);const i=n.points[e];if(!i)throw new Error(r);const s=this.options.radius/(this.options.extent*Math.pow(2,o-1)),a=n.within(i.x,i.y,s),u=[];for(const e of a){const o=n.points[e];o.parentId===t&&u.push(o.numPoints?m(o):this.points[o.index])}if(0===u.length)throw new Error(r);return u}getLeaves(t,e,o){e=e||10,o=o||0;const r=[];return this._appendLeaves(r,t,e,o,0),r}getTile(t,e,o){const r=this.trees[this._limitZoom(t)],n=Math.pow(2,t),{extent:i,radius:s}=this.options,a=s/i,u=(o-a)/n,c=(o+1+a)/n,p={features:[]};return this._addTileFeatures(r.range((e-a)/n,u,(e+1+a)/n,c),r.points,e,o,n,p),0===e&&this._addTileFeatures(r.range(1-a/n,u,1,c),r.points,n,o,n,p),e===n-1&&this._addTileFeatures(r.range(0,u,a/n,c),r.points,-1,o,n,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,r,n){const i=this.getChildren(e);for(const e of i){const i=e.properties;if(i&&i.cluster?n+i.point_count<=r?n+=i.point_count:n=this._appendLeaves(t,i.cluster_id,o,r,n):n<r?n++:t.push(e),t.length===o)break}return n}_addTileFeatures(t,e,o,r,n,i){for(const s of t){const t=e[s],a=t.numPoints,u={type:1,geometry:[[Math.round(this.options.extent*(t.x*n-o)),Math.round(this.options.extent*(t.y*n-r))]],tags:a?g(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:r,extent:n,reduce:i}=this.options,s=r/(n*Math.pow(2,e));for(let r=0;r<t.length;r++){const n=t[r];if(n.zoom<=e)continue;n.zoom=e;const a=this.trees[e+1],u=a.within(n.x,n.y,s);let c=n.numPoints||1,p=n.x*c,h=n.y*c,l=i&&c>1?this._map(n,!0):null;const f=(r<<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 r=o.numPoints||1;p+=o.x*r,h+=o.y*r,c+=r,o.parentId=f,i&&(l||(l=this._map(n,!0)),i(l,this._map(o)))}1===c?o.push(n):(n.parentId=f,o.push(d(p/c,h/c,f,c,l)))}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?b({},t.properties):t.properties;const o=this.points[t.index].properties,r=this.options.map(o);return e&&r===o?b({},r):r}}function d(t,e,o,r,n){return{x:t,y:e,zoom:1/0,id:o,parentId:-1,numPoints:r,properties:n}}function f(t,e){const[o,r]=t.geometry.coordinates;return{x:v(o),y:y(r),zoom:1/0,index:e,parentId:-1}}function m(t){return{type:"Feature",id:t.id,properties:g(t),geometry:{type:"Point",coordinates:[(e=t.x,360*(e-.5)),_(t.y)]}};var e}function g(t){const e=t.numPoints,o=e>=1e4?`${Math.round(e/1e3)}k`:e>=1e3?`${Math.round(e/100)/10}k`:e;return b(b({},t.properties),{cluster:!0,cluster_id:t.id,point_count:e,point_count_abbreviated:o})}function v(t){return t/360+.5}function y(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 b(t,e){for(const o in e)t[o]=e[o];return t}function M(t){return t.x}function x(t){return t.y}var k=function(t){function e(o,n){void 0===n&&(n={});var i=t.call(this)||this;i._updateVisibleObjects=function(){if(i.isMarkersLoaded){i.markersCache.forEach((function(t){t.hide()})),i.clustersCache.forEach((function(t){var e=t.marker,o=t.label;e.hide(),o.hide()}));for(var t=0,e=i._getVisibleObjects();t<e.length;t++){var o=e[t],r=i._getObjectId(o);if(void 0!==r){var n=i._isClusterFeature(o)?i.clustersCache.get(r):i.markersCache.get(r);if(n)if(n instanceof mapgl.Marker)n.show();else{var s=n.marker,a=n.label;s.show(),a.show()}else i._isClusterFeature(o)?i._createCluster(r,o.geometry.coordinates,o.properties.point_count):i._createMarker(r,o.geometry.coordinates)}}}},i._updateBoundsPadding=function(){var t=i.map.getSize();i.boundsPaddingX=.06*t[0],i.boundsPaddingY=.06*t[1]};var s=void 0===n.radius?e.options.radius:n.radius,a=e.options.clusterStyle;n.clusterStyle&&(a="function"==typeof n.clusterStyle?n.clusterStyle:Object(r.a)(Object(r.a)({},a),n.clusterStyle)),i.options={radius:s,clusterStyle:a},i.map=o,i.inputMarkers=[],i.supercluster=new l({minZoom:0,maxZoom:20,radius:i.options.radius}),i.markersCache=new Map,i.clustersCache=new Map,i.isMarkersLoaded=!1;var u=o.getSize();return i.boundsPaddingX=.06*u[0],i.boundsPaddingY=.06*u[1],i.map.on("move",i._updateVisibleObjects),window.addEventListener("resize",i._updateBoundsPadding),i}return Object(r.b)(e,t),e.prototype.load=function(t){var e=this;this.destroy(),this.inputMarkers=t;var o=this._prepareSuperclusterInput(t);this.supercluster.load(o),this._getVisibleObjects().forEach((function(t){var o=e._getObjectId(t);void 0!==o&&(e._isClusterFeature(t)?e._createCluster(o,t.geometry.coordinates,t.properties.point_count):e._createMarker(o,t.geometry.coordinates))})),this.isMarkersLoaded=!0},e.prototype.destroy=function(){this.markersCache.forEach((function(t){t.destroy()})),this.clustersCache.forEach((function(t){var e=t.marker,o=t.label;e.destroy(),o.remove()})),this.markersCache.clear(),this.clustersCache.clear(),this.supercluster.load([]),this.inputMarkers=[],this.isMarkersLoaded=!1},e.prototype._prepareSuperclusterInput=function(t){return t.map((function(t,e){return{type:"Feature",geometry:{type:"Point",coordinates:t.coordinates},properties:null,id:e}}))},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=this.boundsPaddingX,o=this.boundsPaddingY,r=this.map.unproject([-e,t[1]+o]),n=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(r[0],n[0],i[0],s[0]),Math.min(r[1],n[1],i[1],s[1]),Math.max(r[0],n[0],i[0],s[0]),Math.max(r[1],n[1],i[1],s[1])]},e.prototype._createMarker=function(t,e){var o=this,r=this.inputMarkers[t],n=r.icon,i=r.size,s=r.anchor,a=r.hoverIcon,u=r.hoverSize,c=r.hoverAnchor,p=new mapgl.Marker(this.map,{coordinates:e,icon:n,size:i,anchor:s,hoverIcon:a,hoverSize:u,hoverAnchor:c});this.markersCache.set(t,p),p.on("click",(function(e){return o._emitObjectEvent("click",e,"marker",o.inputMarkers[t])})),p.on("mouseover",(function(e){return o._emitObjectEvent("mouseover",e,"marker",o.inputMarkers[t])})),p.on("mouseout",(function(e){return o._emitObjectEvent("mouseout",e,"marker",o.inputMarkers[t])}))},e.prototype._createCluster=function(t,o,n){var i=this,s="function"==typeof this.options.clusterStyle?Object(r.a)(Object(r.a)({},e.options.clusterStyle),this.options.clusterStyle(n,this.inputMarkers.length)):this.options.clusterStyle,a=new mapgl.Marker(this.map,{coordinates:o,zIndex:0,icon:s.markerIcon,size:s.markerSize,anchor:s.markerAnchor,hoverIcon:s.markerHoverIcon,hoverSize:s.markerHoverSize,hoverAnchor:s.markerHoverAnchor}),u=new mapgl.Label(this.map,{coordinates:o,text:String(n),phase:1,color:s.labelColor,fontSize:s.labelFontSize,font:s.labelFont,haloRadius:s.labelHaloRadius,haloColor:s.labelHaloColor,letterSpacing:s.labelLetterSpacing,offset:s.labelOffset});this.clustersCache.set(t,{marker:a,label:u});var c=[];this.supercluster.getLeaves(t,1/0).forEach((function(t){var e=i._getObjectId(t);void 0!==e&&c.push(i.inputMarkers[e])})),a.on("click",(function(t){return i._emitObjectEvent("click",t,"cluster",c)})),a.on("mouseover",(function(t){return i._emitObjectEvent("mouseover",t,"cluster",c)})),a.on("mouseout",(function(t){return i._emitObjectEvent("mouseout",t,"cluster",c)}))},e.prototype._getObjectId=function(t){var e=this._isClusterFeature(t)?Number(t.properties.cluster_id):Number(t.id);if(!Number.isNaN(e))return e},e.prototype._isClusterFeature=function(t){return null!==t.properties&&t.properties.cluster},e.prototype._emitObjectEvent=function(t,e,o,r){var n={originalEvent:e.originalEvent,lngLat:e.lngLat,point:e.point,target:{type:o,data:r}};this.emit(t,n)},e.options={radius:80,clusterStyle:{markerIcon:"data:image/svg+xml;base64,PHN2ZyB4bWxucz0iaHR0cDovL3d3dy53My5vcmcvMjAwMC9zdmciIHdpZHRoPSIzMCIgaGVpZ2h0PSIzMCIgdmlld0JveD0iMCAwIDMwIDMwIj48Y2lyY2xlIGZpbGw9IiM0OGQ5NWQiIGN4PSIxNSIgY3k9IjE1IiByPSIxNSIvPjwvc3ZnPg==",labelColor:"#000000",labelFontSize:14,labelFont:"Open_Sans"}},e}(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,r=function(n){o.off(t,r),e.call(o,n)};return this.on(t,r),this},t.prototype.off=function(t,e){var o=this.events[t];if(!o)return this;var r=o.indexOf(e);return-1!==r&&o.splice(r,1),this},t.prototype.emit=function(t,e){var o=this.events[t];if(!o)return this;for(var r=o.slice(),n=0;n<r.length;n++)r[n].call(this,e);return this},t}());"mapgl"in window?mapgl.ClusterGroup=k:console.error("Looks like you did not load MapGL SDK 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)};function r(t,e){function o(){this.constructor=t}n(t,e),t.prototype=null===e?Object.create(e):(o.prototype=e.prototype,new o)}var i=function(){return(i=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 s(){for(var t=0,e=0,o=arguments.length;e<o;e++)t+=arguments[e].length;var n=Array(t),r=0;for(e=0;e<o;e++)for(var i=arguments[e],s=0,a=i.length;s<a;s++,r++)n[r]=i[s];return n}},,function(t,e,o){"use strict";o.r(e);var n=o(0);function r(t,e,o,n,s,a){if(s-n<=o)return;const u=n+s>>1;!function t(e,o,n,r,s,a){for(;s>r;){if(s-r>600){const i=s-r+1,u=n-r+1,c=Math.log(i),p=.5*Math.exp(2*c/3),h=.5*Math.sqrt(c*p*(i-p)/i)*(u-i/2<0?-1:1),l=Math.max(r,Math.floor(n-u*p/i+h)),f=Math.min(s,Math.floor(n+(i-u)*p/i+h));t(e,o,n,l,f,a)}const u=o[2*n+a];let c=r,p=s;for(i(e,o,r,n),o[2*s+a]>u&&i(e,o,r,s);c<p;){for(i(e,o,c,p),c++,p--;o[2*c+a]<u;)c++;for(;o[2*p+a]>u;)p--}o[2*r+a]===u?i(e,o,r,p):(p++,i(e,o,p,s)),p<=n&&(r=p+1),n<=p&&(s=p-1)}}(t,e,u,n,s,a%2),r(t,e,o,n,u-1,a+1),r(t,e,o,u+1,s,a+1)}function i(t,e,o,n){s(t,o,n),s(e,2*o,2*n),s(e,2*o+1,2*n+1)}function s(t,e,o){const n=t[e];t[e]=t[o],t[o]=n}function a(t,e,o,n){const r=t-o,i=e-n;return r*r+i*i}const u=t=>t[0],c=t=>t[1];class p{constructor(t,e=u,o=c,n=64,i=Float64Array){this.nodeSize=n,this.points=t;const s=t.length<65536?Uint16Array:Uint32Array,a=this.ids=new s(t.length),p=this.coords=new i(2*t.length);for(let n=0;n<t.length;n++)a[n]=n,p[2*n]=e(t[n]),p[2*n+1]=o(t[n]);r(a,p,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 h=a.pop(),l=a.pop(),f=a.pop();if(l-f<=s){for(let s=f;s<=l;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+l)/2);c=e[2*d],p=e[2*d+1],c>=o&&c<=r&&p>=n&&p<=i&&u.push(t[d]);const m=(h+1)%2;(0===h?o<=c:n<=p)&&(a.push(f),a.push(d-1),a.push(m)),(0===h?r>=c:i>=p)&&(a.push(d+1),a.push(l),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],u=[],c=r*r;for(;s.length;){const p=s.pop(),h=s.pop(),l=s.pop();if(h-l<=i){for(let r=l;r<=h;r++)a(e[2*r],e[2*r+1],o,n)<=c&&u.push(t[r]);continue}const f=Math.floor((l+h)/2),d=e[2*f],m=e[2*f+1];a(d,m,o,n)<=c&&u.push(t[f]);const g=(p+1)%2;(0===p?o-r<=d:n-r<=m)&&(s.push(l),s.push(f-1),s.push(g)),(0===p?o+r>=d:n+r>=m)&&(s.push(f+1),s.push(h),s.push(g))}return u}(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 l{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(d(t[e],e));this.trees[n+1]=new p(s,M,x,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 p(s,M,x,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(y(o),v(i),y(r),v(n)),u=[];for(const t of a){const e=s.points[t];u.push(e.numPoints?m(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?m(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?g(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,h=r.y*c,l=i&&c>1?this._map(r,!0):null;const d=(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,h+=o.y*n,c+=n,o.parentId=d,i&&(l||(l=this._map(r,!0)),i(l,this._map(o)))}1===c?o.push(r):(r.parentId=d,o.push(f(p/c,h/c,d,c,l)))}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 f(t,e,o,n,r){return{x:t,y:e,zoom:1/0,id:o,parentId:-1,numPoints:n,properties:r}}function d(t,e){const[o,n]=t.geometry.coordinates;return{x:y(o),y:v(n),zoom:1/0,index:e,parentId:-1}}function m(t){return{type:"Feature",id:t.id,properties:g(t),geometry:{type:"Point",coordinates:[(e=t.x,360*(e-.5)),b(t.y)]}};var e}function g(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 y(t){return t/360+.5}function v(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 b(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 x(t){return t.y}var w=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}(),S=["click","dblclick","mousemove","mouseover","mouseout","mousedown","mouseup","touchstart","touchend"];var I=function(t){function e(o,r){var i;void 0===r&&(r={});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=r.radius)&&void 0!==i?i:e.options.radius,u=e.options.clusterStyle;return r.clusterStyle&&(u="function"==typeof r.clusterStyle?r.clusterStyle:Object(n.a)(Object(n.a)({},u),r.clusterStyle)),s.options={radius:a,clusterStyle:u},s.map=o,s.inputMarkers=[],s.supercluster=new l({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 Object(n.b)(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});S.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,r){var i=this,s="function"==typeof this.options.clusterStyle?Object(n.a)(Object(n.a)({},e.options.clusterStyle),this.options.clusterStyle(r)):this.options.clusterStyle,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]}));S.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:String(r),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}},e}(w);"mapgl"in window?mapgl.Clusterer=I:console.error("Looks like you did not load MapGL API correctly")}]); |
@@ -1,2 +0,2 @@ | ||
!function(e){var r={};function t(n){if(r[n])return r[n].exports;var o=r[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,t),o.l=!0,o.exports}t.m=e,t.c=r,t.d=function(e,r,n){t.o(e,r)||Object.defineProperty(e,r,{enumerable:!0,get:n})},t.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},t.t=function(e,r){if(1&r&&(e=t(e)),8&r)return e;if(4&r&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(t.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&r&&"string"!=typeof e)for(var o in e)t.d(n,o,function(r){return e[r]}.bind(null,o));return n},t.n=function(e){var r=e&&e.__esModule?function(){return e.default}:function(){return e};return t.d(r,"a",r),r},t.o=function(e,r){return Object.prototype.hasOwnProperty.call(e,r)},t.p="/",t(t.s=1)}([function(e,r,t){"use strict";t.d(r,"b",(function(){return o})),t.d(r,"a",(function(){return a})),t.d(r,"c",(function(){return i})); | ||
!function(e){var t={};function r(n){if(t[n])return t[n].exports;var o=t[n]={i:n,l:!1,exports:{}};return e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){"undefined"!=typeof Symbol&&Symbol.toStringTag&&Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),Object.defineProperty(e,"__esModule",{value:!0})},r.t=function(e,t){if(1&t&&(e=r(e)),8&t)return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;var n=Object.create(null);if(r.r(n),Object.defineProperty(n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));return n},r.n=function(e){var t=e&&e.__esModule?function(){return e.default}:function(){return e};return r.d(t,"a",t),t},r.o=function(e,t){return Object.prototype.hasOwnProperty.call(e,t)},r.p="/",r(r.s=1)}([function(e,t,r){"use strict";r.d(t,"b",(function(){return o})),r.d(t,"a",(function(){return i})),r.d(t,"c",(function(){return a})); | ||
/*! ***************************************************************************** | ||
@@ -16,2 +16,2 @@ Copyright (c) Microsoft Corporation. All rights reserved. | ||
***************************************************************************** */ | ||
var n=function(e,r){return(n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,r){e.__proto__=r}||function(e,r){for(var t in r)r.hasOwnProperty(t)&&(e[t]=r[t])})(e,r)};function o(e,r){function t(){this.constructor=e}n(e,r),e.prototype=null===r?Object.create(r):(t.prototype=r.prototype,new t)}var a=function(){return(a=Object.assign||function(e){for(var r,t=1,n=arguments.length;t<n;t++)for(var o in r=arguments[t])Object.prototype.hasOwnProperty.call(r,o)&&(e[o]=r[o]);return e}).apply(this,arguments)};function i(){for(var e=0,r=0,t=arguments.length;r<t;r++)e+=arguments[r].length;var n=Array(e),o=0;for(r=0;r<t;r++)for(var a=arguments[r],i=0,u=a.length;i<u;i++,o++)n[o]=a[i];return n}},function(e,r,t){"use strict";t.r(r);var n=t(0),o=new mapgl.Map("container",{center:[55.31878,25.23584],zoom:13,key:"Your API access key"});function a(e,r){return"data:image/svg+xml;base64,"+btoa('<svg xmlns="http://www.w3.org/2000/svg" width="'+2*r+'" height="'+2*r+'" viewBox="0 0 '+2*r+" "+2*r+'"><circle fill="'+e+'" cx="'+r+'" cy="'+r+'" r="'+r+'"/></svg>')}window.addEventListener("resize",(function(){return o.invalidateSize()}));var i=new mapgl.ClusterGroup(o,{clusterStyle:function(e,r){return e<.3*r?{markerIcon:a("#48d95d",12.5),markerSize:[25,25],markerHoverIcon:a("#24b137",12.5),markerHoverSize:[30,30],labelHaloRadius:2,labelHaloColor:"#88ffffff",labelFontSize:12}:e<.7*r?{markerIcon:a("#ffad33",15),markerSize:[30,30],markerHoverIcon:a("#c07915",15),markerHoverSize:[35,35],labelHaloRadius:2,labelHaloColor:"#88ffffff",labelFont:"Verdana"}:{markerIcon:a("#e82734",17.5),markerSize:[35,35],markerHoverIcon:a("#c3121e",17.5),markerHoverSize:[40,40],labelHaloRadius:2,labelHaloColor:"#88ffffff",labelFontSize:16,labelFont:"Open_Sans_Italic"}}}),u=function(e){console.log(e)};i.on("click",u),i.on("mouseover",u),i.on("mouseout",u);var c=function(e){for(var r=o.getBounds(),t=Object(n.c)(r.northEast,r.southWest),i=[],u=a("#1d8ce8",10),c=a("#12c3b2",10),l=0;l<e;l++)i.push({coordinates:[t[0]+Math.random()*(t[2]-t[0]),t[1]+Math.random()*(t[3]-t[1])],properties:{id:l},icon:l%2==0?c:u});return i}(5e4);i.load(c)}]); | ||
var n=function(e,t){return(n=Object.setPrototypeOf||{__proto__:[]}instanceof Array&&function(e,t){e.__proto__=t}||function(e,t){for(var r in t)t.hasOwnProperty(r)&&(e[r]=t[r])})(e,t)};function o(e,t){function r(){this.constructor=e}n(e,t),e.prototype=null===t?Object.create(t):(r.prototype=t.prototype,new r)}var i=function(){return(i=Object.assign||function(e){for(var t,r=1,n=arguments.length;r<n;r++)for(var o in t=arguments[r])Object.prototype.hasOwnProperty.call(t,o)&&(e[o]=t[o]);return e}).apply(this,arguments)};function a(){for(var e=0,t=0,r=arguments.length;t<r;t++)e+=arguments[t].length;var n=Array(e),o=0;for(t=0;t<r;t++)for(var i=arguments[t],a=0,u=i.length;a<u;a++,o++)n[o]=i[a];return n}},function(e,t,r){"use strict";r.r(t);var n=r(0),o=new mapgl.Map("container",{center:[55.31878,25.23584],zoom:13,key:"Your API access key"});function i(e,t){return"data:image/svg+xml;base64,"+btoa('<svg xmlns="http://www.w3.org/2000/svg" width="'+2*t+'" height="'+2*t+'" viewBox="0 0 '+2*t+" "+2*t+'"><circle fill="'+e+'" cx="'+t+'" cy="'+t+'" r="'+t+'"/></svg>')}window.addEventListener("resize",(function(){return o.invalidateSize()}));var a=new mapgl.Clusterer(o,{clusterStyle:function(e){return e<5e3?{icon:i("#48d95d",12.5),size:[25,25],hoverIcon:i("#24b137",12.5),hoverSize:[30,30],labelHaloRadius:2,labelHaloColor:"#88ffffff",labelFontSize:12}:e<3e4?{icon:i("#ffad33",15),size:[30,30],hoverIcon:i("#c07915",15),hoverSize:[35,35],labelHaloRadius:2,labelHaloColor:"#88ffffff"}:{icon:i("#e82734",17.5),size:[35,35],hoverIcon:i("#c3121e",17.5),hoverSize:[40,40],labelHaloRadius:2,labelHaloColor:"#88ffffff",labelFontSize:16}}}),u=function(e){console.log(e)};a.on("click",u),a.on("mouseover",u),a.on("mouseout",u);var c=function(e){for(var t=o.getBounds(),r=Object(n.c)(t.northEast,t.southWest),a=[],u=i("#1d8ce8",10),c=i("#12c3b2",10),f=0;f<e;f++)a.push({coordinates:[r[0]+Math.random()*(r[2]-r[0]),r[1]+Math.random()*(r[3]-r[1])],properties:{id:f},icon:f%2==0?c:u});return a}(5e4);a.load(c)}]); |
{ | ||
"name": "@2gis/mapgl-clusterer", | ||
"version": "0.0.3", | ||
"version": "0.1.0", | ||
"description": "Marker clustering plugin for Mapgl", | ||
"main": "dist/clustering.js", | ||
"scripts": { | ||
"assets": "mkdir -p dist && cp demo/*.html dist", | ||
"assets": "mkdirp dist && cp demo/*.html dist", | ||
"build": "npm run assets && webpack --production", | ||
@@ -19,5 +19,7 @@ "dev": "npm run assets && webpack-dev-server", | ||
"devDependencies": { | ||
"@2gis/jakarta": "https://gitlab.2gis.ru/WebMaps/jakarta/builds/artifacts/v12.5.0/raw/build.tar.gz?job=release", | ||
"@types/supercluster": "^5.0.2", | ||
"fork-ts-checker-webpack-plugin": "^4.0.5", | ||
"geojson": "^0.5.0", | ||
"mkdirp": "^1.0.3", | ||
"prettier": "^1.19.1", | ||
@@ -24,0 +26,0 @@ "supercluster": "^7.0.0", |
20895
-5.18%14
16.67%