1919import java .util .concurrent .CompletionException ;
2020
2121// snippet-start:[location.java2.scenario.main]
22+ /*
23+ * Before running this Java V2 code example, set up your development
24+ * environment, including your credentials.
25+ *
26+ * For more information, see the following documentation topic:
27+ *
28+ * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
29+ *
30+ */
2231public class LocationScenario {
2332
2433 public static final String DASHES = new String (new char [80 ]).replace ("\0 " , "-" );
@@ -43,18 +52,18 @@ public static void main(String[] args) {
4352 deviceId - The ID of the device (e.g., "iPhone-112356").
4453 """ ;
4554
46- if (args .length != 7 ) {
47- logger .info (usage );
48- return ;
49- }
55+ // if (args.length != 7) {
56+ // logger.info(usage);
57+ // return;
58+ // }
5059
51- String mapName = args [0 ];
52- String keyName = args [1 ];
53- String collectionName = args [2 ];
54- String geoId = args [3 ];
55- String trackerName = args [4 ];
56- String calculatorName = args [5 ];
57- String deviceId = args [6 ];
60+ String mapName = "AWSMap200" ; // args[0];
61+ String keyName = "AWSApiKey200" ; // args[1];
62+ String collectionName = "AWSLocationCollection200" ; // args[2];
63+ String geoId = "geoId200" ; // args[3];
64+ String trackerName = "geoTracker200" ; // args[4];
65+ String calculatorName = "AWSRouteCalc200" ; // args[5];
66+ String deviceId = "iPhone-112356" ; // args[6];
5867
5968 logger .info ("""
6069 AWS Location Service is a fully managed service offered by Amazon Web Services (AWS) that
@@ -82,6 +91,8 @@ location names into geographic coordinates (latitude and longitude),\s
8291 try {
8392 runScenario (mapName , keyName , collectionName , geoId , trackerName , calculatorName , deviceId );
8493 } catch (RuntimeException e ) {
94+ // Clean up AWS Resources.
95+ cleanUp (mapName , keyName , collectionName , trackerName , calculatorName );
8596 logger .info (e .getMessage ());
8697 }
8798 }
@@ -145,9 +156,10 @@ restrict API keys to specific AWS Location operations (e.g., only
145156 In order to get the MAP URL, you need to get the API Key value.
146157 You can get the key value using the AWS Management Console under
147158 Location Services. This operation cannot be completed using the
148- AWS SDK.
159+ AWS SDK. For more information about getting the key value, see
160+ https://docs.aws.amazon.com/location/latest/developerguide/using-apikeys.html.
149161 """ );
150- String mapUrl = "https://maps.geo.aws.amazon.com/maps/v0/maps/{MapName} /tiles/{z}/{x}/{y}?key={KeyValue}" ;
162+ String mapUrl = "https://maps.geo.aws.amazon.com/maps/v0/maps/" + mapName + " /tiles/{z}/{x}/{y}?key={KeyValue}" ;
151163 logger .info ("Embed this URL in your Web app: " + mapUrl );
152164 logger .info ("" );
153165 waitForInputToContinue (scanner );
@@ -291,7 +303,7 @@ restrict API keys to specific AWS Location operations (e.g., only
291303 waitForInputToContinue (scanner );
292304 try {
293305 CalculateRouteResponse response = locationActions .calcDistanceAsync (calculatorName ).join ();
294- logger .info ("Successfully calculated route. The distance is {}" , response .summary ().distance ());
306+ logger .info ("Successfully calculated route. The distance in kilometers is {}" , response .summary ().distance ());
295307 } catch (CompletionException ce ) {
296308 Throwable cause = ce .getCause ();
297309 if (cause instanceof ResourceNotFoundException ) {
@@ -325,7 +337,7 @@ restrict API keys to specific AWS Location operations (e.g., only
325337 waitForInputToContinue (scanner );
326338
327339 logger .info ("Now we are going to perform a nearby Search." );
328- waitForInputToContinue (scanner );
340+ // waitForInputToContinue(scanner);
329341 locationActions .searchNearBy ().join ();
330342 waitForInputToContinue (scanner );
331343 } catch (CompletionException ce ) {
@@ -343,21 +355,7 @@ restrict API keys to specific AWS Location operations (e.g., only
343355 logger .info ("Would you like to delete the AWS Location Services resources? (y/n)" );
344356 String delAns = scanner .nextLine ().trim ();
345357 if (delAns .equalsIgnoreCase ("y" )) {
346- try {
347- locationActions .deleteMap (mapName ).join ();
348- locationActions .deleteKey (keyName ).join ();
349- locationActions .deleteGeofenceCollectionAsync (collectionName ).join ();
350- locationActions .deleteTracker (trackerName ).join ();
351- locationActions .deleteRouteCalculator (calculatorName ).join ();
352- } catch (CompletionException ce ) {
353- Throwable cause = ce .getCause ();
354- if (cause instanceof ResourceNotFoundException ) {
355- logger .info ("The resource was not found: {}" , cause .getMessage (), cause );
356- } else {
357- logger .info ("An unexpected error occurred: {}" , cause .getMessage (), cause );
358- }
359- return ;
360- }
358+ cleanUp (mapName , keyName , collectionName , trackerName , calculatorName );
361359 } else {
362360 logger .info ("The AWS resources will not be deleted." );
363361 }
@@ -369,6 +367,33 @@ restrict API keys to specific AWS Location operations (e.g., only
369367 logger .info (DASHES );
370368 }
371369
370+ /**
371+ * Cleans up resources by deleting the specified map, key, geofence collection, tracker, and route calculator.
372+ *
373+ * @param mapName The name of the map to delete.
374+ * @param keyName The name of the key to delete.
375+ * @param collectionName The name of the geofence collection to delete.
376+ * @param trackerName The name of the tracker to delete.
377+ * @param calculatorName The name of the route calculator to delete.
378+ */
379+ private static void cleanUp (String mapName , String keyName , String collectionName , String trackerName , String calculatorName ) {
380+ try {
381+ locationActions .deleteMap (mapName ).join ();
382+ locationActions .deleteKey (keyName ).join ();
383+ locationActions .deleteGeofenceCollectionAsync (collectionName ).join ();
384+ locationActions .deleteTracker (trackerName ).join ();
385+ locationActions .deleteRouteCalculator (calculatorName ).join ();
386+ } catch (CompletionException ce ) {
387+ Throwable cause = ce .getCause ();
388+ if (cause instanceof ResourceNotFoundException ) {
389+ logger .info ("The resource was not found: {}" , cause .getMessage (), cause );
390+ } else {
391+ logger .info ("An unexpected error occurred: {}" , cause .getMessage (), cause );
392+ }
393+ return ;
394+ }
395+ }
396+
372397 private static void waitForInputToContinue (Scanner scanner ) {
373398 while (true ) {
374399 logger .info ("" );
0 commit comments