-
Notifications
You must be signed in to change notification settings - Fork 5.8k
Expand file tree
/
Copy pathlocation-service-basics.js
More file actions
624 lines (585 loc) · 21.8 KB
/
location-service-basics.js
File metadata and controls
624 lines (585 loc) · 21.8 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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
// snippet-start:[LocationServices.javascriptv3.Scenario.basics]
/*
Before running this JavaScript code example, set up your development environment, including your credentials.
This demo illustrates how to use the AWS SDK for JavaScript (v3) to work with Amazon Location Service.
For more information, see the following documentation topic:
https://docs.aws.amazon.com/sdk-for-javascript/v3/developer-guide/getting-started.html
*/
import {
Scenario,
ScenarioAction,
ScenarioInput,
ScenarioOutput,
} from "@aws-doc-sdk-examples/lib/scenario/index.js";
import {
CreateMapCommand,
CreateGeofenceCollectionCommand,
PutGeofenceCommand,
CreateTrackerCommand,
BatchUpdateDevicePositionCommand,
GetDevicePositionCommand,
CreateRouteCalculatorCommand,
CalculateRouteCommand,
LocationClient,
ConflictException,
ResourceNotFoundException,
DeleteGeofenceCollectionCommand,
DeleteRouteCalculatorCommand,
DeleteTrackerCommand,
DeleteMapCommand,
} from "@aws-sdk/client-location";
import {
GeoPlacesClient,
ReverseGeocodeCommand,
SearchNearbyCommand,
SearchTextCommand,
GetPlaceCommand,
ValidationException,
} from "@aws-sdk/client-geo-places";
import { parseArgs } from "node:util";
import { fileURLToPath } from "node:url";
/*The inputs for this example can be edited in the ./input.json.*/
import data from "./inputs.json" with { type: "json" };
/**
* Used repeatedly to have the user press enter.
* @type {ScenarioInput}
*/
/* v8 ignore next 3 */
const pressEnter = new ScenarioInput("continue", "Press Enter to continue", {
type: "confirm",
verbose: "false",
});
const pressEnterConfirm = new ScenarioInput(
"confirm",
"Press Enter to continue",
{
type: "confirm",
verbose: "false",
},
);
const region = "eu-west-1";
const locationClient = new LocationClient({ region: region });
const greet = new ScenarioOutput(
"greet",
"Welcome to the Amazon Location Use demo! \n" +
"AWS Location Service is a fully managed service offered by Amazon Web Services (AWS) that " +
"provides location-based services for developers. This service simplifies " +
"the integration of location-based features into applications, making it " +
"Maps: The service provides access to high-quality maps, satellite imagery, " +
"and geospatial data from various providers, allowing developers to " +
"easily embed maps into their applications:\n" +
"Tracking: The Location Service enables real-time tracking of mobile devices, " +
"assets, or other entities, allowing developers to build applications " +
"that can monitor the location of people, vehicles, or other objects.\n" +
"Geocoding: The service provides the ability to convert addresses or " +
"location names into geographic coordinates (latitude and longitude), " +
"and vice versa, enabling developers to integrate location-based search " +
"and routing functionality into their applications. " +
"Please define values ./inputs.json for each user-defined variable used in this app. Otherwise the default is used:\n" +
"- mapName: The name of the map to be create (default is 'AWSMap').\n" +
"- keyName: The name of the API key to create (default is ' AWSApiKey')\n" +
"- collectionName: The name of the geofence collection (default is 'AWSLocationCollection')\n" +
"- geoId: The geographic identifier used for the geofence or map (default is 'geoId')\n" +
"- trackerName: The name of the tracker (default is 'geoTracker')\n" +
"- calculatorName: The name of the route calculator (default is 'AWSRouteCalc')\n" +
"- deviceId: The ID of the device (default is 'iPhone-112356')",
{ header: true },
);
const displayCreateAMap = new ScenarioOutput(
"displayCreateAMap",
"1. Create a map\n" +
"An AWS Location map can enhance the user experience of your " +
" application by providing accurate and personalized location-based " +
" features. For example, you could use the geocoding capabilities to " +
" allow users to search for and locate businesses, landmarks, or " +
" other points of interest within a specific region.",
);
const sdkCreateAMap = new ScenarioAction(
"sdkCreateAMap",
async (/** @type {State} */ state) => {
const createMapParams = {
MapName: `${data.inputs.mapName}`,
Configuration: { style: "VectorEsriNavigation" },
};
try {
const command = new CreateMapCommand(createMapParams);
const response = await locationClient.send(command);
state.MapName = response.MapName;
console.log("Map created. Map ARN is: ", state.MapName);
} catch (error) {
console.error("Error creating map: ", error);
throw error;
}
},
);
const displayMapUrl = new ScenarioOutput(
"displayMapUrl",
"2. Display Map URL\n" +
"When you embed a map in a web app or website, the API key is " +
"included in the map tile URL to authenticate requests. You can " +
"restrict API keys to specific AWS Location operations (e.g., only " +
"maps, not geocoding). API keys can expire, ensuring temporary " +
"access control.\n" +
"In order to get the MAP URL you need to create and get the API Key value. " +
"You can create and get the key value using the AWS Management Console under " +
"Location Services. These operations cannot be completed using the " +
"AWS SDK. For more information about getting the key value, see " +
"the AWS Location Documentation.",
);
const sdkDisplayMapUrl = new ScenarioAction(
"sdkDisplayMapUrl",
async (/** @type {State} */ state) => {
const mapURL = `https://maps.geo.aws.amazon.com/maps/v0/maps/${state.MapName}/tiles/{z}/{x}/{y}?key=API_KEY_VALUE`;
state.mapURL = mapURL;
console.log(
`Replace \'API_KEY_VALUE\' in the following URL with the value for the API key you create and get from the AWS Management Console under Location Services. This is then the Map URL you can embed this URL in your Web app:\n
${state.mapURL}`,
);
},
);
const displayCreateGeoFenceColl = new ScenarioOutput(
"displayCreateGeoFenceColl",
"3. Create a geofence collection, which manages and stores geofences.",
);
const sdkCreateGeoFenceColl = new ScenarioAction(
"sdkCreateGeoFenceColl",
async (/** @type {State} */ state) => {
// Creates a new geofence collection.
const geoFenceCollParams = {
CollectionName: `${data.inputs.collectionName}`,
};
try {
const command = new CreateGeofenceCollectionCommand(geoFenceCollParams);
const response = await locationClient.send(command);
state.CollectionName = response.CollectionName;
console.log(
`The geofence collection was successfully created: ${state.CollectionName}`,
);
} catch (caught) {
if (caught instanceof ConflictException) {
console.error(
`An unexpected error occurred while creating the geofence collection: ${caught.message} \n Exiting program.`,
);
return;
}
}
},
);
const displayStoreGeometry = new ScenarioOutput(
"displayStoreGeometry",
"4. Store a geofence geometry in a given geofence collection. " +
"An AWS Location geofence is a virtual boundary that defines a geographic area " +
"on a map. It is a useful feature for tracking the location of " +
"assets or monitoring the movement of objects within a specific region. " +
"To define a geofence, you need to specify the coordinates of a " +
"polygon that represents the area of interest. The polygon must be " +
"defined in a counter-clockwise direction, meaning that the points of " +
"the polygon must be listed in a counter-clockwise order. " +
"This is a requirement for the AWS Location service to correctly " +
"interpret the geofence and ensure that the location data is " +
"accurately processed within the defined area.",
);
const sdkStoreGeometry = new ScenarioAction(
"sdkStoreGeometry",
async (/** @type {State} */ state) => {
const geoFenceGeoParams = {
CollectionName: `${data.inputs.collectionName}`,
GeofenceId: `${data.inputs.geoId}`,
Geometry: {
Polygon: [
[
[-122.3381, 47.6101],
[-122.3281, 47.6101],
[-122.3281, 47.6201],
[-122.3381, 47.6201],
[-122.3381, 47.6101],
],
],
},
};
try {
const command = new PutGeofenceCommand(geoFenceGeoParams);
const response = await locationClient.send(command);
state.GeoFencId = response.GeofenceId;
console.log("GeoFence created. GeoFence ID is: ", state.GeoFencId);
} catch (caught) {
if (caught instanceof ValidationException) {
console.error(
`A validation error occurred while creating geofence: ${caught.message} \n Exiting program.`,
);
return;
}
}
},
);
const displayCreateTracker = new ScenarioOutput(
"displayCreateTracker",
"5. Create a tracker resource which lets you retrieve current and historical location of devices.",
);
const sdkCreateTracker = new ScenarioAction(
"sdkCreateTracker",
async (/** @type {State} */ state) => {
//Creates a new tracker resource in your AWS account, which you can use to track the location of devices.
const createTrackerParams = {
TrackerName: `${data.inputs.trackerName}`,
Description: "Created using the JavaScript V3 SDK",
PositionFiltering: "TimeBased",
};
try {
const command = new CreateTrackerCommand(createTrackerParams);
const response = await locationClient.send(command);
state.trackerName = response.TrackerName;
console.log("Tracker created. Tracker name is : ", state.trackerName);
} catch (caught) {
if (caught instanceof ResourceNotFoundException) {
console.error(
`A validation error occurred while creating geofence: ${caught.message} \n Exiting program.`,
);
} else {
`An unexpected error error occurred: ${caught.message} \n Exiting program.`;
}
return;
}
},
);
const displayUpdatePosition = new ScenarioOutput(
"displayUpdatePosition",
"6. Update the position of a device in the location tracking system." +
"The AWS Location Service does not enforce a strict format for deviceId, but it must:\n " +
"- Be a string (case-sensitive).\n" +
"- Be 1–100 characters long.\n" +
"- Contain only: Alphanumeric characters (A-Z, a-z, 0-9); Underscores (_); Hyphens (-); and be the same ID used when sending and retrieving positions.",
);
const sdkUpdatePosition = new ScenarioAction(
"sdkUpdatePosition",
async (/** @type {State} */ state) => {
// Updates the position of a device in the location tracking system.
const updateDevicePosParams = {
TrackerName: `${data.inputs.trackerName}`,
Updates: [
{
DeviceId: `${data.inputs.deviceId}`,
SampleTime: new Date(),
Position: [-122.4194, 37.7749],
},
],
};
try {
const command = new BatchUpdateDevicePositionCommand(
updateDevicePosParams,
);
const response = await locationClient.send(command);
console.log(
`Device with id ${data.inputs.deviceId} was successfully updated in the location tracking system. `,
);
} catch (caught) {
if (caught instanceof ResourceNotFoundException) {
console.error(
`A validation error occurred while updating the device: ${caught.message} \n Exiting program.`,
);
}
}
},
);
const displayRetrievePosition = new ScenarioOutput(
"displayRetrievePosition",
"7. Retrieve the most recent position update for a specified device.",
);
const sdkRetrievePosition = new ScenarioAction(
"sdkRetrievePosition",
async (/** @type {State} */ state) => {
const devicePositionParams = {
TrackerName: `${data.inputs.trackerName}`,
DeviceId: `${data.inputs.deviceId}`,
};
try {
const command = new GetDevicePositionCommand(devicePositionParams);
const response = await locationClient.send(command);
state.position = response.Position;
console.log("Successfully fetched device position: : ", state.position);
} catch (caught) {
if (caught instanceof ResourceNotFoundException) {
console.error(
`"The resource was not found: ${caught.message} \n Exiting program.`,
);
} else {
`An unexpected error error occurred: ${caught.message} \n Exiting program.`;
}
return;
}
},
);
const displayCreateRouteCalc = new ScenarioOutput(
"displayCreateRouteCalc",
"8. Create a route calculator.",
);
const sdkCreateRouteCalc = new ScenarioAction(
"sdkCreateRouteCalc",
async (/** @type {State} */ state) => {
const routeCalcParams = {
CalculatorName: `${data.inputs.calculatorName}`,
DataSource: "Esri",
};
try {
// Creates a new route calculator with the specified name and data source.
const command = new CreateRouteCalculatorCommand(routeCalcParams);
const response = await locationClient.send(command);
state.CalculatorName = response.CalculatorName;
console.log(
"Route calculator created successfully. Calculator name is: ",
state.CalculatorName,
);
} catch (caught) {
if (caught instanceof ConflictException) {
console.error(
`An conflict occurred: ${caught.message} \n Exiting program.`,
);
return;
}
}
},
);
const displayDetermineDist = new ScenarioOutput(
"displayDetermineDist",
"9. Determine the distance between Seattle and Vancouver using the route calculator.",
);
const sdkDetermineDist = new ScenarioAction(
"sdkDetermineDist",
async (/** @type {State} */ state) => {
// Calculates the distance between two locations asynchronously.
const determineDist = {
CalculatorName: `${data.inputs.calculatorName}`,
DeparturePosition: [-122.3321, 47.6062],
DestinationPosition: [-123.1216, 49.2827],
TravelMode: "Car",
DistanceUnit: "Kilometers",
};
try {
const command = new CalculateRouteCommand(determineDist);
const response = await locationClient.send(command);
console.log(
"Successfully calculated route. The distance in kilometers is : ",
response.Summary.Distance,
);
} catch (caught) {
if (caught instanceof ResourceNotFoundException) {
console.error(
`Failed to calculate route: ${caught.message} \n Exiting program.`,
);
}
return;
}
},
);
const displayUseGeoPlacesClient = new ScenarioOutput(
"displayUseGeoPlacesClient",
"10. Use the GeoPlacesAsyncClient to perform additional operations. " +
"This scenario will show use of the GeoPlacesClient that enables" +
"location search and geocoding capabilities for your applications. " +
"We are going to use this client to perform these AWS Location tasks: \n" +
" - Reverse Geocoding (reverseGeocode): Converts geographic coordinates into addresses.\n " +
" - Place Search (searchText): Finds places based on search queries.\n " +
" - Nearby Search (searchNearby): Finds places near a specific location.\n " +
"First we will perform a Reverse Geocoding operation",
);
const sdkUseGeoPlacesClient = new ScenarioAction(
"sdkUseGeoPlacesClient",
async (/** @type {State} */ state) => {
const geoPlacesClient = new GeoPlacesClient({ region: region });
const reverseGeoCodeParams = {
QueryPosition: [-122.4194, 37.7749],
};
const searchTextParams = {
QueryText: "coffee shop",
BiasPosition: [-122.4194, 37.7749], //San Fransisco
};
const searchNearbyParams = {
QueryPosition: [-122.4194, 37.7749],
QueryRadius: Number("1000"),
};
try {
/* Performs reverse geocoding using the AWS Geo Places API.
Reverse geocoding is the process of converting geographic coordinates (latitude and longitude) to a human-readable address.
This method uses the latitude and longitude of San Francisco as the input, and prints the resulting address.*/
console.log("Use latitude 37.7749 and longitude -122.4194.");
const command = new ReverseGeocodeCommand(reverseGeoCodeParams);
const response = await geoPlacesClient.send(command);
console.log(
"Successfully calculated route. The distance in kilometers is : ",
response.ResultItems[0].Distance,
);
} catch (caught) {
if (caught instanceof ValidationException) {
console.error(
`An conflict occurred: ${caught.message} \n Exiting program.`,
);
return;
}
}
try {
console.log(
"Now we are going to perform a text search using coffee shop",
);
/*Searches for a place using the provided search query and prints the detailed information of the first result.
@param searchTextParams the search query to be used for the place search (ex, coffee shop)*/
const command = new SearchTextCommand(searchTextParams);
const response = await geoPlacesClient.send(command);
const placeId = response.ResultItems[0].PlaceId.toString();
const getPlaceCommand = new GetPlaceCommand({
PlaceId: placeId,
});
const getPlaceResponse = await geoPlacesClient.send(getPlaceCommand);
console.log(
`Detailed Place Information: \n Name and address: ${getPlaceResponse.Address.Label}`,
);
const foodTypes = getPlaceResponse.FoodTypes;
if (foodTypes.length) {
console.log("Food Types: ");
for (const foodType of foodTypes) {
console.log("- ", foodType.LocalizedName);
}
} else {
console.log("No food types available.");
}
} catch (caught) {
if (caught instanceof ValidationException) {
console.error(
`An conflict occurred: ${caught.message} \n Exiting program.`,
);
return;
}
}
try {
console.log("\nNow we are going to perform a nearby search.");
const command = new SearchNearbyCommand(searchNearbyParams);
const response = await geoPlacesClient.send(command);
const resultItems = response.ResultItems;
console.log("\nSuccessfully performed nearby search.");
for (const resultItem of resultItems) {
console.log("Name and address: ", resultItem.Address.Label);
console.log("Distance: ", resultItem.Distance);
}
} catch (caught) {
if (caught instanceof ValidationException) {
console.error(
`An conflict occurred: ${caught.message} \n Exiting program.`,
);
return;
}
}
},
);
const displayDeleteResources = new ScenarioOutput(
"displayDeleteResources",
"11. Delete the AWS Location Services resources. " +
"Would you like to delete the AWS Location Services resources? (y/n)",
);
const sdkDeleteResources = new ScenarioAction(
"sdkDeleteResources",
async (/** @type {State} */ state) => {
const deleteGeofenceCollParams = {
CollectionName: `${state.CollectionName}`,
};
const deleteRouteCalculatorParams = {
CalculatorName: `${state.CalculatorName}`,
};
const deleteTrackerParams = { TrackerName: `${state.trackerName}` };
const deleteMapParams = { MapName: `${state.MapName}` };
try {
const command = new DeleteMapCommand(deleteMapParams);
const response = await locationClient.send(command);
console.log("Map deleted.");
} catch (error) {
console.log("Error deleting map: ", error);
}
try {
const command = new DeleteGeofenceCollectionCommand(
deleteGeofenceCollParams,
);
const response = await locationClient.send(command);
console.log("Geofence collection deleted.");
} catch (error) {
console.log("Error deleting geofence collection: ", error);
}
try {
const command = new DeleteRouteCalculatorCommand(
deleteRouteCalculatorParams,
);
const response = await locationClient.send(command);
console.log("Route calculator deleted.");
} catch (error) {
console.log("Error deleting route calculator: ", error);
}
try {
const command = new DeleteTrackerCommand(deleteTrackerParams);
const response = await locationClient.send(command);
console.log("Tracker deleted.");
} catch (error) {
console.log("Error deleting tracker: ", error);
}
},
);
const goodbye = new ScenarioOutput(
"goodbye",
"Thank you for checking out the Amazon Location Service Use demo. We hope you " +
"learned something new, or got some inspiration for your own apps today!" +
" For more Amazon Location Services examples in different programming languages, have a look at: " +
"https://docs.aws.amazon.com/code-library/latest/ug/location_code_examples.html",
);
const myScenario = new Scenario("Location Services Scenario", [
greet,
pressEnter,
displayCreateAMap,
sdkCreateAMap,
pressEnter,
displayMapUrl,
sdkDisplayMapUrl,
pressEnter,
displayCreateGeoFenceColl,
sdkCreateGeoFenceColl,
pressEnter,
displayStoreGeometry,
sdkStoreGeometry,
pressEnter,
displayCreateTracker,
sdkCreateTracker,
pressEnter,
displayUpdatePosition,
sdkUpdatePosition,
pressEnter,
displayRetrievePosition,
sdkRetrievePosition,
pressEnter,
displayCreateRouteCalc,
sdkCreateRouteCalc,
pressEnter,
displayDetermineDist,
sdkDetermineDist,
pressEnter,
displayUseGeoPlacesClient,
sdkUseGeoPlacesClient,
pressEnter,
displayDeleteResources,
pressEnterConfirm,
sdkDeleteResources,
goodbye,
]);
/** @type {{ stepHandlerOptions: StepHandlerOptions }} */
export const main = async (stepHandlerOptions) => {
await myScenario.run(stepHandlerOptions);
};
// Invoke main function if this file was run directly.
if (process.argv[1] === fileURLToPath(import.meta.url)) {
const { values } = parseArgs({
options: {
yes: {
type: "boolean",
short: "y",
},
},
});
main({ confirmAll: values.yes });
}
// snippet-end:[LocationServices.javascriptv3.Scenario.basics]