-
Notifications
You must be signed in to change notification settings - Fork 11
/
closest-facility.html
110 lines (92 loc) · 3.6 KB
/
closest-facility.html
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
<html>
<head>
<meta charset=utf-8 />
<title>route to the closest facility</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<!-- Load Leaflet from CDN-->
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.2.0/dist/leaflet.css" />
<script src="https://unpkg.com/leaflet@1.2.0"></script>
<!-- Load Esri Leaflet from CDN -->
<script src="https://unpkg.com/esri-leaflet@2.1.1"></script>
<!-- Esri Leaflet GP -->
<script src="https://unpkg.com/esri-leaflet-gp@2.0.3"></script>
<script src="https://unpkg.com/leaflet-shape-markers@1.0.6"></script>
<style>
body {margin:0;padding:0;}
#map {position: absolute;top:0;bottom:0;right:0;left:0;}
#info-pane {
position: absolute;
top: 10px;
right: 10px;
z-index: 1000;
padding: 1em;
background: white;
max-width: 240px;
}
</style>
</head>
<body>
<div id="map"></div>
<div id="info-pane" class="leaflet-bar">
<label>
click on the map to determine the best route to the nearest hospital.
</label>
</div>
<script>
let map = L.map('map').setView([33.780021258266224, -118.18885803222658], 12);
L.esri.basemapLayer('Topographic').addTo(map);
// layers to organize graphics
let routesLayer = L.featureGroup();
let ambulanceLayer = L.featureGroup();
map.addLayer(routesLayer);
map.addLayer(ambulanceLayer);
let ambulanceIcon = L.icon({
iconUrl: 'https://cdn4.iconfinder.com/data/icons/SIGMA/medical/png/256/ambulance.png',
iconSize: [40, 40],
iconAnchor: [20, 20]
});
// use a service proxy to authenticate automagically
// https://developers.arcgis.com/authentication/working-with-proxies/
let closestFacilityService = L.esri.GP.service({
url: "https://utility.arcgis.com/usrsvcs/appservices/FnVYYTap8ykTDC1m/rest/services/World/ClosestFacility/NAServer/ClosestFacility_World",
path: 'solveClosestFacility'
});
let closestFacilityTask = closestFacilityService.createTask();
// tell the solver we'll be driving an ambulance
closestFacilityTask.setParam("restrictionAttributeNames", "Driving an Emergency Vehicle");
closestFacilityTask.setParam("returnCFRoutes", true);
// fetch nearby hospitals from LA open data
L.esri.query({
url: "https://public.gis.lacounty.gov/public/rest/services/LACounty_Dynamic/LMS_Data_Public/MapServer/81"
})
.within(map.getBounds())
.run(function (err, response, raw) {
// draw the hospitals on the map
map.addLayer(L.geoJSON(response, {
pointToLayer: function (geoJsonPoint, latlng) {
return L.shapeMarkers.crossMarker(latlng, 25, { color: 'red', weight: 5 })
}
}));
// supply the same hospitals as an input parameter to the closest facility service
closestFacilityTask.setParam("facilities", response);
});
// when someone clicks on the map, find the route to the nearest hospital
map.on("click", function (evt) {
// clear any existing
routesLayer.clearLayers();
ambulanceLayer.clearLayers();
// add an ambulance icon to the map
ambulanceLayer.addLayer(L.marker(evt.latlng, {
icon: ambulanceIcon
}));
// supply the location of the map click as a service parameter
closestFacilityTask.setParam("incidents", evt.latlng);
// and call the solver
closestFacilityTask.run(function (err, res, raw) {
// draw the GeoJSON feature that was returned
routesLayer.addLayer(L.geoJSON(res.routes.features[0]));
})
});
</script>
</body>
</html>