首页 > 解决方案 > 如何在 arcgis 4.x 中显示抖动强度?

问题描述

我从这个链接开始:

ArcGIS 示例 - GeoJSON 图层

我想按照以下链接显示震动强度:

样本

服务

我试图实现它。请看一看:

密码笔

<html>
  <head>
    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="initial-scale=1,maximum-scale=1,user-scalable=no"
    />
    <!--
  ArcGIS API for JavaScript, https://js.arcgis.com
  For more information about the layers-geojson sample, read the original sample description at developers.arcgis.com.
  https://developers.arcgis.com/javascript/latest/sample-code/layers-geojson/index.html
  -->
<title>GeoJSONLayer - 4.15</title>

    <style>
      html,
      body,
      #viewDiv {
        padding: 0;
        margin: 0;
        height: 100%;
        width: 100%;
      }
    </style>

    <link
      rel="stylesheet"
      href="https://js.arcgis.com/4.15/esri/themes/light/main.css"
    />
    <script src="https://js.arcgis.com/4.15/"></script>

    <script>
      require([
        "esri/Map",
        "esri/layers/GeoJSONLayer",
        "esri/views/MapView",
        "esri/widgets/Legend"
      ], function(Map, GeoJSONLayer, MapView, Legend) {
        // If GeoJSON files are not on the same domain as your website, a CORS enabled server
        // or a proxy is required.
        const url =
          //"https://earthquake.usgs.gov/fdsnws/event/1/query?format=geojson&starttime&minlatitude=33.387&minlongitude=-127.134&maxlatitude=40.457&maxlongitude=-115.994&minlatitude=33.387&minlongitude=-127.134&maxlatitude=40.457&maxlongitude=-115.994&producttype=shakemap";
              "https://services9.arcgis.com/RHVPKKiFTONKtxq3/arcgis/rest/services/USGS_Seismic_Data_v1/FeatureServer/1/query?where=1=1&geometry=-122.40,37.76&geometryType=esriGeometryPoint&inSR=4326&distance=100&units=esriSRUnit_StatuteMile&outFields=*&returnGeometry=true&geometryPrecision=4&outFields=*&f=pgeojson";

        // Paste the url into a browser's address bar to download and view the attributes
        // in the GeoJSON file. These attributes include:
        // * mag - magnitude
        // * type - earthquake or other event such as nuclear test
        // * place - location of the event
        // * time - the time of the event
        // Use the Arcade Date() function to format time field into a human-readable format

        const template = {
          title: "Earthquake Info",
          content: "Magnitude {mag} {type} hit {place} on {time}",
          fieldInfos: [
            {
              fieldName: "time",
              format: {
                dateFormat: "short-date-short-time"
              }
            }
          ]
        };

        const renderer = {
          type: "simple",
          field: "mag",
          symbol: {
            type: "simple-marker",
            color: "orange",
            outline: {
              color: "white"
            }
          },
          visualVariables: [
            {
              type: "size",
              field: "mag",
              stops: [
                {
                  value: 2.5,
                  size: "4px"
                },
                {
                  value: 8,
                  size: "40px"
                  
                }
              ]
            }
          ]
        };

        const geojsonLayer = new GeoJSONLayer({
          url: url,
          title: "MMI",
          copyright: "USGS Earthquakes",
          popupTemplate: template,
          renderer: renderer //optional
        });

        const map = new Map({
          basemap: "gray",
          layers: [geojsonLayer]
        });

        const view = new MapView({
          container: "viewDiv",
          center: [-122, 37],
          zoom: 6,
          map: map
        });
        view.ui.add(
          new Legend({
            view: view
          }),
          "bottom-left"
        );
      });
    </script>
  </head>

  <body>
    <div id="viewDiv"></div>
  </body>
</html>

该网址返回一个多面体特征:

服务查询尝试

该地图显示没有多边形的点。请你帮助我好吗?

标签: geojsonarcgis-js-api

解决方案


查询实际上是返回多边形,您遇到的问题是错误的渲染器。这就是您将其视为点的原因。

在这里,您的示例正在运行,我使用您可能想要自定义的服务中定义的渲染器,

<html>

<head>
  <meta charset="utf-8" />
  <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
  <title>GeoJSONLayer - 4.15</title>

  <style>
    html,
    body,
    #viewDiv {
      padding: 0;
      margin: 0;
      height: 100%;
      width: 100%;
    }
  </style>

  <link rel="stylesheet" href="https://js.arcgis.com/4.15/esri/themes/light/main.css" />
  <script src="https://js.arcgis.com/4.15/"></script>

  <script>
    require([
      "esri/Map",
      "esri/layers/GeoJSONLayer",
      "esri/views/MapView",
      "esri/widgets/Legend",
      "esri/renderers/support/jsonUtils"
    ], function (Map, GeoJSONLayer, MapView, Legend, rendererJsonUtils) {
      const url =
"https://services9.arcgis.com/RHVPKKiFTONKtxq3/arcgis/rest/services/USGS_Seismic_Data_v1/FeatureServer/1/query?where=1=1&geometry=-122.40,37.76&geometryType=esriGeometryPoint&inSR=4326&distance=100&units=esriSRUnit_StatuteMile&outFields=*&returnGeometry=true&geometryPrecision=4&outFields=*&f=pgeojson";

      const template = {
        title: "Shake Intensity",
        content: [
          {
            type: "fields",
            fieldInfos: [
            {
                fieldName: "grid_value",
                label: "Grid Value"
              },
              {
                fieldName: "mag",
                label: "Magnitude"
              },
              {
                fieldName: "eventTime",
                label: "Event Time",
                format: {
                  dateFormat: "short-date-short-time"
                }
              },
              {
                fieldName: "updated",
                label: "Updated",
                format: {
                  dateFormat: "short-date-short-time"
                }
              },
              {
                fieldName: "url",
                label: "Url"
              }
            ]
          }
        ]
      };
      const rendererJSON = {
        "field": "grid_value",
        "classificationMethod": "esriClassifyManual",
        "classBreakInfos": [
          {
            "classMaxValue": 1.9999,
            "symbol": {
              "color": [
                0,
                0,
                0,
                0
              ],
              "style": "esriSFSSolid",
              "type": "esriSFS",
              "outline": {
                "color": [
                  0,
                  0,
                  0,
                  0
                ],
                "width": 0.4,
                "style": "esriSLSSolid",
                "type": "esriSLS"
              }
            },
            "description": "",
            "label": "I (Not Felt)"
          },
          {
            "classMaxValue": 3.9999,
            "symbol": {
              "color": [
                0,
                0,
                0,
                0
              ],
              "style": "esriSFSSolid",
              "type": "esriSFS",
              "outline": {
                "color": [
                  0,
                  0,
                  0,
                  0
                ],
                "width": 0.4,
                "style": "esriSLSSolid",
                "type": "esriSLS"
              }
            },
            "description": "",
            "label": "II - III (Weak)"
          },
          {
            "classMaxValue": 4.9999,
            "symbol": {
              "color": [
                140,
                250,
                230,
                255
              ],
              "style": "esriSFSSolid",
              "type": "esriSFS",
              "outline": {
                "color": [
                  0,
                  0,
                  0,
                  0
                ],
                "width": 0.4,
                "style": "esriSLSSolid",
                "type": "esriSLS"
              }
            },
            "description": "",
            "label": "IV (Light)"
          },
          {
            "classMaxValue": 5.9999,
            "symbol": {
              "color": [
                140,
                250,
                140,
                255
              ],
              "style": "esriSFSSolid",
              "type": "esriSFS",
              "outline": {
                "color": [
                  0,
                  0,
                  0,
                  0
                ],
                "width": 0.4,
                "style": "esriSLSSolid",
                "type": "esriSLS"
              }
            },
            "description": "",
            "label": "V (Moderate)"
          },
          {
            "classMaxValue": 6.9999,
            "symbol": {
              "color": [
                255,
                220,
                20,
                255
              ],
              "style": "esriSFSSolid",
              "type": "esriSFS",
              "outline": {
                "color": [
                  0,
                  0,
                  0,
                  0
                ],
                "width": 0.4,
                "style": "esriSLSSolid",
                "type": "esriSLS"
              }
            },
            "description": "",
            "label": "VI (Strong)"
          },
          {
            "classMaxValue": 7.9999,
            "symbol": {
              "color": [
                255,
                180,
                0,
                255
              ],
              "style": "esriSFSSolid",
              "type": "esriSFS",
              "outline": {
                "color": [
                  0,
                  0,
                  0,
                  0
                ],
                "width": 0.4,
                "style": "esriSLSSolid",
                "type": "esriSLS"
              }
            },
            "description": "",
            "label": "VII (Very Strong)"
          },
          {
            "classMaxValue": 8.9999,
            "symbol": {
              "color": [
                255,
                120,
                20,
                255
              ],
              "style": "esriSFSSolid",
              "type": "esriSFS",
              "outline": {
                "color": [
                  0,
                  0,
                  0,
                  0
                ],
                "width": 0.4,
                "style": "esriSLSSolid",
                "type": "esriSLS"
              }
            },
            "description": "",
            "label": "VIII (Severe) "
          },
          {
            "classMaxValue": 9.9999,
            "symbol": {
              "color": [
                255,
                0,
                0,
                255
              ],
              "style": "esriSFSSolid",
              "type": "esriSFS",
              "outline": {
                "color": [
                  0,
                  0,
                  0,
                  0
                ],
                "width": 0.4,
                "style": "esriSLSSolid",
                "type": "esriSLS"
              }
            },
            "description": "",
            "label": "IX (Violent)"
          },
          {
            "classMaxValue": 12,
            "symbol": {
              "color": [
                143,
                0,
                0,
                255
              ],
              "style": "esriSFSSolid",
              "type": "esriSFS",
              "outline": {
                "color": [
                  0,
                  0,
                  0,
                  0
                ],
                "width": 0.4,
                "style": "esriSLSSolid",
                "type": "esriSLS"
              }
            },
            "description": "",
            "label": "X+ (Extreme)"
          }
        ],
        "type": "classBreaks",
        "minValue": 0
      };
      const renderer = rendererJsonUtils.fromJSON(rendererJSON);

      const geojsonLayer = new GeoJSONLayer({
        url: url,
        title: "MMI",
        copyright: "USGS Earthquakes",
        popupTemplate: template,
        renderer
      });

      const map = new Map({
        basemap: "gray",
        layers: [geojsonLayer]
      });

      const view = new MapView({
        container: "viewDiv",
        center: [-122, 37],
        zoom: 6,
        map: map
      });
      view.ui.add(
        new Legend({
          view: view
        }),
        "bottom-left"
      );
    });
  </script>
</head>

<body>
  <div id="viewDiv"></div>
</body>

</html>

最后一件事,我不太清楚您为什么要查询和使用GeoJSONLayer,请记住,您可以使用FeatureLayer它实际上支持许多查询操作,并且您不需要获取它为您执行的 json 数据。


推荐阅读