forked from aws-samples/amazon-location-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
100 lines (90 loc) · 2.93 KB
/
index.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT-0
import { StrictMode, useState, useEffect } from "react";
import ReactDOM from "react-dom";
import Amplify, { Auth, Geo } from "aws-amplify";
import ReactMapGL, { NavigationControl } from "react-map-gl";
import { AmplifyMapLibreRequest } from "maplibre-gl-js-amplify";
import "maplibre-gl/dist/maplibre-gl.css";
import "./index.css";
import {
IDENTITY_POOL_ID,
REGION,
MAP
} from "./configuration";
import PolygonsFeature from "./components/PolygonsFeature";
// Configuring Amplify Geo with existing Amazon Cognito and Amazon Location Service information
// Fill values in configuration.js
Amplify.configure({
Auth: {
identityPoolId: IDENTITY_POOL_ID, // REQUIRED - Amazon Cognito Identity Pool ID
region: REGION, // REQUIRED - Amazon Cognito Region
},
geo: {
AmazonLocationService: {
maps: {
items: {
[MAP.NAME]: { // REQUIRED - Amazon Location Service Map resource name
style: [MAP.STYLE] // REQUIRED - String representing the style of map resource
},
},
default: [MAP.NAME], // REQUIRED - Amazon Location Service Map resource name to set as default
},
region: REGION, // REQUIRED - Amazon Location Service Region
},
}
});
const App = () => {
const [credentials, setCredentials] = useState();
const [transformRequest, setRequestTransformer] = useState();
const [viewport, setViewport] = useState({
longitude: -123.1187,
latitude: 49.2819,
zoom: 11,
});
//Fetch credentials when the app loads
useEffect(() => {
const fetchCredentials = async () => {
// Fetch AWS credentials from Amazon Cognito using Amplify Auth and storing it in state
setCredentials(await Auth.currentUserCredentials());
};
fetchCredentials();
}, []);
// Create a new transformRequest function whenever the credentials change
useEffect(() => {
if (credentials != null) {
// transformRequest is used to sign the request with AWS Sigv4 Auth
const { transformRequest } = new AmplifyMapLibreRequest(
credentials,
Geo.getDefaultMap().region
);
// Wrap the new value in an anonymous function to prevent React from recognizing it as a function and immediately calling it
setRequestTransformer(() => transformRequest);
}
}, [credentials]);
return (
<div>
{transformRequest ? (
<ReactMapGL
{...viewport}
width="100%"
height="100vh"
transformRequest={transformRequest}
mapStyle={MAP.NAME}
onViewportChange={setViewport}
>
<NavigationControl showCompass={false} style={{ left: 20, top: 20 }}/>
<PolygonsFeature />
</ReactMapGL>
) : (
<h1>Loading...</h1>
)}
</div>
);
};
ReactDOM.render(
<StrictMode>
<App />
</StrictMode>,
document.getElementById("root")
);