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 add @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 outputs from '../amplify_outputs.json';Amplify.configure(outputs);
const createClient = async () => { const session = await fetchAuthSession(); const client = new LocationClient({ credentials: session.credentials, region: amplifyconfig.aws_project_region }); return client;};
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 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 APIconst 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 APIconst 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);});