Here is my code:
<!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>Map and Layer</title>
<link rel="stylesheet" href="https://js.arcgis.com/3.15/esri/css/esri.css">
<style>
html, body, #map {
height: 100%;
margin: 0;
}
#info {
position: absolute;
right: 0;
top: 0;
padding: 10px;
background-color: #999;
font: 14px Segoe UI;
width: 200px;
text-align: center;
border-radius: 0 0 0 10px;
}
</style>
<script src="https://js.arcgis.com/3.15/"></script>
<script>
var map, calculateAcreage;
require([
"esri/map", "esri/layers/FeatureLayer", "esri/InfoTemplate",
"esri/dijit/Legend",
"esri/Color", "dojo/number", "dojo/domReady!"
], function(
Map, FeatureLayer, InfoTemplate,
Legend,
Color, number
) {
map = new Map("map", {
basemap: "gray",
center: [-97.144, 34.174],
zoom: 6
});
map.on("layers-add-result", createLegend);
var infoTemplate = new InfoTemplate(
"${Name},
"${SHAPE.STArea()} sq.ft is equivalent to ${SHAPE.STArea():calculateAcreage} acres."
);
var layer = new FeatureLayer("http://url", {
infoTemplate: infoTemplate,
outFields: ["*"],
opacity: 0.5
});
// convert squarefeet to acre
calculateAcreage = function(value) {
var squarefeet = (value.hasOwnProperty("attributes")) ? value.attributes.SHAPE.STArea() : value;
return number.format(squarefeet / 43560, { places: 2 });
};
map.addLayers([layer]);
function createLegend(results) {
var legend = new Legend({
layerInfos: [{
layer: results.layers[0].layer,
title: " "
}],
map: map
}, "legend");
legend.startup();
}
});
</script>
</head>
<body>
<div id="map"></div>
<div id="info">
<div style="font-size: 36px;">Acreage of Cooperator Land.</div>
<div id="legend"></div>
</div>
</body>
</html>
Solved! Go to Solution.
Kushendra,
I couldn't figure out a simple way to access the Shape.STArea() value. It does seem like you should be able to use it directly. Maybe someone else will have a solution.
I do have a workaround for you using the geometryEngine. Call your function from the infoTemplate something like this:
var infoTemplate = new InfoTemplate( "${Name}", calculateAcreage );
Add the function:
function calculateAcreage(value) { var sqft = geometryEngine.geodesicArea(value.geometry, "square-feet"); var squarefeet = number.format(sqft, { places: 1 }); var acres = number.format(sqft / 43560, { places: 2 }); return squarefeet + " sq.ft is equivalent to " + acres + " acres." }
I hope this helps!
Regards,
Tom
Kushendra,
I couldn't figure out a simple way to access the Shape.STArea() value. It does seem like you should be able to use it directly. Maybe someone else will have a solution.
I do have a workaround for you using the geometryEngine. Call your function from the infoTemplate something like this:
var infoTemplate = new InfoTemplate( "${Name}", calculateAcreage );
Add the function:
function calculateAcreage(value) { var sqft = geometryEngine.geodesicArea(value.geometry, "square-feet"); var squarefeet = number.format(sqft, { places: 1 }); var acres = number.format(sqft / 43560, { places: 2 }); return squarefeet + " sq.ft is equivalent to " + acres + " acres." }
I hope this helps!
Regards,
Tom
Awesome Tom!. That works great. I didn't realize geometry engine is powerful one. I have been digging out all this morning to fix this issue. Thank you so much for the help!. Truly appreciated!!
Kushendra,
You are welcome!
Also note that if you are using a typical ArcGIS basemaps, then geodesicArea is correct. If the basemap you are using is for instance in stateplane coordinates, you should use planarArea.
esri/geometry/geometryEngine | API Reference | ArcGIS API for JavaScript
The first paragraph in this link gives a good explanation of which to use.
Best Regards,
Tom
Thanks for the insights Tom.
MS SQL has Geometry functions that might be helpful if you didn't want to preform the calc on the client side.
Select shape.STArea() as Area from sde.COUNTIES
Thanks Jordan.