Search code examples
javascriptmapshere-api

HERE Maps JS API v3: listen to `clusteringend` event


Is there any convenient way to tell whether the clustering process has finished/ended? I don't seem to see any native clusteringend event in the clustering provider that I can listen to.

Have been bangin' my head again since the last couple of days on this issue. =(


Solution

  • Yeah, whatever. Rather than waiting for some disappointing answer from some HERE Maps rookie, I tried to work around my issue and made something freaky like so (haven't tested it thoroughly but it's been working quite well so far):

    var
    /*
     * Work "listen-to-clusteringend-event-issue" around.
     */
    processedNoisePointCounter   = 0,
    incrementProcessedNoisePoint = function () {
        processedNoisePointCounter++;
    
        if (processedNoisePointCounter === dataPoints.length) {
            map.dispatchEvent('clusteringend');
        }
    },
    clusteredDataProvider        = new H.clustering.Provider([]),
    defaultTheme                 = clusteredDataProvider.getTheme(),
    customTheme                  = {
    
        /**
         *
         * @implements {H.clustering.ITheme.getClusterPresentation}
         */
        getClusterPresentation: function (cluster) {
            var clusterMarker = defaultTheme.getClusterPresentation.call(defaultTheme, cluster);
    
            cluster.forEachEntry(function (entry) {
    
                if (!entry.isCluster()) {
                    incrementProcessedNoisePoint();
                }
            });
    
            return clusterMarker;
        },
    
        /**
         *
         * @implements {H.clustering.ITheme.getNoisePresentation}
         */
        getNoisePresentation: function (noisePoint) {
            var noiseMarker = defaultTheme.getNoisePresentation.call(defaultTheme, noisePoint);
    
            incrementProcessedNoisePoint();
    
            return noiseMarker;
        }
    };
    
    clusteredDataProvider.setTheme(customTheme);
    clusteredDataProvider.setDataPoints(dataPoints);
    
    var layer = new H.map.layer.ObjectLayer(clusteredDataProvider);
    map.addLayer(layer);
    
    map.addEventListener('clusteringend', function () {
        var message = 'Clustering allegedly ends. ;)';
    
        console.log(message);
        window.alert(message);
    });
    

    The JSFiddle is available here.