使用javascript 4.15 api无法使用来自webapi核心3.1其余部分的geojson收集功能创建要素图层

问题描述 投票:0回答:1

请注意,我能够使用geojson数据并轻松地在传单地图上创建一个图层。另外,尝试使用lib arcgis-to-geojson-utils,但无法使其正常工作。这是我的代码。非常感谢您的帮助。

view.when()
        .then(fetchData)
        .then(createGraphics)
        .then(createLayer)
        .then(addToView)
        .catch(function(e) {
                console.error("Creating FeatureLayer   failed", e);
        })

 function fetchData() {
    return  fetch(url,options)
    .then(response =>   {
      return response.json()
    });       
}

 function createGraphics(schoolsGeoData) {
  const geoData = JSON.parse(schoolsGeoData);
  return geoData.features.map((school, i) => {
        let schoolAttr = {
                    OBJECTID: school.properties["id"],
                    name: school.properties["name"]
        }
            return new Graphic({
                      //  type: "point",
                        attributes: schoolAttr,
                        geometry: new Point({
                          x: school.geometry.coordinates[0],
                          y: school.geometry.coordinates[1]
                        }),
                     });
           })
}

 function createLayer(graphics) {
   return new FeatureLayer({
            source: graphics,
            objectIdField: "OBJECTID",
            fields: schoolFeilds(),
            popupTemplate: schoolTemplate(),
            renderer: myRenderer(),
            geometryType: "point" ,
            spatialReference: {
              wkid: 4326
            },
        });
}

 function addToView(layer) {
  if(layer) {
     view.map.add(layer);
   }
}
esri arcgis-js-api
1个回答
0
投票
您的代码具有合理的逻辑,当然缺少一些东西,我不知道它们是否在起作用。例如,我不知道您为fetch操作添加了哪些选项,但是默认情况下它将为您正确获取数据,并且您不必转换为JSON,因为它可以实现这一点。

我实际上是想问您更多的代码,或者究竟是什么问题,但是我决定提醒您,您实际上有一个特定的层来处理GeoJSON数据,GeoJSONLayer

  • 但是,无论如何,我将在这里为您要执行的操作提供一个运行中的示例,使用我提到的示例中的数据,获取数据并使用FeatureLayer

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" /> <title>GeoJSON with FeatureLayer - 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/views/MapView", "esri/layers/FeatureLayer", "esri/Graphic" ], function(Map, MapView, FeatureLayer, Graphic) { const map = new Map({ basemap: "gray" }); const view = new MapView({ center: [-80, 35], zoom: 8, map, container: "viewDiv" }); view.when() .then(fetchData) .then(createGraphics) .then(createLayer) .then(addToView) .catch(function(e) { console.error("Creating FeatureLayer failed", e); }) function fetchData() { return fetch( 'https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_month.geojson' ) .then(response => { return response.json() }); } function createGraphics(data) { // const geoData = JSON.parse(data); return data.features.map((feature, i) => { return new Graphic({ attributes: { OBJECTID: i, title: feature.properties["title"], mag: feature.properties["mag"], type: feature.properties["type"], place: feature.properties["place"], time: feature.properties["time"] }, geometry: { type: "point", x: feature.geometry.coordinates[0], y: feature.geometry.coordinates[1] }, }); }) } function createLayer(graphics) { const popupTemplate = { title: "{title}", content: "Magnitude {mag} {type} hit {place} on {time}", fieldInfos: [ { fieldName: "time", format: { dateFormat: "short-date-short-time" } } ], outFields: ["title", "mag", "type", "place", "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: "10px" }, { value: 8, size: "40px" } ] } ] }; return new FeatureLayer({ source: graphics, fields: [{ name: "OBJECTID", alias: "ObjectID", type: "oid" }, { name: "title", alias: "Title", type: "string" } , { name: "mag", alias: "Magnitude", type: "double" } , { name: "type", alias: "Type", type: "string" }, { name: "place", alias: "Place", type: "string" }, { name: "time", alias: "Time", type: "date" }], objectIdField: "OBJECTID", popupTemplate, renderer }); } function addToView(layer) { if(layer) { view.map.add(layer); } } }); </script> </head> <body> <div id="viewDiv"></div> </body> </html>
  • © www.soinside.com 2019 - 2024. All rights reserved.