Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
  <title>ArcGIS API for JavaScript Tutorials: Add layers to a map</title>
  <style>
    html, body, #viewDiv {
      padding: 0;
      margin: 0;
      height: 100%;
      width: 100%;
    }
		::-webkit-scrollbar {
		  width: 12px;
		}
		::-webkit-scrollbar-track {
		  background: #d0d0d0; 
		}
		::-webkit-scrollbar-thumb {
		  background: rgba(0,0,0,0.5); 
		  border-radius: 4px;
		}
		::-webkit-scrollbar-thumb:hover {
		  background: #555;
		}
		#infoDiv {
		  position: absolute;
		  top: 15px;
		  left: 60px;
		}
		#infoDiv input {
		  border: none;
		  box-shadow: rgba(0, 0, 0, 0.3) 0px 1px 2px;
		}
	</style><script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
	<link rel="stylesheet" href="https://js.arcgis.com/4.19/esri/css/main.css" />
  </head>
<body>
  <div id="viewDiv"></div>
  <div id="infoDiv">
    <input
        class="esri-component esri-widget--button esri-widget esri-interactive"
        type="button"
        id="view-switch-btn"
        value="3D"
      />
 </div>
</body>
</html> 
              
            
!

CSS

              
                
              
            
!

JS

              
                function loadScript(src, callback) {
	'use strict';
	var script = document.createElement('script');
	script.type = 'text/javascript';
	script.src = src;
	script.onreadystatechange = function () {
		if (this.readyState === 'complete' || this.readyState === 'loaded') {
			callback();
		}
	};
	script.onload = callback;
	var scriptTag = document.getElementsByTagName('script')[0];
	scriptTag.parentNode.insertBefore(script, scriptTag);
}

loadScript('https://js.arcgis.com/4.19/', function () {
	onLoadGis()
})

function onLoadGis() {
    require([
          "esri/Map",
        "esri/views/MapView",
        "esri/layers/GraphicsLayer",
        "esri/geometry/SpatialReference",
        "esri/Graphic",
        "esri/symbols/SimpleFillSymbol",
        "esri/geometry/projection",
        "esri/symbols/SimpleLineSymbol",
        "esri/geometry/Polygon",
        "esri/geometry/Point",
        "esri/geometry/Polyline",
        "esri/geometry/geometryEngine",
        "esri/geometry/support/webMercatorUtils",
        "esri/widgets/Sketch",
        "esri/layers/WMSLayer",
        "esri/tasks/GeometryService",
        "esri/widgets/BasemapToggle",
        "esri/geometry/Extent",
        "esri/PopupTemplate",
        "esri/views/SceneView", 
        "esri/WebMap", 
        "esri/WebScene"
      ], function ( Map, MapView, GraphicsLayer, SpatialReference, Graphic, SimpleFillSymbol, Projection, SimpleLineSymbol, Polygon, Point, Polyline, geometryEngine, webMercatorUtils, Sketch, WMSLayer, GeometryService, BasemapToggle, Extent, PopupTemplate, SceneView, WebMap, WebScene) {

     var address = 'Whitebridge Farm, Ely Road, CB6 1RT';
		 var lng = "-3.118092";
		 var lat = "54.509865";
      if (address != "") {
        $.ajax({
          url: "https://geocode.arcgis.com/arcgis/rest/services/World/GeocodeServer/findAddressCandidates?f=json&singleLine=" + address + "&outFields=Match_addr,Addr_type",
          async: false,
          success: function (result) {
            lng = result.candidates[0].location.x;
            lat = result.candidates[0].location.y;
          }
        });
      }
      const viewLayer = new GraphicsLayer({
        id: "viewLayer",
        title: "viewLayer",
       });
      var map = new Map({
        basemap: "streets",
			  layers: [viewLayer]
      });
      var appConfig = {
		  	activeView: null,
		  	container: "viewDiv"
		  }; 
     var initialViewParams = {
			container: appConfig.container,
			zoom: 17,
			center: [lng, lat],
			map: null,
		 };
     initialViewParams.map = map;
     const view = createView(initialViewParams, "2d");
     appConfig.activeView = view;
			var scene = new WebScene({
			basemap: "streets",
			});
	   initialViewParams.container = null;
	   initialViewParams.map = scene;
	   const sceneView = createView(initialViewParams, "3d");
	   function createView(params, type) {
			 var view;
			 var is2D = type === "2d";
			 if (is2D) {
				 view = new MapView(params);
			 } else {
				 view = new SceneView(params);
			 }
			 return view;
		 } 
		 var switchButton = document.getElementById("view-switch-btn");
       switchButton.addEventListener("click", function() {
       switchView();
		 });
		 function switchView() {
       var is3D = appConfig.activeView.type === "3d";
       var activeViewpoint = appConfig.activeView.viewpoint.clone();
       appConfig.activeView.container = null;
       if (is3D) {
         view.viewpoint = activeViewpoint;
         view.container = appConfig.container;
         appConfig.activeView = view;
         switchButton.value = "3D";
         map.layers = scene.layers
       } else {
         sceneView.viewpoint = activeViewpoint;
         sceneView.container = appConfig.container;
         appConfig.activeView = sceneView;
         switchButton.value = "2D";
         scene.layers = map.layers
       }
		 }
      var basemapToggle = new BasemapToggle({
	        view: view,
	        nextBasemap: "satellite"
		});
		view.ui.add(basemapToggle, "top-left");
      
      function initialize() {
        mainFieldPolygons = [];
        var mainFieldPolygonsTemp = JSON.parse('[{"id":"1_1","labelName":"16: Test7547654","spatialReference":{"wkid" : 102100}, "rings": [[[16974.516072577,6879683.556583529],[16974.516072577,6887021.511298897],[24312.470787944,6887021.511298897],[24312.470787944,6879683.556583529],[16974.516072577,6879683.556583529]]],"type":"Polygon","layerName":"Boundary","Area":"NONE","color":"#FF0000","outline":1,"pattern":"None","cropRotation":0,"isDrilled":false,"visible":true,"isBoundaryLayer":true}]');
        var allLong = [];
        var allLat = [];

        for(var i = 0; i < mainFieldPolygonsTemp.length; i++) {
          if(mainFieldPolygonsTemp[i]) {
            if (mainFieldPolygonsTemp[i].rings) {
              var cordinatesLength = mainFieldPolygonsTemp[i].rings.length;
              const color = mainFieldPolygonsTemp[i].color;
              const labelName = mainFieldPolygonsTemp[i].labelName
              var isBoundaryLayer = mainFieldPolygonsTemp[i].isBoundaryLayer;
              var graphic;

              if(mainFieldPolygonsTemp[i].type == "Polygon"){
                const newPattern = mainFieldPolygonsTemp[i].pattern;
                const outline = mainFieldPolygonsTemp[i].outline;
                var polygon = new Polygon(mainFieldPolygonsTemp[i]);
                let pattern = "none";
                if(newPattern == 'Cross') {
                  pattern = "cross" ;
                } else if(newPattern == 'Hatch') {
                  pattern = "horizontal" ;
                } else if(newPattern == 'Dots') {
                  pattern = "vertical" ;
                } else if(newPattern == 'None' && outline == 2){
                  pattern = "solid";
                }
                var layerDrawFillSymbol = new SimpleFillSymbol({
                  color: color,
                  outline: {
                    type: "simple-line",
                    color: color,
                    width: "2px",
                    style: "solid"
                  },
                  style: pattern
                }) ;
                graphic = new Graphic(polygon, layerDrawFillSymbol);
              } else if(mainFieldPolygonsTemp[i].type == "LineString"){
                var polyline = new Polyline({
                    type: mainFieldPolygonsTemp[i].type,
                    paths: mainFieldPolygonsTemp[i].rings,
                    spatialReference: mainFieldPolygonsTemp[i].spatialReference
                });
                const simpleLineSymbol = {
                    type: "simple-line",
                    color: color,
                    width: '2px'
                 };

                graphic = new Graphic({
                  geometry: polyline,
                    symbol: simpleLineSymbol
                });
              } else if(mainFieldPolygonsTemp[i].type == "Point"){
                var point = new Point({
                    type: mainFieldPolygonsTemp[i].type,
                    x: mainFieldPolygonsTemp[i].rings[0],
                    y: mainFieldPolygonsTemp[i].rings[1],
                    spatialReference: mainFieldPolygonsTemp[i].spatialReference
                });
                graphic = new Graphic({
                  geometry: point,
                    symbol: {
                      type: "simple-marker",
                    color: color,
                    size: '8px',
                    outline: {
                        width: 0
                    }
                    }
                });
              }

              graphic.attributes = {
                  polygonId: mainFieldPolygonsTemp[i].id,
                  layerName: mainFieldPolygonsTemp[i].layerName,
                  color: color,
                  outline: mainFieldPolygonsTemp[i].outline,
                  pattern: mainFieldPolygonsTemp[i].pattern,
                  isBoundary: mainFieldPolygonsTemp[i].isBoundaryLayer, 
                  labelName: labelName,
                  Area: mainFieldPolygonsTemp[i].Area,
                  label: null,
                  cropRotation: mainFieldPolygonsTemp[i].cropRotation,
                  isDrilled: mainFieldPolygonsTemp[i].isDrilled,
                  soil: '',
                  subsoil: '',
                  gridVisible: mainFieldPolygonsTemp[i].id.startsWith("Grid") ? mainFieldPolygonsTemp[i].visible : true
                }

              graphic.polygonId = mainFieldPolygonsTemp[i].id;
              if(graphic.polygonId.startsWith("Grid")){
                graphic.visible = false;
              } else {
                graphic.visible = mainFieldPolygonsTemp[i].visible;
              }
              if(mainFieldPolygonsTemp[i].isBoundaryLayer) {
                view.extent = graphic.geometry.extent;
                if(view.zoom > 0){
                  view.zoom = view.zoom - 1;
                }
              }

              if(mainFieldPolygonsTemp[i].isBoundaryLayer) {
                mainFieldGraphic = graphic;
              }
              mainFieldPolygons.push(graphic) ;
              viewLayer.add(graphic);
              console.log(graphic);
            }
          }
        }
      }
      onLoadGis.initialize = initialize;
      initialize();
  });
}
              
            
!
999px

Console