【问题标题】:HERE maps - how to get all visible clusters?HERE 地图 - 如何获取所有可见的集群?
【发布时间】:2022-11-07 18:12:53
【问题描述】:

我想获取当前可见簇的数组,然后获取每个点数据。我在 React 中创建方法,似乎 getClusterPresentation 的主题中的方法返回所有地图缩放的所有可能集群。如何获取集群数据?这是我的代码:

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

【问题讨论】:

    标签: reactjs here-api heremaps


    【解决方案1】:

    请检查以下与标记聚类相关的代码。您在我们的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>

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-10
      • 1970-01-01
      • 2022-11-30
      • 2017-05-19
      • 2021-05-25
      • 2016-01-02
      • 1970-01-01
      相关资源
      最近更新 更多