How to retrieve province, municipality, ward no and district?

157
2
05-19-2020 04:44 AM
SiyabongaKubeka
Occasional Contributor

Hi All

I have the following JavaScript code that I use to retrieve province, municipality, ward no and district. Now I want to put inside its own variable, how can I do that?

<script>
var lon;
var lat;
var latlon;
var provinces;
var municipalities;
var districts;
var wards;
require([
"esri/Map",
"esri/views/MapView",
"esri/WebMap",
"esri/layers/FeatureLayer",
"esri/tasks/QueryTask",
"esri/tasks/support/Query",
"esri/config",
"esri/widgets/Sketch",
"esri/layers/GraphicsLayer"],

function(Map, MapView, WebMap, FeatureLayer, QueryTask, Query,
esriConfig, Sketch, GraphicsLayer) {
esriConfig.portalUrl = "https://portal.environment.gov.za/portal";

const featureLayerUrl = 'https://portal.environment.gov.za/server/rest/services/Boundary/Wards/MapServer/0';
var webmap = new WebMap({
portalItem: {
id: "04582be14885483da48f29398960f653"
}
});

var graphicsLayer = new GraphicsLayer();

var view = new MapView({
map: webmap,
container: "viewDiv",
popup: null
});

var featureLayer = new FeatureLayer({
url: featureLayerUrl
});

webmap.add(featureLayer);

webmap.layers.add(graphicsLayer);

var sketch = new Sketch({
layer: graphicsLayer,
view: view,
creationMode: "update",

});

view.ui.add(sketch, {
position: "top-right"
});

sketch.on('create', function (event) {
// check if the create event's state has changed to complete indicating
// the graphic create operation is completed.
if (event.state === "complete") {

if (view.zoom >= 11) {
let gra = event.graphic.clone();
event.graphic.layer.removeAll();
gra.symbol.color = "red";
gra.layer.add(gra);
console.log(view.zoom);
console.log("X = ", gra.geometry.x);
console.log("Y = ", gra.geometry.y);
console.log("Lat = ", event.graphic.geometry.latitude);
console.log("Long = ", event.graphic.geometry.longitude);
lat = event.graphic.geometry.latitude;
lon = event.graphic.geometry.longitude;
zoomLevel = view.zoom;
for (var i = 0; i < gra.geometry.rings.length; i++){
for (var p = 0; p < gra.geometry.rings.length; p++){
var LatLon = String(gra.geometry.rings

);
console.log(LatLon);
latlon = LatLon;


}
}
debugger;
}
else{
alert("please zoom in");
event.graphic.layer.remove(event.graphic);
}
}

});

view.on('click', function(event){
var queryTask = new QueryTask({
url: featureLayerUrl
});
var query = new Query();
query.geometry = view.toMap(event);
query.distance = 0;
query.units = "meters";
query.spatialRelationship = "intersects";
query.returnGeometry = true;
query.outFields = [ "PROVINCE", "DCS12_NAME", "S12_NAME", "WARD_NO" ];

queryTask.execute(query).then(function(results){
console.log(results.features[0].attributes);
});

});
});
</script>

Tags (2)
0 Kudos
2 Replies
RobertScheitlin__GISP
MVP Esteemed Contributor

You are almost there.

queryTask.execute(query).then(function(results){
  var feat = results.features[0];
  provinces = feat.attributes.PROVINCE;
  wards = feat.attributes.WARD_NO;
  districts = feat.attributes.DCS12_NAME;
  municipalities = feat.attributes.S12_NAME
});
SiyabongaKubeka
Occasional Contributor

Hi Robert,

Thank you very much.

0 Kudos