Click Feature to Open URL

523
1
12-20-2020 07:51 PM
Keagan1
New Contributor

Hi all,

I'm trying to get a URL to open up from a link in an attribute field when  the feature is clicked on. I have added the following sample code taken from another post to MapManager.js and have gotten it to work with the sample links that came with it.

  1.   var featureLayer = new FeatureLayer("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Demographics/ESRI_Census_USA/MapServer/3");  
  2.   map.addLayer(featureLayer);  
  3.   
  4.   //Code for the specific URL to open in a new window  
  5.   featureLayer.on('click',function(e){   
  6.     var specific = e.graphic.attributes['SpecificAttribute']   
  7.     window.open("http://YourUrl.com/"+specific);  
  8.   });  

I don't want to create a new feature layer, I would like to use an existing layer - how do I alter the code to do this? Also, will this work on a point feature as well as a polygon? Because right now it doesn't work on point features.

I'm inexperienced with ArcGIS API for JavaScript but I'm trying to use online resources to find a solution so any help would be appreciated as I've spent a long time trying to get this to work!

0 Kudos
1 Reply
ChristianBischof
Esri Contributor

Try to get your existing feature layer with the following logic (let is just another forma of declaring a variable which I personally prefer, you can as well use var). And don't get confused of (e) => { //code } it means the same as function (e) { //code }

 

let myFeatureLayerTitle = 'TitleOfYourExistingFeatureLayer';
let myFeatureLayer = webmap.allLayers.find((layer) => {
   return layer.title === myFeatureLayerTitle;
});

 

If you don't know the title of your layer you can also use the ID property or something else which identifies the layer. Additionally you can put the var view variable into the global scope (put it before the API require statement require([ ... ] ..... ) so you can use it in your browser dev tools and access your layers with view.map.allLayers.items

 

0 Kudos