Skip to content Skip to sidebar Skip to footer

Try To Create A Cluster Unsing A Sample But Addlayer Got Undefined

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

Solution 1:

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();

varmap = 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) {
    returnnew 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.

Post a Comment for "Try To Create A Cluster Unsing A Sample But Addlayer Got Undefined"