许可优化
许可优化
产品
产品
解决方案
解决方案
服务支持
服务支持
关于
关于
软件库
当前位置:服务支持 >  软件文章 >  React Leaflet Esri聚集要素:实现地图点要素聚合

React Leaflet Esri聚集要素:实现地图点要素聚合

阅读数 14
点赞 0
article_banner

问题描述

  1. esri-leaflet以插件(plugins)形式,直接在前端提供了要素图层的自动聚合功能,即esri.cluster.Featurelayer
  2. 在react环境下,esri.cluster.Featurelayer的使用与直接引入CDN有一些使用的细节差异

环境准备

  1. 安装lealet/esri-leaflet
操作步骤省略
  1. 安装esri-leaflet-cluster插件
npm install esri-leaflet-cluster leaflet.markercluster
注意:因为esri.cluster.Featurelayer是基于leaflet.markercluster进行扩展开发的。故而安装时,两个包都需要安装。后者本身不是leaflet的官方组件,其实也是第三方扩展的,具体使用细节参考,https://github.com/Leaflet/Leaflet.markercluster

在这里插入图片描述

  1. 引入esri.cluster.Featurelayerleaflet.markercluster

        的API和CSS样式文件支持
import React, { useEffect } from 'react';
import L from "leaflet"
import * as esri from "esri-leaflet"
import * as markercluster from "leaflet.markercluster"
import * as cluster from  "esri-leaflet-cluster"
import "leaflet.markercluster/dist/MarkerCluster.Default.css"
注意:若未引入leaflet.markercluster则编译运行后,浏览器很有可能报如下错误,主要就是基础类找不到的原因

TypeError: Object(…) is not a function

  21 |    this._layers = {};
  22 |    this._leafletIds = {};
  23 | 
> 24 |    this.cluster = markerClusterGroup(options);
     | ^  25 |    this._key = 'c' + (Math.random() * 1e9).toString(36).replace('.', '_');

实例 代码

import React, { useEffect } from 'react';
import L from "leaflet"
import * as esri from "esri-leaflet"
import * as markercluster from "leaflet.markercluster"
import * as cluster from  "esri-leaflet-cluster"
import "leaflet.markercluster/dist/MarkerCluster.Default.css"

function ClusterFeatureLayer(props) {
    useEffect(()=>{
        
        //对于clusterFeatureLayer必须设置maxZoom属性,否则编译会出错
        let map = L.map("clusterfeaturelayer_map", { maxZoom: 14}).setView([29.776,106.308],10)
        let layer = cluster.featureLayer({ 
            url: "http://localhost:6080/arcgis/rest/services/02_Tky_GDCGK/FeatureServer/4",
            pointToLayer: function (geojson, latlng) {
                return L.circleMarker(latlng, 2, {
                  color: "#2D84C8"
                });
              }
            })
        layer.addTo(map)
    },[])

    return (
        <div id="clusterfeaturelayer_map" style={{ width: "100%", height: "800px" }}></div>
    );
}

export default ClusterFeatureLayer;

代码 说明

  1. import的内容一定要完整,否则编译会出现各种奇怪的错误
  2. 添加了聚集图层的map一定要设置maxZoom属性,否则会引起Uncaught Map has no maxZoom specified而导致地图不能加载 TypeError: Cannot read property '_addChild' of undefined
  3. 聚集功能是完全基于前端实现展示层实现的,因此不需要对后台的FS有任何的特殊设置
  4. 一定要分清esri.cluster.Featurelayerleaflet.markercluster的职责范围:
       
  5. 因为esri.cluster.Featurelayer继承扩展自leaflet.markercluster因此,前者可以使用后者所提供的各种函数接口或config参数


免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删


相关文章
技术文档
QR Code
微信扫一扫,欢迎咨询~
customer

online

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 board-phone 155-2731-8020
close1
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

姓名不为空

姓名不为空
手机不正确

手机不正确

手机不正确
公司不为空

公司不为空

公司不为空