-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathesri-leaflet-demo2.html
157 lines (140 loc) · 5.46 KB
/
esri-leaflet-demo2.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
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
153
154
155
156
157
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<title>Get Started</title>
<!-- Load Leaflet from CDN -->
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" crossorigin="" />
<script src="https://unpkg.com/[email protected]/dist/leaflet.js" crossorigin=""></script>
<!-- Load Esri Leaflet from CDN -->
<script src="https://unpkg.com/esri-leaflet@^3.0.8/dist/esri-leaflet.js"></script>
<script src="https://unpkg.com/[email protected]/dist/esri-leaflet-vector.js"></script>
<!-- Load Esri Leaflet Geocoder from CDN -->
<link rel="stylesheet" href="https://unpkg.com/esri-leaflet-geocoder@^3.1.3/dist/esri-leaflet-geocoder.css">
<script src="https://unpkg.com/esri-leaflet-geocoder@^3.1.3/dist/esri-leaflet-geocoder.js"></script>
<!-- require ArcGIS REST JS libraries from https://unpkg.com -->
<script src="https://unpkg.com/@esri/[email protected]/dist/bundled/request.umd.min.js"></script>
<script
src="https://unpkg.com/@esri/[email protected]/dist/bundled/routing.umd.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style>
body { margin:0; padding:0; }
#map {
position: absolute;
top:0;
bottom:0;
right:0;
left:0;
font-family: Arial, Helvetica, sans-serif;
font-size: 14px;
color: #323232;
}
.div-icon{
filter: drop-shadow( 1px 1px 0px rgba(0, 0, 0, .3))
/* box-shadow: 0 3px 2px -2px rgba(0, 0, 0, .7); */
/* -webkit-filter: drop-shadow( 3px 3px 2px rgba(0, 0, 0, .7));
filter: drop-shadow( 3px 3px 2px rgba(0, 0, 0, .7)); */
}
.leaflet-popup-content p {
margin: 0
}
.leaflet-popup-content h3 {
margin: 0
}
.leaflet-popup-content ol {
padding: 0rem 14px;
}
</style>
</head>
<body>
<div id="map"></div>
<ul id="myList"></ul>
<script>
const apiKey = "YOUR_API_KEY";
const basemapEnum = "d80698b0deca416d8ef8aba06389e8ac";
const authentication = new arcgisRest.ApiKeyManager({
key: apiKey
});
const map = L.map('map', {
minZoom: 2,
maxZoom: 14
}).setView([38.88, -77.2], 11);
L.esri.Vector.vectorBasemapLayer(basemapEnum, {
apiKey: apiKey
}).addTo(map);
// Add a living Atlas layer
const layer = L.esri.featureLayer({
url: "https://services1.arcgis.com/ioennV6PpG5Xodq0/ArcGIS/rest/services/OpenData_S1/FeatureServer/5",
}).addTo(map);
const searchControl = L.esri.Geocoding.geosearch({
position: "topright",
placeholder: "Enter an address or place e.g. 1 York St",
useMapBounds: false,
zoomToResult: false,
providers: [L.esri.Geocoding.arcgisOnlineProvider({
apikey: apiKey,
nearby: [38.9, -77.2],
})]
}).addTo(map);
const results = L.layerGroup().addTo(map);
const startIcon = new L.Icon({
iconUrl: 'https://raw.githubusercontent.com/pointhi/leaflet-color-markers/master/img/marker-icon-2x-red.png',
shadowUrl: 'https://cdnjs.cloudflare.com/ajax/libs/leaflet/0.7.7/images/marker-shadow.png',
iconSize: [25, 41],
iconAnchor: [12, 41],
popupAnchor: [1, -34],
shadowSize: [41, 41]
});
searchControl.on("results", data => {
results.clearLayers();
if(data.results.length) {
const latlng = data.results[0].latlng; //Geocode address
const marker = L.marker(latlng, { icon: startIcon });
results.addLayer(marker);
getClosestFacility(latlng).then(showClosestFacility);
}
});
function getClosestFacility(latlng){
return new Promise((resolved, rejected) => {
layer.query().run((err, featureSet) => {
const facilities = featureSet.features.map(f => f.geometry.coordinates); //The hospitals
resolved(facilities);
});
}).then(facilities => {
const incidents = [
[latlng.lng, latlng.lat]
];
return arcgisRest.closestFacility({
incidents: incidents, //Where you searched
facilities: facilities,
returnCFRoutes: true, //Return route to closest hospital
params: {
defaultTargetFacilityCount: 2, //Only one hospital
impedanceAttributeName: 'Miles'
},
authentication: authentication
});
});
}
const routeLines = L.layerGroup().addTo(map);
async function showClosestFacility(facilityResponse){
let routeList = [];
routeLines.clearLayers();
const { routes, directions } = facilityResponse;
const route = L.geoJSON(routes.geoJson).addTo(routeLines);
map.fitBounds(route.getBounds());
const { features, summary } = directions[0];
const distance = Math.round(summary.totalLength*10)/10;
for(let i = 1; i < features.length - 1; i++) {
routeList.push(features[i].attributes.text);
}
route.bindPopup(`<div style="margin-top:10px; font-size:13px;"><h3 style="font-size:17px;margin-bottom:5px;">Directions</h3><ol>
${routeList.reduce((u, l) => u.concat(`<li style="margin-bottom:4px;">${l}.</li>`), '')}</ol>
<p style="margin-top:10px;text-align:end;font-size:14px;"><b>Total Time:</b> ${Math.round(summary.totalTime)} minutes
<i>(${distance} ${distance === 1 ? 'mile' : 'miles'})</i></p></div>`)
route.openPopup();
}
</script>
</body>
</html>