arcgis for js FeatureLayer和GeoJSON一个矢量点同时渲染图形和文本

效果

FeatureLayer和GeoJSONLayer, 一个矢量点同时渲染图形和文本

代码

相关参数自行查阅文档, 这里就不做注释了

示例代码手动创建FeatureLayer方式, 如果是通过远程url加载图层的 渲染方式同理, GeoJSONLayer同理

html 复制代码
<!DOCTYPE html>
<html lang="zn">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>

    <style>
      body {
        margin: 0;
      }
      #mapview {
        position: absolute;
        width: 100%;
        height: 100%;
      }
      * {
        margin: 0;
        padding: 0;
      }
    </style>

    <link rel="stylesheet" href="https://js.arcgis.com/4.23/esri/themes/light/main.css" />
    <script src="https://js.arcgis.com/4.23/"></script>
  </head>
  <body>
    <div id="mapview"></div>

    <script>
      require([
        'esri/Map',
        'esri/views/MapView',
        'esri/layers/FeatureLayer',
        'esri/Graphic'
      ], function (Map, MapView, FeatureLayer, Graphic) {
        // 初始化底图
        window.map = new Map({
          basemap: 'dark-gray-vector'
        })

        // 创建2维视图
        let view = new MapView({
          container: 'mapview',
          map: map,
          zoom: 11,
          center: [104.783916597735, 32.55699155692144] // 设置地图的初始化中心点坐标
        })

        // 生成100个随机点
        var points = generateRandomPointsInPolygon(
          [
            [
              [104.64487088240317, 32.66500681729125],
              [104.91060269392625, 32.6635616904849],
              [104.88210690535206, 32.54642886312837],
              [104.68812954939528, 32.54787588095771]
            ]
          ],
          100
        )

        // 创建feature点位
        const features = points.map((point, index) => ({
          geometry: {
            type: 'point',
            x: point[0],
            y: point[1]
          },
          attributes: {
            ObjectID: Math.random(),
            name: `点${index + 1}`
          }
        }))
        if (!features.length) {
          return
        }
        console.log(features)

        const featureLayer = new FeatureLayer({
          source: features,
          title: '生态因子点位图层',
          objectIdField: 'ObjectID',  // 重要, 必须
          fields: [{ name: 'name', type: 'string' }],
          outFields: ['*'],
          popupTemplate: {
            title: '{name}'
          },
          // 渲染成简单圆点
          renderer: {
            type: 'simple',
            symbol: {
              type: 'simple-marker',
              color: 'red',
              size: '10px',
              outline: {
                width: 1,
                color: '#fff'
              }
            }
          },
          // 渲染额外的文本图形
          labelingInfo: [
            {
              symbol: {
                type: 'text',
                color: '#fff',
                // haloSize: 1,
                // haloColor: '#fff',
                backgroundColor: 'rgba(27,140,155,0.7)',
                // xoffset: -22,
                yoffset: 15,
                horizontalAlignment: 'center',
                font: { family: 'sans-serif', size: 8 }
              },
              labelPlacement: 'center-center',
              labelExpressionInfo: { expression: '$feature.name' }
            }
          ]
        })
        map.add(featureLayer)

        // 添加点击
        view.on('click', event => {
          const { longitude, latitude } = event.mapPoint
          // console.log('>>> 点击的坐标: ')
          console.log(`${longitude}, ${latitude}`)
        })

        // 生成随机点
        function generateRandomPointsInPolygon(polygonCoords, number) {
          // 判断是否在图形内
          function isPointInsidePolygon(point, polygonCoords) {
            let crossings = 0

            for (let i = 0, j = polygonCoords[0].length - 1; i < polygonCoords[0].length; j = i++) {
              const xi = polygonCoords[0][i][0]
              const yi = polygonCoords[0][i][1]
              const xj = polygonCoords[0][j][0]
              const yj = polygonCoords[0][j][1]

              if (
                ((yi <= point[1] && point[1] < yj) || (yj <= point[1] && point[1] < yi)) &&
                point[0] < ((xj - xi) * (point[1] - yi)) / (yj - yi) + xi
              ) {
                crossings++
              }
            }
            return crossings % 2 !== 0
          }

          // 存储生成的随机点
          let randomPoints = []

          // 获取多边形的最小和最大经纬度范围
          let minLat = polygonCoords[0][0][1]
          let maxLat = polygonCoords[0][0][1]
          let minLng = polygonCoords[0][0][0]
          let maxLng = polygonCoords[0][0][0]

          for (let i = 0; i < polygonCoords[0].length; i++) {
            const lat = polygonCoords[0][i][1]
            const lng = polygonCoords[0][i][0]

            if (lat < minLat) {
              minLat = lat
            }
            if (lat > maxLat) {
              maxLat = lat
            }
            if (lng < minLng) {
              minLng = lng
            }
            if (lng > maxLng) {
              maxLng = lng
            }
          }

          for (let i = 0; i < number; i++) {
            let isPointInside = false
            let randomPoint

            // 不断尝试生成随机点,直到点在多边形内
            while (!isPointInside) {
              // 在多边形的经纬度范围内随机生成一个点
              const randomLat = minLat + (maxLat - minLat) * Math.random()
              const randomLng = minLng + (maxLng - minLng) * Math.random()

              randomPoint = [randomLng, randomLat]

              // 使用射线法判断点是否在多边形内
              isPointInside = isPointInsidePolygon(randomPoint, polygonCoords)
            }
            randomPoints.push(randomPoint)
          }
          return randomPoints
        }
      })
    </script>
  </body>
</html>
相关推荐
Highcharts.js9 分钟前
Highcharts与TypeScript集成指南
前端·javascript·typescript·前端框架·数据可视化·highcharts
跳跳的小古风1 小时前
前端转全栈:一条接口从前端请求到 SQL 完整流程
java·前端
亿元程序员1 小时前
微信小游戏备案很久?这样操作可以直接发布!
前端
福狼1 小时前
The Expanse:让风沙成为世界的一部分
前端·three.js
桃李无言下自成蹊1 小时前
为什么越清晰的二维码越扫不出来:jsQR 四轮解码与 8 个真实坑
javascript
程序员鱼皮1 小时前
这 3 年 AI Agent 到底进化了啥?万字大白话,一次讲透!
前端·后端·aigc
用户900305093621 小时前
Claude Code平替评测:换掉之前先理清楚这些选型条件
前端
bai2731 小时前
同一个 JSON,Node 读出了三种坏法:Windows 上四种写法的编码实测
前端
ZANE1 小时前
纯前端实现的 .gitignore 生成器技术实现
前端
小y的笔记2 小时前
Vue 3 异步搜索实战:watch、请求取消与竞态控制
前端·javascript