Select to view content in your preferred language

Feature Widget access multiple layers with PortalItemID

2823
12
Jump to solution
06-18-2019 06:46 AM
MarquesMunson1
Regular Contributor

I am using JavaScript 4.11 and I am trying to get the Feature Widget to access the popupTemplate for each layer that is under the same portalItem ID. So far the widget can only access one of the layers. Is there a way to write the code to have the widget access all of the items? I have the code below.

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

<title>ArcGIS API for JavaScript Adoption Statistics Prototype</title>

<style>
html, body, #viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
/*******STYLE FOR THE HOVER FEATURE */
.esri-feature {
letter-spacing: 0em;
font-family: "Oswald", sans-serif;
line-height: 1.55rem;
font-feature-settings: "liga" 1, "calt" 0;
background: fff;
padding: 1em;
}

</style>

<!------------REFERECING ESRI CDN FOR JAVASCRIPT 4.11------------->
<link rel="stylesheet" href="https://js.arcgis.com/4.11/esri/css/main.css">
<script src="https://js.arcgis.com/4.11/"></script>

<script>
require([
"esri/Map",
"esri/views/MapView",
"esri/layers/FeatureLayer", //Adding feature layer to the application using FeatureLayer module
"esri/widgets/Legend", //Adding Legend to the application
"esri/widgets/LayerList",
"esri/widgets/Feature"
], function(Map, MapView, FeatureLayer, Legend, LayerList, Feature) { //functions must match the require statements

/***************SETTING THE BASEMAP*********** */
var map = new Map({
basemap: "dark-gray-vector", //specify the type of basemap (gray, topo, streets, dark-gray, etc)
});

/*************** CONTAINER FOR THE ACTUAL MAP WITH DIMENSIONS*********** */

var view = new MapView({
container: "viewDiv",
map: map,
center: [-25.432,34.09042], //longitude, latitude--- this is the center location of the map
zoom: 2 //zoom level, lower number = more zoomed out
});

/*************** ADDING LAYERS TO THE MAP*********** */
var adoptionstats = new FeatureLayer({
portalItem: {
id: "e5683bb09bad438aae49eef2a8796296"
}
});


map.add(adoptionstats, 0); // the zero is for adding order, since only one layer technically do not need this



/***************ADD FEATURE POP UP HERE*************************/

view.when().then(function() {
var legend = new Legend({
view: view
});
view.ui.add(legend, "bottom-left");

var layerList = new LayerList({
view: view
});

// Add widget to the top right corner of the view
view.ui.add(layerList, "top-right");

// Create a default graphic for when the application starts
var graphic = {
popupTemplate: {
content: "Mouse over features to show details..."
}
};

// Provide graphic to a new instance of a Feature widget
var feature = new Feature({
graphic: graphic,
view: view
});

view.ui.add(feature, "bottom-right");

view.whenLayerView(adoptionstats).then(function(layerView) {
let highlight;
// listen for the pointer-move event on the View
view.on("pointer-move", function(event) {
// Perform a hitTest on the View
view.hitTest(event).then(function(event) {
// Make sure graphic has a popupTemplate
let results = event.results.filter(function(result) {
return result.graphic.layer.popupTemplate;
});
let result = results[0];
highlight && highlight.remove();
// Update the graphic of the Feature widget
// on pointer-move with the result
if (result) {
feature.graphic = result.graphic;
highlight = layerView.highlight(result.graphic);
} else {
feature.graphic = graphic;
}
});
});
});
});

/****************END FEATURE POP UP HERE */


});
</script>
</head>

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

</html>
0 Kudos
12 Replies
MarquesMunson1
Regular Contributor

perfect! I already played around with it to see what I can do and it works!

DavidWilson3
Regular Contributor

Glad to hear I was helpful, if you have further questions on this application of yours just let me know and I can see if I can help!

0 Kudos
MarquesMunson1
Regular Contributor

I definitely will! My next step will be to add some interactive charts

0 Kudos