这里是API群集错误:“未定义addLayer". [英] Here API clustering error: "addLayer is not defined"

查看:150
本文介绍了这里是API群集错误:“未定义addLayer".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试在我的应用程序中使用clastering,但是我收到一个未定义addLayer的错误消息."

I am try to use clastering in my aplication but I am get a error addLayer is not defined".

我不知道如何解决此问题.

I have no Idea how to resolve this issue.

我只是从Here API示例api集群中复制并粘贴此示例函数

I just copy and paste this sample function from Here API samples api clusters

然后我将lat和lng传递给函数,但addLayer未定义.

And I'm passing lat and lng, to the function but addLayer is undefined.

我有map对象,但是addLayer未定义.

I have the map object, but the addLayer is undefined.

function startClustering(map, data) {
  // First we need to create an array of DataPoint objects,
  // for the ClusterProvider
  var dataPoints = data.map(function(item) {
    return new H.clustering.DataPoint(item.latitude, item.longitude);
  });

  // Create a clustering provider with custom options for clusterizing the input
  var clusteredDataProvider = new H.clustering.Provider(dataPoints, {
    clusteringOptions: {
      // Maximum radius of the neighbourhood
      eps: 32,
      // minimum weight of points required to form a cluster
      minWeight: 2
    }
  });

  // Create a layer tha will consume objects from our clustering provider
  var clusteringLayer = new H.map.layer.ObjectLayer(clusteredDataProvider);

  // To make objects from clustering provder visible,
  // we need to add our layer to the map

  map.addLayer(clusteringLayer);

}

然后我将lat和lng传递给函数,但addLayer未定义.

And I'm passing lat and lng, to the function but addLayer is undefined.

我有地图对象,胸围不存在addLayer.

I have the map object, bust does not exist addLayer.

我嵌入了脚本

<script type="text/javascript" src="https://js.api.here.com/v3/3.0/mapsjs-clustering.js"></script>

在我的索引上.

我不知道如何解决此问题.

I don't know how to resolve this issue.

如果有人知道如何解决,我很乐意倾听

If someone knows how to resolve I'm glad to listen

编辑:每个评论请求的其他代码:

Additional code per comment request:

function addMarkerToCameraGroup(map, coordinate, html) { // add and remove markers
  document.querySelector(".camera-box").addEventListener("click", function() {
    if (document.getElementsByClassName("camera-marker-position")[0] === undefined) {
      map.addObject(cameraMarker);
      self.startClustering(map, coordinate);
    } else {
      map.removeAll();
    }
  });
}

推荐答案

您第二次以(a href ="https://stackoverflow.com/questions/51899752/try-to-create-a) -cluster-unsing-a-sample-but-addlayer-got-undefinied>尝试创建不使用示例的集群,但addLayer无法定义" ,其中未包含此问题的某些详细信息.但是,考虑到您在此问题上所做的其他编辑/上下文,似乎您正在添加事件侦听器,其中map可能超出范围或尚未为匿名函数内部的可见性定义.

You asked this question a second time as: "Try to create a cluster unsing a sample but addLayer got undefinied" which doesn't include some of the details in this question. Given the additional edit / context you made on this question however, it would seem you are adding an event listener in which map is probably out of scope or not yet defined for visibility inside the anonymous function.

document.querySelector(".camera-box").addEventListener("click", function() {
    if (document.getElementsByClassName("camera-marker-position")[0] === undefined) {
      map.addObject(cameraMarker);
      self.startClustering(map, coordinate);
    } else {
      map.removeAll();
    }
  });

您可以进行类型检查,以查看单击事件发生时是否已定义地图,并且/或者确认该地图是否为H.map.在没有看到完整列表的情况下,也可能是您没有将map设置为全局变量,而是在页面初始化的其他位置声明了它,因此在触发click事件时超出了范围.

You could do a typing check to see if map has been defined when the click event occurs and/or confirm whether it is an H.map. Without seeing the full listing, it may also be the case that you have not made map a global variable but instead declared it somewhere else in the page initialization so is out of scope when the click event is fired.

您可以检查 JavaScript回调范围,以获取有关可以在其中提供map的闭包的详细信息声明函数时的事件.有关更多常规信息: JavaScript HTML DOM EventListener

You can check JavaScript callback scope for details on closures where you could provide the map to the event when declaring the function. For more general information: JavaScript HTML DOM EventListener

这篇关于这里是API群集错误:“未定义addLayer".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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