在这里地图 - 如何获取所有可见簇?

发布于 2025-02-10 02:46:51 字数 849 浏览 1 评论 0原文

我想获取当前可见群集的数组,然后获取每个点数据。我在React中创建方法,看来主题中的方法GetClusterPresentation返回所有MAP Zooms的所有可能群集。如何获取集群数据?这是我的代码:

    const dataPoints = points.map(
      point => new H.clustering.DataPoint(point.lat, point.lng, undefined, point),
    );

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

    const defaultTheme = clusteredDataProvider.getTheme();

    clusteredDataProvider.setTheme({
      getClusterPresentation: cluster => {
        const clusterMarker = defaultTheme.getClusterPresentation(cluster);

        return clusterMarker;
      },
      getNoisePresentation: noisePoint => {},
    });
    const layer = new H.map.layer.ObjectLayer(clusteredDataProvider);
    map.addLayer(layer);

I want to get array of currently visible clusters and then get each point data. I create method in React and it seems that method in theme for getClusterPresentation returns all possible clusters for all map zooms. How to get clusters data? This is my code:

    const dataPoints = points.map(
      point => new H.clustering.DataPoint(point.lat, point.lng, undefined, point),
    );

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

    const defaultTheme = clusteredDataProvider.getTheme();

    clusteredDataProvider.setTheme({
      getClusterPresentation: cluster => {
        const clusterMarker = defaultTheme.getClusterPresentation(cluster);

        return clusterMarker;
      },
      getNoisePresentation: noisePoint => {},
    });
    const layer = new H.map.layer.ObjectLayer(clusteredDataProvider);
    map.addLayer(layer);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

断爱 2025-02-17 02:46:51

请检查以下与标记聚类有关的代码。然后您在我们的指南

/**
 * Display clustered markers on a map
 *
 * Note that the maps clustering module https://js.api.here.com/v3/3.1/mapsjs-clustering.js
 * must be loaded to use the Clustering

 * @param {H.Map} map A HERE Map instance within the application
 * @param {Object[]} data Raw data that contains airports' coordinates
*/
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);
}

/**
 * Boilerplate map initialization code starts below:
 */

// Step 1: initialize communication with the platform
// In your own code, replace variable window.apikey with your own apikey
var platform = new H.service.Platform({
  apikey: window.apikey
});

var defaultLayers = platform.createDefaultLayers();

// Step 2: initialize a map
var map = new H.Map(document.getElementById('map'), defaultLayers.vector.normal.map, {
  center: new H.geo.Point(30.789, 33.790),
  zoom: 2,
  pixelRatio: window.devicePixelRatio || 1
});
// add a resize listener to make sure that the map occupies the whole container
window.addEventListener('resize', () => map.getViewPort().resize());

// Step 3: make the map interactive
// MapEvents enables the event system
// Behavior implements default interactions for pan/zoom (also on mobile touch environments)
var behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(map));


// Step 4: create the default UI component, for displaying bubbles
var ui = H.ui.UI.createDefault(map, defaultLayers);

// Step 5: cluster data about airports's coordinates
// airports variable was injected at the page load
startClustering(map, airports);
#map {
    width: 95%;
    height: 450px;
    background: grey;
}

#panel {
    width: 100%;
    height: 400px;
}
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=yes">
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
    <title>Marker Clustering</title>
    <link rel="stylesheet" type="text/css" href="https://js.api.here.com/v3/3.1/mapsjs-ui.css" />
    <link rel="stylesheet" type="text/css" href="demo.css" />
    <link rel="stylesheet" type="text/css" href="styles.css" />
    <link rel="stylesheet" type="text/css" href="../template.css" />
    <script type="text/javascript" src='../test-credentials.js'></script>    
    <script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-core.js"></script>
    <script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-service.js"></script>
    <script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-ui.js"></script>
    <script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-mapevents.js"></script>
    <script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-clustering.js"></script>
    <script type="text/javascript" src="./data/airports.js"></script>
  </head>
  <body id="markers-on-the-map">

    <div class="page-header">
        <h1>Marker Clustering</h1>
        <p>Cluster multiple markers together to better visualize the data</p>
    </div>
    <p>This example displays a map showing the distribution of 
        airports across the world. The locations were obtained by using 
        the <a href="http://openflights.org/data.html" target="_blank">OpenFlights Airport Database</a>. 
        Instead of adding a marker for each location, the data has been clustered, 
        and individual airports are only shown at higher zoom levels.</p>
    <div id="map"></div>
    <h3>Code</h3>
    <p>Marker clustering requires the presence of the <code>mapsjs-clustering</code> module of the API. 
      The <code>H.clustering.Provider</code> class is used to load in data points and prepare them for clustering. 
      The result is added to the map as an additional layer using the <code>map.addLayer()</code> method.</p>
    <script type="text/javascript" src='demo.js'></script>
  </body>
</html>

Please check the below code related to Marker Clustering. And you check same example in our Guide

/**
 * Display clustered markers on a map
 *
 * Note that the maps clustering module https://js.api.here.com/v3/3.1/mapsjs-clustering.js
 * must be loaded to use the Clustering

 * @param {H.Map} map A HERE Map instance within the application
 * @param {Object[]} data Raw data that contains airports' coordinates
*/
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);
}

/**
 * Boilerplate map initialization code starts below:
 */

// Step 1: initialize communication with the platform
// In your own code, replace variable window.apikey with your own apikey
var platform = new H.service.Platform({
  apikey: window.apikey
});

var defaultLayers = platform.createDefaultLayers();

// Step 2: initialize a map
var map = new H.Map(document.getElementById('map'), defaultLayers.vector.normal.map, {
  center: new H.geo.Point(30.789, 33.790),
  zoom: 2,
  pixelRatio: window.devicePixelRatio || 1
});
// add a resize listener to make sure that the map occupies the whole container
window.addEventListener('resize', () => map.getViewPort().resize());

// Step 3: make the map interactive
// MapEvents enables the event system
// Behavior implements default interactions for pan/zoom (also on mobile touch environments)
var behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(map));


// Step 4: create the default UI component, for displaying bubbles
var ui = H.ui.UI.createDefault(map, defaultLayers);

// Step 5: cluster data about airports's coordinates
// airports variable was injected at the page load
startClustering(map, airports);
#map {
    width: 95%;
    height: 450px;
    background: grey;
}

#panel {
    width: 100%;
    height: 400px;
}
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=yes">
    <meta http-equiv="Content-type" content="text/html;charset=UTF-8">
    <title>Marker Clustering</title>
    <link rel="stylesheet" type="text/css" href="https://js.api.here.com/v3/3.1/mapsjs-ui.css" />
    <link rel="stylesheet" type="text/css" href="demo.css" />
    <link rel="stylesheet" type="text/css" href="styles.css" />
    <link rel="stylesheet" type="text/css" href="../template.css" />
    <script type="text/javascript" src='../test-credentials.js'></script>    
    <script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-core.js"></script>
    <script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-service.js"></script>
    <script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-ui.js"></script>
    <script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-mapevents.js"></script>
    <script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-clustering.js"></script>
    <script type="text/javascript" src="./data/airports.js"></script>
  </head>
  <body id="markers-on-the-map">

    <div class="page-header">
        <h1>Marker Clustering</h1>
        <p>Cluster multiple markers together to better visualize the data</p>
    </div>
    <p>This example displays a map showing the distribution of 
        airports across the world. The locations were obtained by using 
        the <a href="http://openflights.org/data.html" target="_blank">OpenFlights Airport Database</a>. 
        Instead of adding a marker for each location, the data has been clustered, 
        and individual airports are only shown at higher zoom levels.</p>
    <div id="map"></div>
    <h3>Code</h3>
    <p>Marker clustering requires the presence of the <code>mapsjs-clustering</code> module of the API. 
      The <code>H.clustering.Provider</code> class is used to load in data points and prepare them for clustering. 
      The result is added to the map as an additional layer using the <code>map.addLayer()</code> method.</p>
    <script type="text/javascript" src='demo.js'></script>
  </body>
</html>

笨死的猪 2025-02-17 02:46:51

在最近的版本v3.1.19中,这里在H.Map.provider.localobjectProvider中添加了一种新方法GetObjectswithin。

这是示例代码:

// Obtains cluster from within the visible map area:
function getVisibleClusters(clusteringProvider, zoom, callback) {
  clusteringProvider.getObjectsWithin(
    map.getViewModel().getLookAtData().bounds,
    objects => {
      callback(objects.filter(object => object.data.isCluster()).map(object => object.data));
    },
    {visiblesOnly: true, zoom, types: mapsjs.map.Object.Type.ANY}
  );
}

In the recent release v3.1.19, HERE added a new Method getObjectsWithin to H.map.provider.LocalObjectProvider.

Here are the sample codes:

// Obtains cluster from within the visible map area:
function getVisibleClusters(clusteringProvider, zoom, callback) {
  clusteringProvider.getObjectsWithin(
    map.getViewModel().getLookAtData().bounds,
    objects => {
      callback(objects.filter(object => object.data.isCluster()).map(object => object.data));
    },
    {visiblesOnly: true, zoom, types: mapsjs.map.Object.Type.ANY}
  );
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文