Here API clustering error: “addLayer is not defined”

不问归期 提交于 2019-12-11 17:47:48

问题


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.

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

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

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

}

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

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

I'm embed the script

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

on my index.

I don't know how to resolve this issue.

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

EDIT: 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();
    }
  });
}

回答1:


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

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.

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



来源:https://stackoverflow.com/questions/51847218/here-api-clustering-error-addlayer-is-not-defined

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!