Page updated Jan 16, 2024

Use Amazon Location Service SDK

Amplify Geo provides solutions for common use cases with Amazon Location Service but for any functionality that is not currently supported by Amplify Geo you can access the Amazon Location Service SDK directly.

Follow this guide to get started with the aws-sdk for Amazon Location Service using Amplify Auth credentials.

Overview

In this tutorial, we’ll go over the following:

  • Setting up the AWS SDK JavaScript v3 package for the Amazon Location Service SDK calls with Amplify auth.
  • Code examples using the Amazon Location Service SDK.

Installing SDK dependencies

The first step to using the SDKs in the client is to install the necessary dependencies with the following command:

npm install @aws-sdk/client-location
1npm install @aws-sdk/client-location

Connecting your app to Amazon Location Service

In the following procedure, you’ll connect your app to the Amazon Location Service APIs.

To connect your app to the Amazon Location Service

In your React App, open src/App.js file, and call the following function to initialize the Amazon Location Service client:

import { Amplify } from 'aws-amplify'; import { fetchAuthSession } from 'aws-amplify/auth'; import { LocationClient, AssociateTrackerConsumerCommand } from '@aws-sdk/client-location'; import amplifyconfig from './amplifyconfiguration.json'; Amplify.configure(amplifyconfig); const createClient = async () => { const session = await fetchAuthSession(); const client = new LocationClient({ credentials: session.credentials, region: amplifyconfig.aws_project_region }); return client; };
1import { Amplify } from 'aws-amplify';
2import { fetchAuthSession } from 'aws-amplify/auth';
3import {
4 LocationClient,
5 AssociateTrackerConsumerCommand
6} from '@aws-sdk/client-location';
7import amplifyconfig from './amplifyconfiguration.json';
8Amplify.configure(amplifyconfig);
9
10const createClient = async () => {
11 const session = await fetchAuthSession();
12 const client = new LocationClient({
13 credentials: session.credentials,
14 region: amplifyconfig.aws_project_region
15 });
16 return client;
17};

You’ve now successfully connected your app to the Amazon Location Service.

Using the Amazon Location Service APIs

In order to access Amazon Location Service APIs, ensure you've provisioned resources and configured your app using the instructions in either Amplify CLI Geo Maps docs or the Amazon Location Service console.

You can check out the Amazon Location API Reference documentation for a complete list of supported features.

Example: Getting Device Position

This example requires you to have first provisioned a Tracker resource using the Amazon Location Service console.

The following code details how to use the Amazon Location Service APIs to update a device position and get a device position using the tracker you just created:

// UpdateDevicePosition API const params = { TrackerName: 'trackerId', Updates: [ { DeviceId: 'deviceId', Position: [-122.431297, 37.773972], SampleTime: new Date() } ] }; const command = new BatchUpdateDevicePositionCommand(params); client.send(command, (err, data) => { if (err) console.error(err); if (data) console.log(data); }); // GetDevicePosition API const client = await createClient(); const params = { TrackerName: 'trackerId', DeviceId: 'deviceId' }; const command = new GetDevicePositionCommand(params); client.send(command, (err, data) => { if (err) console.error(err); if (data) console.log(data); });
1// UpdateDevicePosition API
2const params = {
3 TrackerName: 'trackerId',
4 Updates: [
5 {
6 DeviceId: 'deviceId',
7 Position: [-122.431297, 37.773972],
8 SampleTime: new Date()
9 }
10 ]
11};
12const command = new BatchUpdateDevicePositionCommand(params);
13client.send(command, (err, data) => {
14 if (err) console.error(err);
15 if (data) console.log(data);
16});
17
18// GetDevicePosition API
19const client = await createClient();
20const params = {
21 TrackerName: 'trackerId',
22 DeviceId: 'deviceId'
23};
24const command = new GetDevicePositionCommand(params);
25client.send(command, (err, data) => {
26 if (err) console.error(err);
27 if (data) console.log(data);
28});