0

Here API clustering error: "addLayer is not defined"

Error I am calling the function startClustering, but I get a error map.addLayer is undefined.

I think maybe the map object lost the context scope, but how can I could pass the right scope to have access to addLayer function from the map.

    //Boilerplate map initialization code starts below:
    var defaultLayers = platform.createDefaultLayers();

    var map = new H.Map(document.getElementById('map'),
        defaultLayers.normal.map, {
            center: {
                lat: -19.9211375,
                lng: -43.9490617,
            },
            zoom: 6
        });

 var behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(map));

    // create default UI with layers provided by the platform
    var ui = H.ui.UI.createDefault(map, defaultLayers, 'pt-BR');

function startClustering(map, data) {
            var dataPoints = data.map(function (item) {
                return new H.clustering.DataPoint(item.coords.lat, item.coords.lng);
            });

            var clusteredDataProvider = new H.clustering.Provider(dataPoints, {
                clusteringOptions: {
                eps: 32,
                minWeight: 2
                }
            });

            var clusteringLayer = new H.map.layer.ObjectLayer(clusteredDataProvider);

            map.addLayer(clusteringLayer);
    }

startClustering(map, travelData);
Cœur
  • 37,241
  • 25
  • 195
  • 267
João Fagner
  • 119
  • 1
  • 3
  • 12

1 Answers1

0

I was not able to reproduce the "addLayer is not defined" error you observed. Since you did not provide a full listing (for example, travelData is not defined), I suspect the issue may not be demonstrated in the code sample to help you debug.

It looks like you followed the clustering example from the documentation:

https://developer.here.com/api-explorer/maps-js/v3.0/clustering/marker-clustering

There is no need to wrap it in a function though if you suspect a scoping issue. For example this works for me:

var platform = new H.service.Platform({
    'app_id': 'your-app-id',
    'app_code': 'your-app-code',
    });

var defaultLayers = platform.createDefaultLayers();

var map = new H.Map(
    document.getElementById('map'),
    defaultLayers.normal.map, {
        center: {lat: -19.9211375,
                 lng: -43.9490617},
        zoom: 6,
    });

var behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(map));

var ui = H.ui.UI.createDefault(map, defaultLayers, 'pt-BR');

var travelData = [
    {lat: -19.8211, lng: -43.9490617},
    {lat: -19.7211, lng: -43.9490617},
    {lat: -19.6211, lng: -43.9490617},
    {lat: -19.5211, lng: -43.9490617},
    {lat: -19.4211, lng: -43.9490617},
    {lat: -19.3211, lng: -43.9490617},
];

var dataPoints = travelData.map(function(item) {
    return new H.clustering.DataPoint(item.lat, item.lng);
});

var clusteredDataProvider = new H.clustering.Provider(dataPoints, {
    clusteringOptions: {eps: 32, minWeight: 2 }
});

var clusteringLayer = new H.map.layer.ObjectLayer(clusteredDataProvider);
map.addLayer(clusteringLayer);

It's not exactly the same as I simplified travelData a bit so it may depend on your dataset. Also make sure you are including the mapjs-clustering.js script in your HTML, though you'd see an error like "H.clustering is undefined" if that were the issue.

j12y
  • 2,112
  • 3
  • 17
  • 22