Labels in Service

169
6
Jump to solution
02-06-2019 09:54 AM
jaykapalczynski
Frequent Contributor

I am consuming this service for Railroad crossings.

When I put it in my app it renders the labels when the layer is turned off.

When I turn it on it still shows them.

I cant seem to turn them off....I want them to turn off when I turn the layer off.

Layer: Rail Crossings (ID: 0) 

Any thoughts?  I am trying to below but nothing seems to work.

window.RailroadCrossings = new FeatureLayer("https://fragis.fra.dot.gov/fragis/rest/services/FRAGradeXing/MapServer/0", {
	mode: FeatureLayer.MODE_ONDEMAND,
        showLabels: false,
        onStatusChanged: {
            if(status = Enums.LayerStatusInitialized) {
               enableLabels = false;
                   }  
	}, 
	infoTemplate: infoTemplateRailroadCrossings,
	id:"RailroadCrossing",
	definitionExpression: " StateCode='51' AND POSXING='1' "
});

I also tried to showLabels as false on the map itself but then no other labels show up...I have them true in this example

window.map = new Map("map", {
	basemap: "gray",
	center: [-77.980586, 37.566286],
	zoom: 15,
	logo: false,
	showLabels : true
});
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Esteemed Contributor

Jay,

   This simplified code works fine for me:

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

<link rel="stylesheet" href="https://js.arcgis.com/3.27/esri/css/esri.css">
<script src="https://js.arcgis.com/3.27/"></script>

<style>
html, body, #map {
  padding: 0;
  margin: 0;
  height: 100%;
  width: 100%;
}
</style>

<script>
require([
    "esri/map",
    "esri/layers/FeatureLayer",
    "dojo/domReady!"
  ],
  function(
    Map,
    FeatureLayer
  ) {

    var map = new Map("map", {
      basemap: "hybrid",
      center: [-79.383417, 36.581599],
      zoom: 11,
      showLabels : true
    });
    
    var RailroadCrossings = new FeatureLayer("https://fragis.fra.dot.gov/fragis/rest/services/FRAGradeXing/MapServer/0", {
      mode: FeatureLayer.MODE_ONDEMAND,
      showLabels: false,
      id:"RailroadCrossing",
      definitionExpression: "StateCode=51 AND POSXING='1'"
    });
    map.addLayer(RailroadCrossings);
  });
</script>
</head>

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

</html>

View solution in original post

0 Kudos
6 Replies
RobertScheitlin__GISP
MVP Esteemed Contributor

Jay,

   This simplified code works fine for me:

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

<link rel="stylesheet" href="https://js.arcgis.com/3.27/esri/css/esri.css">
<script src="https://js.arcgis.com/3.27/"></script>

<style>
html, body, #map {
  padding: 0;
  margin: 0;
  height: 100%;
  width: 100%;
}
</style>

<script>
require([
    "esri/map",
    "esri/layers/FeatureLayer",
    "dojo/domReady!"
  ],
  function(
    Map,
    FeatureLayer
  ) {

    var map = new Map("map", {
      basemap: "hybrid",
      center: [-79.383417, 36.581599],
      zoom: 11,
      showLabels : true
    });
    
    var RailroadCrossings = new FeatureLayer("https://fragis.fra.dot.gov/fragis/rest/services/FRAGradeXing/MapServer/0", {
      mode: FeatureLayer.MODE_ONDEMAND,
      showLabels: false,
      id:"RailroadCrossing",
      definitionExpression: "StateCode=51 AND POSXING='1'"
    });
    map.addLayer(RailroadCrossings);
  });
</script>
</head>

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

</html>
0 Kudos
jaykapalczynski
Frequent Contributor

hmmmm...interesting...Maybe something to do with having my code in a seperate js file that I am referencing and using window. to get back to the main js page....will have to look closer...

0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Jay,

  What is the whole 

onStatusChanged

thing?

0 Kudos
jaykapalczynski
Frequent Contributor

OK I modified a bit to replicate what I have in my code....this appears to be working so it must be something else in my code that is causing this to happen...

Maybe my legend build and the button to turn on an off the layer....will look further..

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

<link rel="stylesheet" href="https://js.arcgis.com/3.27/esri/css/esri.css">

<script>
var packagePath = window.location.pathname.substring(0, window.location.pathname.lastIndexOf('/'));

//dojoConfig define non-esri JavaScript source required before loading arcgis js

var dojoConfig = {
	packages: [
		{
		name: "js",
		location: packagePath + "/js"
		}
	]
};

</script>

<script src="https://js.arcgis.com/3.27/"></script>
<script src="js/layerlist_Railroads_TEST.js"></script>

<style>
html, body, #map {
  padding: 0;
  margin: 0;
  height: 100%;
  width: 100%;
}
</style>

<script>
require([
    "esri/map","esri/layers/FeatureLayer","esri/InfoTemplate","dojo/_base/lang","dojo/_base/array","dojo/domReady!"
  ],
  function(
    Map,FeatureLayer,InfoTemplate,lang,array
  ) {

    var map = new Map("map", {
      basemap: "hybrid",
      center: [-79.383417, 36.581599],
      zoom: 11,
      showLabels : true
    });
	
		// Call the JS page that has all the GRID stuff in it. Run the Functions	
	window.LayerListRailroads();	

		map.addLayers([
			RailroadCrossings
		]);	

  });
</script>
</head>

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

</html>

require([
    "esri/map",  "dojo/dom", "esri/layers/FeatureLayer","dojo/on", "dojo/_base/array", "dojo/_base/lang",
    "esri/urlUtils", "dijit/registry", "esri/arcgis/utils", "esri/domUtils", 
	"dojo/_base/connect", "dojo/_base/array", "dojo/parser", "esri/InfoTemplate", "dijit/form/CheckBox", 
	"dojo/dom-construct", "dojo/keys", "dojo/domReady!",
	"js/widgets","js/grids"
], function(
    Map, dom, FeatureLayer, on, array, lang,
    urlUtils, registry, arcgisUtils, domUtils, 
	connect, arrayUtils, parser, InfoTemplate, CheckBox, 
	domConstruct, keys
) {
		  

	window.LayerListRailroads = function(){

	//window.legendLayers6 = [];


// LAYER FILES =======================================================================================
	
	// RAILS -------------------------	
			


		// GRAB THE DOMAIN VALUE FROM RailroadCrossings instead of the coded value	
		var subTypeValRR;  
		getSubtypeDomainRR = function (fieldVal, fieldName){ 
		  if (RailroadCrossings.typeIdField!=null) {  
			  //console.log("Have Subtypes");  
			  if (fieldName==RailroadCrossings.typeIdField) {  
				   array.forEach(RailroadCrossings.types, lang.hitch(this, function (lsf) {  
					   if (fieldVal==lsf.id){  
						   fieldVal=lsf.name;  
						   subTypeValRR=lsf.id;  
					   }  
				   }));  
			  } else {   
				   array.forEach(RailroadCrossings.types, lang.hitch(this, function (lsf) {  
					   if (lsf.id==subTypeValRR){  
						   array.forEach(lsf.domains[fieldName].codedValues, lang.hitch(this, function (domain) {  
							  if (fieldVal==domain.code){  
								  fieldVal=domain.name;  
							  }  
						   }));  
					   }  
				   }));  
			  }  
		  } else {  
			  //console.log("No Subtypes");  
			  subTypeValRR=null;  
			  array.forEach(RailroadCrossings.fields, lang.hitch(this, function (ldf) {  
				  if (ldf.name==fieldName){  
					  if (ldf.domain){  
						  array.forEach(ldf.domain.codedValues, lang.hitch(this, function(domain){  
							  if (fieldVal==domain.code){  
								  fieldVal=domain.name;  
							  }  
						  }));  
					  }  
				  }  
				
			  }));  
		  }  
		  return fieldVal;  
		};
		var infoTemplateRailroadCrossings = new InfoTemplate();
			infoTemplateRailroadCrossings.setTitle("Railroad Crossingss");	   
			infoTemplateRailroadCrossings.setContent("<table>" +
			"<tr><td id='tblTitle'>Railroad Crossing</td><td id='tblTitle2'></td></tr>" +
			"<tr><td id='tblTitleLine'></td><td id='tblTitleLine2'></td></tr>" +
			"<tr><td></td><td></td></tr>" +
			"<tr><td id='tblMainline1'>crossing</td><td id='tblSubline2'>${CROSSING}</td></tr>" +
			"<tr><td id='tblMainline1'>date</td><td id='tblSubline2'>${EFFDATE}</td></tr>" +
			"<tr><td id='tblMainline1'>reason</td><td id='tblSubline2'>${REASON:getSubtypeDomainRR}</td></tr>" + 
			"<tr><td id='tblMainline1'>rr</td><td id='tblSubline2'>${RAILROAD}</td></tr>" +
			"<tr><td id='tblMainline1'>rr division</td><td id='tblSubline2'>${RRDIV}</td></tr>" +
			"<tr><td id='tblMainline1'>rr subdivision</td><td id='tblSubline2'>${RRSUBDIV}</td></tr>" +			
			"<tr><td id='tblMainline1'>highway</td><td id='tblSubline2'>${HIGHWAY}</td></tr>" +
			"<tr><td id='tblMainline1'>street</td><td id='tblSubline2'>${STREET}</td></tr>" +
			"<tr><td id='tblMainline1'>milepost</td><td id='tblSubline2'>${MILEPOST}</td></tr>" +
			"<tr><td id='tblMainline1'>crossing type</td><td id='tblSubline2'>${TYPEXING:getSubtypeDomainRR}</td></tr>" +
			"<tr><td id='tblMainline1'>crossing pos</td><td id='tblSubline2'>${POSXING:getSubtypeDomainRR}</td></tr>" +
			"<tr><td id='tblMainline1'>rrcont</td><td id='tblSubline2'>${RRCONT}</td></tr>" +	
			"<tr><td id='tblMainline1'>inv link</td><td id='tblSubline2'><i>For more INV information please <a href='${INV_LINK}' target='_blank'><font color='0x84CCDA'>click here</font></a>."+
			"<tr><td id='tblMainline1'>acc link</td><td id='tblSubline2'><i>For more ACC information please <a href='${ACC_LINK}' target='_blank'><font color='0x84CCDA'>click here</font></a>."+			
			"</table><hr>");			
		window.RailroadCrossings = new FeatureLayer("https://fragis.fra.dot.gov/fragis/rest/services/FRAGradeXing/MapServer/0", {
			mode: FeatureLayer.MODE_ONDEMAND,
			showLabels: false,
			visible: true,
			opacity: 0.5,
			outFields:["*"],
			infoTemplate: infoTemplateRailroadCrossings,
			id:"RailroadCrossing",
			definitionExpression: " StateCode='51' AND POSXING='1' "
		});
		// Set Definition Criteria
		//RailroadCrossings.setDefinitionExpression(" StateCode='51' AND POSXING='1' ");
		// Push to Layer List
		//legendLayers6.push({ layer: RailroadCrossings, title: 'Railroad Crossings' });	
		
// END LAYER FILES =======================================================================================		

		} // End of LoadLayerList
	  
	});  // End fo require
0 Kudos
RobertScheitlin__GISP
MVP Esteemed Contributor

Jay,

   Although your definitionExpression is working, it is incorrect. The StateCode field is an integer and you have 51 in single qoute when it should not contain any qoutes when working with numbers in SQL.

0 Kudos
jaykapalczynski
Frequent Contributor

I found the error...it was in an unrelated function regarding a find near me tool....while I did not pin point the issue at this time I was able to comment out the code and remedy the problem.

Thank you Robert for pointing out the fact that the code and service was working.  It took me down another path that allowed me to see the real issue.  Much appreciated.

0 Kudos