-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.js
179 lines (171 loc) · 5.17 KB
/
main.js
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
var OPENCAGEDATA_URL = 'https://api.opencagedata.com/geocode/v1/json';
var SUGGESTIONS_ENABLED = true;
// Get the user's w3w API key via prompt
var key = localStorage.getItem('OPENCAGE_API_KEY');
if (!key || key === 'null') {
localStorage.setItem(
'OPENCAGE_API_KEY',
prompt(
'What is your OpenCage Data API key? It will be stored only in your localStorage'
)
);
}
var API_KEY = localStorage.getItem('OPENCAGE_API_KEY');
if (!API_KEY || API_KEY === 'null') {
// Test key always returns "Friedrich-Ebert-Straße 7, 48153 Münster, Germany"
API_KEY = '6d0e711d72d74daeb2b0bfd2a5cdfdba';
}
require([
'esri/config',
'esri/Map',
'esri/Graphic',
'esri/portal/Portal',
'esri/request',
'esri/views/MapView',
'esri/widgets/BasemapGallery',
'esri/widgets/Expand',
'esri/widgets/Search',
'esri/widgets/Search/SearchSource',
'esri/geometry/geometryEngine',
'esri/geometry/Point',
], function (
esriConfig,
Map,
Graphic,
Portal,
esriRequest,
MapView,
BasemapGallery,
Expand,
Search,
SearchSource,
geometryEngine,
Point
) {
function geocode(options) {
return esriRequest(OPENCAGEDATA_URL, {
query: {
key: API_KEY,
q: options.query,
proximity: options.proximity,
no_annotations: 1,
limit: 6,
},
responseType: 'json',
});
}
esriConfig.portalUrl = 'https://jsapi.maps.arcgis.com';
// Intialize a portal instance and load it
var portal = new Portal();
portal.load().then(function () {
var basemap = portal.useVectorBasemaps
? portal.defaultVectorBasemap
: portal.defaultBasemap;
var map = new Map({
basemap: basemap,
});
var view = new MapView({
container: 'viewDiv',
map: map,
center: [-2.547855, 54.00366], // lon, lat
scale: 4000000,
});
// The BasemapGallery will use the basemaps
// configured by the Portal URL defined in esriConfig.portalUrl
var basemapGallery = new BasemapGallery({
view: view,
});
var bgExpand = new Expand({
view: view,
content: basemapGallery,
});
view.ui.add(bgExpand, 'bottom-left');
// Custom SearchSource
var opencageSearchSource = new SearchSource({
placeholder: 'example: W10',
suggestionsEnabled: SUGGESTIONS_ENABLED,
minSuggestCharacters: 3,
getSuggestions: function (params) {
var address = params.suggestTerm.replace(/ /g, '+');
return geocode({
query: address,
proximity: view.center.latitude + ',' + view.center.longitude,
}).then(function (response) {
// console.log('esri request response', response);
var suggestions = response.data.results.map(function (feature) {
return {
key: 'name',
text: feature.formatted,
location: {
longitude: feature.geometry.lng,
latitude: feature.geometry.lat,
},
sourceIndex: params.sourceIndex,
};
});
// console.log('suggestions', suggestions);
return suggestions;
});
},
// Provide a getResults method to find
// results from the suggestions, the device location or the text input
getResults: function (params) {
var query;
// You can perform a different query if a location
// is provided
if (params.location) {
query = params.location.latitude + ',' + params.location.longitude;
} else {
query = params.suggestResult.text.replace(/ /g, '+');
}
// console.log('query', query);
return geocode({
query: query,
}).then(function (results) {
console.log('getResults().esriRequest()', results);
// Parse the results of your custom search
var searchResults = results.data.results.map(function (feature) {
// Create a Graphic the Search widget can display
var graphic = new Graphic({
geometry: new Point({
x: feature.geometry.lng,
y: feature.geometry.lat,
}),
attributes: {
name: feature.formatted,
label: feature.formatted,
props: feature.properties,
},
});
var buffer = geometryEngine.geodesicBuffer(
graphic.geometry,
250,
'meters'
);
// Return a Search Result
var searchResult = {
extent: buffer.extent,
feature: graphic,
name: feature.formatted,
};
// console.log('searchResult', searchResult);
return searchResult;
});
// console.log(searchResults);
// Return an array of Search Results
return searchResults;
});
},
});
// Create Search widget using custom SearchSource
var searchWidget = new Search({
view: view,
sources: [opencageSearchSource],
includeDefaultSources: false,
});
// Add the search widget to the top left corner of the view
view.ui.add(searchWidget, {
position: 'top-right',
});
});
});