forked from mross889/Hospital_locations
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuery.js
More file actions
152 lines (129 loc) · 3.59 KB
/
Query.js
File metadata and controls
152 lines (129 loc) · 3.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
require([
"esri/Map",
"esri/views/MapView",
"esri/layers/GraphicsLayer",
"esri/tasks/QueryTask",
"esri/tasks/support/Query",
"esri/symbols/SimpleMarkerSymbol"
], function(Map, MapView, GraphicsLayer, QueryTask, Query,SimpleMarkerSymbol) {
// URL to feature service containing Global Hospital Locations
var hospUrl =
"https://services.arcgis.com/o6oETlrWetREI1A2/ArcGIS/rest/services/Hospital_LL/FeatureServer/0";
// Define the popup content for each result
var popupTemplate = {
// autocasts as new PopupTemplate()
title: "{Hospital}, {City_Town}",
fieldInfos: [
{
fieldName: "Hosp_beds",
label: "Bed Count",
format: {
places: 0,
digitSeperator: true
}
},
{
fieldName: "Occupency",
label: "Beds Occupied",
format: {
places: 0,
digitSeperator: true
}
},],
};
var hospSymbol = {
type: "simple-marker", // autocasts as new 2D Point
symbolLayers: [
{
type: "point", // autocasts as new point symbol
resource: {
primitive: "point"
}
}
],
};
// Create graphics layer and symbol to use for displaying the results of query
//THIS WILL BE DISPLAYED INSIDE THE CIRCLE.
var resultsLayer = new GraphicsLayer();
// Point QueryTask to URL of feature service
var qTask = new QueryTask({
url: hospUrl
});
var params = new Query({
returnGeometry: true,
outFields: ["*"]
});
const map = new Map({
basemap: "hybrid",
layers: [resultsLayer] // add graphics layer to the map
});
var view = new MapView({
map: map,
container: "viewDiv1",
center: [-100, 38],
zoom: 4,
popup: {
dockEnabled: true,
dockOptions: {
position: "top-right",
breakpoint: false
}
}
});
// Call doQuery() each time the button is clicked
view.when(function() {
view.Accpane3.add("optionsDiv1");
document.getElementById("doBtn").addEventListener("click", doQuery);
});
var attributeName = document.getElementById("attSelect");
var expressionSign = document.getElementById("signSelect");
var value = document.getElementById("valSelect");
// Executes each time the button is clicked
function doQuery() {
// Clear the results from a previous query
resultsLayer.removeAll();
params.where =
attributeName.value + expressionSign.value + value.value;
// executes the query and calls getResults() once the promise is resolved
// promiseRejected() is called if the promise is rejected
qTask
.execute(params)
.then(getResults)
.catch(promiseRejected);
}
// Called each time the promise is resolved
function getResults(response) {
// Loop through each of the results and assign a symbol and PopupTemplate
// to each so they may be visualized on the map
var hospResults = response.features.map(function(feature) {
feature.symbol = {
type: "simple-marker",
style: "circle",
color: "blue",
size: "8px", // pixels
outline: {
color: [124, 11, 11],
width: 2 // points
}
};
feature.popupTemplate = popupTemplate;
return feature;
});
resultsLayer.addMany(hospResults);
// animate to the results after they are added to the map
view.goTo(hospResults).then(function() {
view.popup.open({
features: hospResults,
featureMenuOpen: true,
updateLocationEnabled: true
});
});
// print the number of results returned to the user
document.getElementById("printResults").innerHTML =
hospResults.length + " results found!";
}
// Called each time the promise is rejected
function promiseRejected(error) {
console.error("Promise rejected: ", error.message);
}
});