首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ArcGIS 3.x到4.x迁移

ArcGIS 3.x到4.x迁移
EN

Stack Overflow用户
提问于 2018-11-12 10:27:54
回答 2查看 642关注 0票数 0

我当时使用的是ArcGIS JS 3.10,并有一个Github存储库在地图上显示GeoJSON数据,但现在我必须升级到4.9版本,我读取3.x到4.x迁移文档,由ESRI发布,并应用诸如链接位置等方面的更改,但它已经不再工作了。

代码语言:javascript
复制
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=7,IE=9">
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
<title>ArcGIS GeoJSON Layer</title>
<!-- ArcGIS API for JavaScript CSS-->
<link rel="stylesheet" href="https://js.arcgis.com/4.9/esri/css/main.css">
@*<link rel="stylesheet" href="//js.arcgis.com/3.9/js/esri/css/esri.css">*@
<!-- Web Framework CSS - Bootstrap (getbootstrap.com) and Bootstrap-map-js (github.com/esri/bootstrap-map-js) -->
<link rel="stylesheet" href="//netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css">
<link rel="stylesheet" href="//esri.github.io/bootstrap-map-js/src/css/bootstrapmap.css">
<style>
    html, body, #mapDiv {
        height: 500px;
        width: 100%;
    }
</style>

<script src="https://js.arcgis.com/4.9/"></script>
<!-- ArcGIS API for JavaScript library references -->
@*<script src="//js.arcgis.com/3.10"></script>*@

<!-- Terraformer reference -->
<script src="/vendor/terraformer/terraformer.min.js"></script>
<script src="/vendor/terraformer-arcgis-parser/terraformer-arcgis-parser.min.js"></script>

<script>
    require(["esri/Map",
        "/Scripts/Refine.js",
        "dojo/on",
        "dojo/dom",
        "dojo/domReady!"],
        function (Map, GeoJsonLayer, on, dom) {

            // Create map
            var map = new Map("mapDiv", {
                basemap: "gray",
                center: [-122.5, 45.5],
                zoom: 5
            });

            map.on("load", function () {
                addGeoJsonLayer("http://113.197.55.251/api/punjab");
            });

            // Add the layer
            function addGeoJsonLayer(url) {
                // Create the layer
                var geoJsonLayer = new GeoJsonLayer({
                    url: url
                });
                // Zoom to layer
                geoJsonLayer.on("update-end", function (e) {
                    map.setExtent(e.target.extent.expand(1.2));
                });
                // Add to map
                map.add(geoJsonLayer);
            }
        });
</script>
</head>
<body>
<div id="mapDiv"></div>
</body>
</html>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-11-12 13:33:35

在ArcGIS 4.9中,您必须使用如下所示的MapView

对于GeoJson和EsriJson之间的转换,我建议您使用arcgis-to-geojson-utils库。

在html:中导入库

代码语言:javascript
复制
<script src="https://unpkg.com/@esri/arcgis-to-geojson-utils"></script>

javascript:

代码语言:javascript
复制
require([
    "esri/Map",
    "esri/views/MapView",
    "esri/Graphic",
    "esri/layers/FeatureLayer",
    "esri/layers/support/Field",
    "dojo/on",
    "dojo/dom",
    "dojo/domReady!"],
    function (Map, MapView, Graphic, FeatureLayer, Field, on, dom) {

        // Create mapView and map
        var mapView = new MapView({
            container: mapDiv,
            map: new Map({
                basemap: "gray"
            }),
            center: [-122.5, 45.5],
            zoom: 5
        }).when(function(mapView) {
            makeRequest("http://113.197.55.251/api/punjab", function(response) {
                createLayerFromGeoJSON(response, mapView);
            });
        });

        // Request the geojson data using XmlHttpRequest
        function makeRequest(url, callback) {
            var xhr = new XMLHttpRequest();
            xhr.open("GET", url, true);
            xhr.setRequestHeader("Content-type", "application/json; charset=utf-8");
            xhr.onreadystatechange = function() {
                if (xhr.readyState == 4 && xhr.status == 200) {
                    callback(JSON.parse(xhr.response));
                }
            };
            xhr.onerror = function(error) {
              throw error;
            }
            xhr.send(); 
        };

        // Create the layer from the geojson data
        function createLayerFromGeoJSON(geojson, mapView) {

            // Convert geojson to esriJson using arcgis-to-geojson-utils library
            var esriJson = ArcgisToGeojsonUtils.geojsonToArcGIS(geojson);

            // Create an array of graphics from the esriJson
            var graphics = esriJson.map(function(feature, i) {
                return new Graphic({
                    geometry: {
                        type: "polygon",
                        rings: feature.geometry.rings
                    },
                    attributes: {
                        ObjectID: i,
                        Name: feature.attributes.Name
                    }
                 });
            });

            // Create a FeatureLayer from the graphics
            var layer = new FeatureLayer({
                title: "My feature layer",
                source: graphics, // autocast as an array of esri/Graphic
                geometryType: "polygon",
                fields: [
                    new Field({
                        name: "ObjectID",
                        alias: "ObjectID",
                        type: "oid"
                    }),
                    new Field({
                        name: "Name",
                        alias: "Name",
                        type: "string"
                    }),
                ], 
                objectIdField: "ObjectID", // This must be defined when creating a layer from Graphics
                renderer: {
                    type: "simple",  // autocasts as new SimpleRenderer()
                    symbol: {
                        type: "simple-fill",  // autocasts as new SimpleFillSymbol()
                        color: {r: 200, g: 200, b: 200, a: 0.5},
                        outline: {  // autocasts as new SimpleLineSymbol()
                            width: 0.5,
                            color: "black"
                        }
                    }
                }
            });
            mapView.map.add(layer);
            return layer;
        };

    });
票数 0
EN

Stack Overflow用户

发布于 2018-11-12 13:33:26

在ArcGis JS4.x版本中,还必须声明视图构造函数(MapView用于2D,SceneView用于3D)。下面是关于如何设置2D视图的指南:https://developers.arcgis.com/javascript/latest/sample-code/intro-mapview/index.html

添加Json文件的另一种方法是将现有的json文件转换为esri格式,如本指南所示:https://developers.arcgis.com/javascript/latest/sample-code/sandbox/index.html?sample=layers-featurelayer-collection

因为我不知道您的json文件属性,所以我不能提供一个工作示例,但它非常简单。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53260223

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档