尝试在不添加示例的情况下创建集群,但addLayer未定义 [英] Try to create a cluster unsing a sample but addLayer got undefined

查看:54
本文介绍了尝试在不添加示例的情况下创建集群,但addLayer未定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

此处API群集错误:未定义addLayer"

错误,我正在调用函数startClustering,但出现错误map.addLayer未定义.

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

我认为也许地图对象失去了上下文范围,但是如何传递正确的范围才能从地图访问addLayer函数.

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

推荐答案

我无法重现您观察到的未定义addLayer" 错误.由于您没有提供完整的清单(例如,未定义 travelData ),因此我怀疑该代码示例中可能没有显示该问题来帮助您进行调试.

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

它与我简化的travelData并不完全相同,因此可能取决于您的数据集.还请确保在HTML中包含mapjs-clustering.js脚本,尽管如果出现问题,则会看到类似"H.clustering is undefined" 之类的错误.

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.

这篇关于尝试在不添加示例的情况下创建集群,但addLayer未定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆