I'm trying to have my feature layer labels show up at a certain zoom level. I can set maxScale and get results, but minScale doesn't work.
var labelColor = new Color("#00F");
var font = new Font();
font.setFamily("arial");
font.setSize("10pt");
var textSymbol = new TextSymbol();
textSymbol.setFont(font);
var basin = "{Name}";
var json = {
"labelExpressionInfo": {"value": basin + " Basin"},
"minScale":3
};
var labelClass = new LabelClass(json);
labelClass.symbol = textSymbol;
waterbasins.setLabelingInfo([ labelClass ]);
Solved! Go to Solution.
Lloyd,
Using this sample you have to zoom in three levels before the label will show:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Labeling features client-side</title>
<link rel="stylesheet" href="https://js.arcgis.com/3.20/esri/css/esri.css">
<style>
html, body, #map {
height: 100%; width: 100%; margin: 0; padding: 0;
}
</style>
<script src="https://js.arcgis.com/3.20/"></script>
<script>
var map;
require([
"esri/map",
"esri/geometry/Extent",
"esri/layers/FeatureLayer",
"esri/symbols/SimpleLineSymbol",
"esri/symbols/SimpleFillSymbol",
"esri/symbols/TextSymbol",
"esri/renderers/SimpleRenderer",
"esri/layers/LabelClass",
"esri/Color",
"dojo/domReady!"
], function(Map, Extent, FeatureLayer,
SimpleLineSymbol, SimpleFillSymbol,
TextSymbol, SimpleRenderer, LabelClass, Color)
{
// load the map centered on the United States
var bbox = new Extent({"xmin": -1940058, "ymin": -814715, "xmax": 1683105, "ymax": 1446096, "spatialReference": {"wkid": 102003}});
//create the map and set the extent, making sure to "showLabels"
map = new Map("map", {
extent: bbox,
showLabels : true //very important that this must be set to true!
});
// create a renderer for the states layer to override default symbology
var statesColor = new Color("#666");
var statesLine = new SimpleLineSymbol("solid", statesColor, 1.5);
var statesSymbol = new SimpleFillSymbol("solid", statesLine, null);
var statesRenderer = new SimpleRenderer(statesSymbol);
// create the feature layer to render and label
var statesUrl = "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3";
var states = new FeatureLayer(statesUrl, {
id: "states",
outFields: ["*"]
});
states.setRenderer(statesRenderer);
// create a text symbol to define the style of labels
var statesLabel = new TextSymbol().setColor(statesColor);
statesLabel.font.setSize("14pt");
statesLabel.font.setFamily("arial");
//this is the very least of what should be set within the JSON
var json = {
"labelExpressionInfo": {"value": "{STATE_NAME}"},
"minScale": 5000000
};
//create instance of LabelClass (note: multiple LabelClasses can be passed in as an array)
var labelClass = new LabelClass(json);
labelClass.symbol = statesLabel; // symbol also can be set in LabelClass' json
states.setLabelingInfo([ labelClass ]);
map.addLayer(states);
});
</script>
</head>
<body>
<div id="map"></div>
</body>
</html>
Lloyd,
Zoom Level and Zoom Scale are two different animals a map may have zoom levels like 1 through 21 where valid zoom scale could be 500 through 300,000. So a minScale of 3 is a very unlikely value.
OK. I tried setting the minScale at various points between 500 to 300,000 and the labels aren't showing up at any zoom level.
Lloyd,
Hmm. Normally a label expression looks like this:
"labelExpressionInfo": {"value": "{SomeFieldName}"},
Should I even be setting the minScale under the json variable after the label expression? I've tried setting it as labelClass.minScale = 7999; which is zoom level 4 according to this example. The labels still aren't showing up. Do I have to have a maxScale in addition to minScale?
var json = {
"labelExpressionInfo": {"value": basin + " Basin"},
};
var labelClass = new LabelClass(json);
//labelClass.maxScale =;
labelClass.minScale = 7999;
labelClass.symbol = textSymbol;
waterbasins.setLabelingInfo([ labelClass ]);
Lloyd,
Have you tried not using either min or max scale just to test if your using label class correctly?
Yes, the labels show up fine without scale settings. The only thing that has worked is when I set maxScale to 3 or 4. The labels disappear at those zoom levels. That shouldn't work based on what you said before.
Lloyd,
Using this sample you have to zoom in three levels before the label will show:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
<title>Labeling features client-side</title>
<link rel="stylesheet" href="https://js.arcgis.com/3.20/esri/css/esri.css">
<style>
html, body, #map {
height: 100%; width: 100%; margin: 0; padding: 0;
}
</style>
<script src="https://js.arcgis.com/3.20/"></script>
<script>
var map;
require([
"esri/map",
"esri/geometry/Extent",
"esri/layers/FeatureLayer",
"esri/symbols/SimpleLineSymbol",
"esri/symbols/SimpleFillSymbol",
"esri/symbols/TextSymbol",
"esri/renderers/SimpleRenderer",
"esri/layers/LabelClass",
"esri/Color",
"dojo/domReady!"
], function(Map, Extent, FeatureLayer,
SimpleLineSymbol, SimpleFillSymbol,
TextSymbol, SimpleRenderer, LabelClass, Color)
{
// load the map centered on the United States
var bbox = new Extent({"xmin": -1940058, "ymin": -814715, "xmax": 1683105, "ymax": 1446096, "spatialReference": {"wkid": 102003}});
//create the map and set the extent, making sure to "showLabels"
map = new Map("map", {
extent: bbox,
showLabels : true //very important that this must be set to true!
});
// create a renderer for the states layer to override default symbology
var statesColor = new Color("#666");
var statesLine = new SimpleLineSymbol("solid", statesColor, 1.5);
var statesSymbol = new SimpleFillSymbol("solid", statesLine, null);
var statesRenderer = new SimpleRenderer(statesSymbol);
// create the feature layer to render and label
var statesUrl = "https://sampleserver6.arcgisonline.com/arcgis/rest/services/Census/MapServer/3";
var states = new FeatureLayer(statesUrl, {
id: "states",
outFields: ["*"]
});
states.setRenderer(statesRenderer);
// create a text symbol to define the style of labels
var statesLabel = new TextSymbol().setColor(statesColor);
statesLabel.font.setSize("14pt");
statesLabel.font.setFamily("arial");
//this is the very least of what should be set within the JSON
var json = {
"labelExpressionInfo": {"value": "{STATE_NAME}"},
"minScale": 5000000
};
//create instance of LabelClass (note: multiple LabelClasses can be passed in as an array)
var labelClass = new LabelClass(json);
labelClass.symbol = statesLabel; // symbol also can be set in LabelClass' json
states.setLabelingInfo([ labelClass ]);
map.addLayer(states);
});
</script>
</head>
<body>
<div id="map"></div>
</body>
</html>
Still no labels at any zoom level if I set a minScale.
The sample I provided works fine for me did you test it?