-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
260 lines (223 loc) · 8.06 KB
/
index.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<title>Lat Lon</title>
<link rel="stylesheet" href="https://js.arcgis.com/4.9/esri/css/main.css">
<link rel="stylesheet" href="https://s3-us-west-1.amazonaws.com/patterns.esri.com/files/calcite-web/1.1.0/css/calcite-web.min.css">
<script src="https://js.arcgis.com/4.9/"></script>
<style>
html,
body,
#viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
#textContainer{
background-color: white;
padding: 5px;
text-align: center;
width: 300px;
}
.no-marg{
margin: 0;
}
.hide{
display: none;
}
</style>
<script>
require([
"esri/identity/OAuthInfo",
"esri/identity/IdentityManager",
"esri/WebMap",
"esri/layers/FeatureLayer",
"esri/Map",
"esri/views/MapView",
"esri/Graphic",
"esri/widgets/Search",
"esri/widgets/BasemapToggle"
],
function(OAuthInfo, esriId, WebMap, FeatureLayer, Map, MapView, Graphic, Search, BasemapToggle) {
/****************************************************
* Initialize OAuth / Login
****************************************************/
const portalUrl = "https://www.arcgis.com/sharing";
var info = new OAuthInfo({
appId: "WwNy6bnHK2YboEpE"
});
esriId.registerOAuthInfos([info]);
const signInBtn = document.getElementById("sign-in");
signInBtn.addEventListener("click", _ => {
esriId.getCredential(portalUrl);
});
esriId.checkSignInStatus(portalUrl).then((cred) => {
if(cred && cred.userId){
signInBtn.innerText = cred.userId;
}
});
/****************************************************
* Constants and constant functions
****************************************************/
const sigs = 5;
let pin;
const sym = {
type: "simple-marker",
color: [226, 119, 40],
outline: {
color: [255, 255, 255],
width: 1
}}
const getLatLonText = (lat, lon) => {
const lonText = `${Math.abs(lon).toFixed(sigs)}° ${lon < 0 ? 'W' : 'E'}`
const latText = `${Math.abs(lat).toFixed(sigs)}° ${lat < 0 ? 'S' : 'N'}`
return `<b>Lat:</b> ${latText}, <b>Lon:</b> ${lonText}`
}
const getCoordText = (lat, lon) => `[${lon.toFixed(sigs)}, ${lat.toFixed(sigs)}]`;
const getZoomText = (zoom, scale) => `<b>Zoom:</b> ${zoom.toFixed(0)} <b>Scale:</b> 1:${scale.toFixed(0)}`
/****************************************************
* Load the map and helpful widgets
****************************************************/
const map = new Map({
basemap: "topo-vector"
});
const view = new MapView({
container: "viewDiv",
map: map,
center: [-86.049, 38.485],
zoom: 3,
constraints: {
snapToZoom: false
}
});
const toggle = new BasemapToggle({
view: view,
nextBasemap: "hybrid"
});
const searchWidget = new Search({
view: view
});
const latlonEl = document.getElementById('latlonText');
const coordTextEl = document.getElementById('coordText');
const zoomTextEl = document.getElementById('zoomText');
const textContainer = document.getElementById('textContainer');
const dataContainer = document.getElementById('data-form');
view.ui.add(textContainer, {position: "top-left", index: 0})
view.ui.add([dataContainer, searchWidget], "top-right");
view.ui.add(toggle, 'bottom-right');
/****************************************************
* Listeners for adding custom data
****************************************************/
// When a user adds a webmap by id
// Load it then zoom to its extent
const mapAddBtn = document.getElementById('map-add');
const mapTextBtn = document.getElementById('map-id');
mapAddBtn.addEventListener("click", _ => {
const id = mapTextBtn.value;
if(!id) return;
map = new WebMap({
portalItem: {id: id}
});
view.map = map;
map.when(() => {
view.viewpoint = map.initialViewProperties.viewpoint;
map.remove(polygonGraphicsLayer);
map.add(polygonGraphicsLayer);
})
.catch(er => console.log(er));
});
const lyrAddBtn = document.getElementById('layer-add');
const lyrTextBtn = document.getElementById('lyr-id');
let lyr;
lyrAddBtn.addEventListener("click", _ => {
const id = lyrTextBtn.value;
if(!id) return;
lyr = new FeatureLayer({
portalItem: {id: id}
});
map.add(lyr);
lyr.queryExtent().then(res => {
view.goTo(res.extent);
}).catch(e => {
console.log(e)
});
});
/****************************************************
* Listen for events and update the text
****************************************************/
function updateText(point) {
latlonEl.innerHTML = getLatLonText(point.latitude, point.longitude);
coordTextEl.innerHTML = getCoordText(point.latitude, point.longitude);
}
function updateZoom(zoom, scale){
zoomTextEl.innerHTML = getZoomText(zoom, scale);
}
view.on('pointer-move', evt => {
if(pin) return;
const point = view.toMap({x: evt.x, y: evt.y});
updateText(point);
})
view.on('click', evt => {
const screenPt = {x: evt.x, y: evt.y};
view.hitTest(screenPt).then(res => {
fRes = res.results.filter(f => f.graphic.layer === null);
if(pin) view.graphics.remove(pin);
if(fRes.length < 1){
const point = view.toMap(screenPt);
pin = new Graphic({geometry: point, symbol: sym});
view.graphics.add(pin);
updateText(point);
} else {
pin = null;
}
});
});
view.watch('scale', scale => {
updateZoom(view.zoom, view.scale);
})
view.when(() => {
textContainer.classList.remove('hide');
dataContainer.classList.remove('hide');
});
});
</script>
</head>
<body>
<header class="top-nav">
<nav class="class-top-nav-list" role="navigation" aria-labelledby="usernav">
<a class="top-nav-title margin-left-1">Lat Lon</a>
<a id="sign-in" class="top-nav-link icon-ui-user margin-right-1 right" href="#">Sign In</a>
</nav>
</header>
<div id="viewDiv"></div>
<div id="textContainer" class="hide">
<p class="avenir-italic font-size--3 no-marg">Move to get coordinates, click to lock a location</p>
<p id="latlonText" class="no-marg"><b>Lat</b>: --.------ <b>Lon</b>: --.------</p>
<p id="zoomText" class="no-marg"><b>Zoom:</b> -- <b>Scale:</b> --------.</p>
<p id="coordText" class="no-marg">[--.------, --.------]</p>
</div>
<div class="esri-widget hide" style="padding: 10px; width: 250px" id="data-form">
<label>
Add a webmap
<div class="input-group">
<input class="input-group-input" type="text" placeholder="ArcGIS Online Item ID" id="map-id"/>
<span class="input-group-button">
<button class="btn" id="map-add">+</button>
</span>
</div>
</label>
<label>
Add a layer
<div class="input-group">
<input id="lyr-id" class="input-group-input" type="text" placeholder="ArcGIS Online Item ID"/>
<span class="input-group-button">
<button class="btn" id="layer-add">+</button>
</span>
</div>
</label>
</div>
</body>
</html>