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

              
                <!DOCTYPE html>
<html>
<head>
<title>Hosted Feature layer</title>
<link rel="stylesheet" href="https://js.arcgis.com/4.9/esri/css/main.css"><style>
html,body,#viewDiv {
	padding:0px;
	margin:0px;
	width:100%;
	height:100%;
}
</style>
<script src="https://js.arcgis.com/4.9/"></script>

<script>

var query = null;

var myMap=null, view=null, lyStates=null;
var lyGraphics = null;
var sketchViewModel = null;
var highlightHandler = null;


require([
	"esri/Basemap",
	"esri/WebScene",
	"esri/layers/TileLayer",
    "esri/layers/SceneLayer",
    "esri/layers/FeatureLayer",
    "esri/layers/GraphicsLayer",
	"esri/Map",
	"esri/views/MapView",
	"esri/views/SceneView",
	"esri/layers/IntegratedMeshLayer",
	"esri/widgets/Compass",
	"esri/widgets/Home",
	"esri/tasks/GeometryService",
	"esri/tasks/support/ProjectParameters",
	"esri/geometry/SpatialReference",
	
    "esri/tasks/QueryTask",
    "esri/tasks/support/Query",
	"esri/widgets/Sketch/SketchViewModel",
	
	"esri/Graphic",
    "esri/geometry/Point",
    "esri/geometry/Polyline",
    "esri/geometry/Polygon",
    "esri/symbols/SimpleMarkerSymbol",
    "esri/symbols/SimpleLineSymbol",
    "esri/symbols/SimpleFillSymbol",

	"esri/config",
	"dojo/on",
	"dojo/dom",
	"dojo/domReady!"
],
function (Basemap, WebScene, TileLayer, SceneLayer, FeatureLayer, GraphicsLayer, 
		Map, MapView, SceneView, IntegratedMeshLayer, 
		Compass, Home, 
		GeometryService, ProjectParameters, SpatialReference,
		QueryTask, Query,
		SketchViewModel,
		Graphic, Point, Polyline, Polygon,
	    SimpleMarkerSymbol, SimpleLineSymbol, SimpleFillSymbol,
		esriConfig, on, dom) {
	
	try {
		createSgWorld();
	}
	catch(e) {
		alert("Error:" + e)
		return;
	}
	
	function createSgWorld() {
	    var renderer = {
	    	type: "simple", 
			symbol: {
				type: "simple-fill",
				color: "orange",
				outline: { // autocasts as new SimpleLineSymbol()
					color: [255, 255, 0],
					width: 4 // points
				}
			}
	    };
	    
	    var lyStates = new FeatureLayer({
	      	url: "https://services.arcgis.com/P3ePLMYs2RVChkJx/ArcGIS/rest/services/USA_States_Generalized/FeatureServer/0",
	      	renderer: renderer,
	      	opacity:1,  	
	      	popupTemplate: { // autocasts as new PopupTemplate()
	        	title: "{STATE_NAME}",
	        	content: [{
	          		type: "fields",
	          		fieldInfos: [
	          			{fieldName: "FID", label: "FID"},
	          			{fieldName: "POPULATION", label: "Population"}
	            	]
	        	}]
	      	},
	      	elevationInfo:{
				mode:"relative-to-ground",
				offset:10000
	    	},
	      	outFields: ["*"]
	    });
	    
	    lyGraphics = new GraphicsLayer({
	    	id: "tempGraphics",
	    	elevationInfo:{
	    		mode:"relative-to-ground",
	    		offset:31000
	    	}
	    });
	    
	    myMap = new Map({
	        //basemap: "satellite",
	        basemap: "dark-gray",
	        //ground: "world-elevation",
	        layers: [lyStates, lyGraphics]
		});

	  	view = new SceneView({
	    	center: [-96.240234, 40.178873],	    		
	    	container: "viewDiv",
	    	map: myMap,
	    	zoom: 6
// 	    	highlightOptions: {
// 	            color:"#91ff94",
// 	            haloOpacity: 1,
// 	            fillOpacity: 0.5
// 	        }
		});	
	  	
	    var homeBtn = new Home({
	        view: view
	    });
	    view.ui.add("plBtn1", "top-left");

	  	view.when(function() {
	  		const polygonSymbol = {
	  	      	type: "simple-fill", // autocasts as new SimpleFillSymbol()
	  	        color: "rgba(138,43,226, 0.8)",
	  	        style: "solid",
	  	        outline: {
	  	        	color: "white",
	  	          	width: 1
	  	        }
	  		};
	    	var sketchViewModelParam = {
               	view:view,
               	layer: lyGraphics,
               	polygonSymbol: polygonSymbol
        	};
	        sketchViewModel = new SketchViewModel(sketchViewModelParam);
	        setUpClickHandler();
	        // Listen to create-complete event to add a newly created graphic to view
	        sketchViewModel.on("create-complete", addGraphic);

	        // Listen the sketchViewModel's update-complete and update-cancel events
	        sketchViewModel.on("update-complete", updateGraphic);
	        sketchViewModel.on("update-cancel", updateGraphic);

	        on(dom.byId("btn1"), "click", sumByPologonSelection);
	        	
	        // called when sketchViewModel's create-complete event is fired.
	        function addGraphic(event) {
	        	// create-complete event geometry.
	        	var g = event.geometry; 	
	        	
	        	const graphic = new Graphic({
	        		geometry: g,
	        		symbol: sketchViewModel.graphic.symbol
	        	});
	        	lyGraphics.add(graphic);	        	
	        	
	        	var query = lyStates.createQuery();
	        	query.where = "";
	        	query.geometry = g;
	        	query.spatialRelationship = "intersects";
	        	lyStates.queryFeatures(query).then(function(results) {
	        		if(highlightHandler!=null) {
	        			highlightHandler.remove();
	        		}
	        		
	        		view.whenLayerView(lyStates).then(function(layerView) {
		        		highlightHandler = layerView.highlight(results.features);
		        		var s = "";
		        		var i;
		        		for(i=0;i<results.features.length;i++) {
		        			if(i==0) {
		        				s = results.features[i].attributes["STATE_NAME"];
		        			}
		        			else {
		        				s = s + ", " + results.features[i].attributes["STATE_NAME"];
		        			}
		        		}
		        		
		        		view.popup.open({
		        			title:'title',
		        			location:view.center,
		        			content:'select states count: ' + results.features.length + " " + s
		        		});
	        		});
	        	});	 
	        }

	        // Runs when sketchViewModel's update-complete or update-cancel
	        // events are fired.
	        function updateGraphic(event) {
	        	/*
	        	// Create a new graphic and set its geometry event.geometry
	        	var graphic = new Graphic({
	        		geometry: event.geometry,
	        		symbol: editGraphic.symbol
	        	});
	        	graphicsLayer.add(graphic);

	        	// set the editGraphic to null update is complete or cancelled.
	        	editGraphic = null;
	        	*/
	        }

	        // set up logic to handle geometry update and reflect the update on "graphicsLayer"
	        function setUpClickHandler() {
	        	view.on("click", function (event) {
	        		view.hitTest(event).then(function (response) {
	        			/*
	        			var results = response.results;
	        			if (results.length > 0) {
	        				for (var i = 0; i < results.length; i++) {
	        					// Check if we're already editing a graphic
	        					if (!editGraphic && results[i].graphic.layer.id === "tempGraphics") {
	        						// Save a reference to the graphic we intend to update
	        						editGraphic = results[i].graphic;
	    
	        						// Remove the graphic from the GraphicsLayer
	        						// Sketch will handle displaying the graphic while being updated
	        						graphicsLayer.remove(editGraphic);
	        						sketchViewModel.update(editGraphic);
	        						break;
	        					}
	        				}
	        			}
	        			*/
	        		});
	        	});
	        }
	  	});
	} // end createSgWorld
	
	function sumByPologonSelection(evt) {
		if(sketchViewModel==null) {
			throw new Error("sketchViewModel null");
		}
		sketchViewModel.create("polygon");
	}

});

</script>
</head>

<body>

<!-- Main content -->
<div id="viewDiv" style="">
	<div id="plBtn1">
		<button id="btn1">Select by Polygon</button>
	</div>
</div>

</body>
</html>

              
            
!

CSS

              
                
              
            
!

JS

              
                
              
            
!
999px

Console