Skip to content

Commit 1c42fb6

Browse files
stable documentation before vertical promotion
1 parent eda67a1 commit 1c42fb6

File tree

6 files changed

+798
-33
lines changed

6 files changed

+798
-33
lines changed

src/directory/directory.mjs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,9 @@ export const directory = {
551551
{
552552
path: 'src/pages/[platform]/build-a-backend/add-aws-services/geo/existing-resources/index.mdx'
553553
},
554+
{
555+
path: 'src/pages/[platform]/build-a-backend/add-aws-services/geo/use-legacy-resources/index.mdx'
556+
},
554557
{
555558
path: 'src/pages/[platform]/build-a-backend/add-aws-services/geo/google-migration/index.mdx'
556559
},

src/pages/[platform]/build-a-backend/add-aws-services/geo/configure-location-search/index.mdx

Lines changed: 0 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -68,31 +68,3 @@ const backend = defineBackend({
6868
## Location Search Index Pricing Plan
6969
The pricing plan for Search Index will be set to `RequestBasedUsage`.
7070
We advice you to go through the [location service pricing](https://aws.amazon.com/location/pricing/) along with the [location service terms](https://aws.amazon.com/service-terms/) (_82.5 section_) to learn more about the pricing plan.
71-
72-
73-
{/* MOVE THIS TO LEGACY RESOURCES PAGE */}
74-
{/* ## Advanced Settings
75-
You can optionally configure the data provider and result storage location for your location search index.
76-
77-
### Location Search data provider
78-
You can select a data provider as the source for geocoding, reverse geocoding and searches.
79-
Each provider gathers and curates their data using different means. They may also have varying expertise in different regions of the world.
80-
The available data providers of geospatial data are shown. To learn more about data providers, please refer this [location service documentation](https://docs.aws.amazon.com/location/latest/developerguide/what-is-data-provider.html).
81-
82-
- Here – For additional information about HERE Technologies, see [Here guide](https://docs.aws.amazon.com/location/latest/developerguide/HERE.html).
83-
- Esri – For additional information about Esri, see [Esri guide](https://docs.aws.amazon.com/location/latest/developerguide/esri.html).
84-
85-
<Callout>
86-
87-
**Note:** If your application is tracking or routing assets you use in your business (such as delivery vehicles or employees), you may only use `HERE` as your geolocation provider.
88-
See section 82 of the [AWS service terms](https://aws.amazon.com/service-terms/) for more details.
89-
90-
</Callout>
91-
92-
### Location Search result storage location
93-
You can specify how the results of a search operation will be stored by the caller.
94-
95-
- SingleUse - specifies that the results won't be stored.
96-
- Storage - specifies that the result can be cached or stored in a database.
97-
98-
Refer [this location service doc](https://docs.aws.amazon.com/location-places/latest/APIReference/API_DataSourceConfiguration.html#locationplaces-Type-DataSourceConfiguration-IntendedUse) for more information. */}

src/pages/[platform]/build-a-backend/add-aws-services/geo/location-search/index.mdx

Lines changed: 115 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,80 @@ To add a location search UI component to your map, you can use the [maplibre-gl-
4444
Install the necessary dependencies with the following command:
4545

4646
```bash title="Terminal" showLineNumbers={false}
47-
npm add @maplibre/maplibre-gl-geocoder maplibre-gl@1 maplibre-gl-js-amplify
47+
npm add @maplibre/maplibre-gl-geocoder \
48+
maplibre-gl@1 \
49+
maplibre-gl-js-amplify \
50+
@aws-sdk/client-geo-places \
51+
@aws/amazon-location-utilities-auth-helper
4852
```
4953

5054
> **Note:** Make sure that `maplibre-gl-js-amplify` version `4.0.0` or above is installed.
5155
5256
First, create a map onto which you want to add the location search UI component. See the guide on [creating and displaying maps](/[platform]/build-a-backend/add-aws-services/geo/maps).
5357

54-
Then, use `createAmplifyGeocoder()` to get a new instance of `MaplibreGeocoder` and add the location search UI component to the map.
58+
<BlockSwitcher>
59+
<Block name="API Key">
60+
61+
Start off my getting an instance of GeoPlaces by authenticating using the API key and the `AmazonLocationClient`.
62+
63+
```javascript
64+
import { GeoPlaces, GeoPlacesClient } from "@aws-sdk/client-geo-places";
65+
import { withAPIKey } from "@aws/amazon-location-utilities-auth-helper";
66+
67+
const geoPlaces = getGeoPlaces();
68+
69+
function getGeoPlaces() {
70+
const authHelper = amazonLocationClient.withAPIKey(API_KEY, AWS_REGION); // Authenticate using the API key and AWS region
71+
const locationClient = new amazonLocationClient.GeoPlacesClient(authHelper.getClientConfig()); // Create a GeoPlaces client
72+
const geoPlaces = new GeoPlaces(locationClient); // Create GeoPlaces instance
73+
return geoPlaces; // Return the GeoPlaces instance
74+
}
75+
```
76+
77+
Create a new instance of `MaplibreGeocoder` and add the location search UI component to the map. Use the `map` from [this](/[platform]/build-a-backend/add-aws-services/geo/maps/#display-a-map) set up tutorial.
78+
79+
> **Note:** Ensure that your package bundler (webpack, rollup, etc) is configured to handle css files. Check out the webpack documentation [here](https://webpack.js.org/loaders/css-loader/).
80+
81+
```javascript
82+
import maplibregl from "maplibre-gl";
83+
import "maplibre-gl/dist/maplibre-gl.css";
84+
import "@maplibre/maplibre-gl-geocoder/dist/maplibre-gl-geocoder.css";
85+
import "maplibre-gl-js-amplify/dist/public/amplify-geocoder.css"; // Optional CSS for Amplify recommended styling
86+
87+
async function initializeMap(mapStyle = "Standard", colorScheme = "Dark") {
88+
const styleUrl = `https://maps.geo.${AWS_REGION}.amazonaws.com/v2/styles/${mapStyle}/descriptor?key=${API_KEY}&color-scheme=${colorScheme}`;
89+
const map = new maplibregl.Map({
90+
container: 'map', // The ID of the map container
91+
style: styleUrl, // The style URL for the map
92+
center: [-123.1187, 49.2819], // Starting center coordinates
93+
zoom: 11, // Initial zoom levels
94+
validateStyle: false // Disable style validation
95+
});
96+
return map; // Return the initialized map
97+
}
98+
99+
const map = initializeMap();
100+
101+
// highlight-start
102+
const geocoder = new MaplibreGeocoder(geoPlaces, {
103+
maplibregl,
104+
showResultsWhileTyping: true, // Show results while typing
105+
debounceSearch: 300, // Debounce search requests
106+
limit: 30, // Limit number of results
107+
reverseGeocode: true, // Enable reverse geocoding
108+
zoom: 14, // Zoom level on result selection
109+
placeholder: "Search text or nearby (lat,long)" // Place holder text for search box.
110+
});
111+
112+
// Add the search box to the map
113+
map.addControl(geocoder, 'top-left');
114+
// highlight-end
115+
```
116+
</Block>
117+
118+
<Block name="Legacy Resources">
119+
120+
Use the `createAmplifyGeocoder()` to get a new instance of `MaplibreGeocoder` and add the location search UI component to the map.
55121

56122
> **Note:** Ensure that your package bundler (webpack, rollup, etc) is configured to handle css files. Check out the webpack documentation [here](https://webpack.js.org/loaders/css-loader/).
57123
@@ -78,11 +144,25 @@ async function initializeMap() {
78144

79145
initializeMap();
80146
```
147+
</Block>
148+
</BlockSwitcher>
81149

82150
![A search box on the top right corner of a map](/images/geocoder-search-box-map.png)
83151

84152
### Display the location search box outside the map
85153

154+
<BlockSwitcher>
155+
<Block name="API Key">
156+
You can also use maplibre-gl-geocoder to display the location search UI component anywhere in your application, even outside the map.
157+
158+
Use `geocoder` from above to create a new `MaplibreGeocoder` and add it within your Amplify app.
159+
160+
```javascript
161+
document.getElementById("search").appendChild(geocoder.onAdd());
162+
```
163+
</Block>
164+
165+
<Block name="Legacy Resources">
86166
You can also use [maplibre-gl-geocoder](https://github.com/maplibre/maplibre-gl-geocoder) to display the location search UI component anywhere in your application, even outside the map.
87167

88168
To do so, extract the html element using function `onAdd()` and attach it anywhere in your DOM instead of adding it via the map's `addControl()` function.
@@ -91,11 +171,42 @@ To do so, extract the html element using function `onAdd()` and attach it anywhe
91171
const geocoder = createAmplifyGeocoder();
92172
document.getElementById("search").appendChild(geocoder.onAdd());
93173
```
174+
</Block>
175+
</BlockSwitcher>
94176

95177
![A search box with multiple Starbucks locations](/images/geocoder-search-box.png)
96178

97179
### Customize Search Icons
98180

181+
<BlockSwitcher>
182+
<Block name="API Keys">
183+
You can customize the search icons used by the [maplibre-gl-geocoder](https://github.com/maplibre/maplibre-gl-geocoder) to use any image of your choosing. [MapLibre markers](https://maplibre.org/maplibre-gl-js/docs/API/#markers-and-controls) require an [HTMLElement](https://developer.mozilla.org/en-US/docs/Web/HTML/Element) when passing in custom images.
184+
185+
The following example puts an existing SVG icon into an HTMLElement before being passed to `createAmplifyGeocoder` which creates a [maplibre-gl-geocoder](https://github.com/maplibre/maplibre-gl-geocoder).
186+
187+
```javascript
188+
import myIcon from "./myIcon.svg" // relative path to your custom icon
189+
190+
const icon = new Image(100, 100);
191+
icon.src = myIcon;
192+
193+
const geocoder = new MaplibreGeocoder(geoPlaces, {
194+
maplibregl,
195+
showResultsWhileTyping: true, // Show results while typing
196+
debounceSearch: 300, // Debounce search requests
197+
limit: 30, // Limit number of results
198+
reverseGeocode: true, // Enable reverse geocoding
199+
zoom: 14, // Zoom level on result selection
200+
placeholder: "Search text or nearby (lat,long)", // Place holder text for search box.
201+
// highlight-next-line
202+
showResultMarkers: { element: icon }
203+
});
204+
205+
map.addControl(geocoder);
206+
```
207+
</Block>
208+
209+
<Block name="Legacy Resources">
99210
You can customize the search icons used by the [maplibre-gl-geocoder](https://github.com/maplibre/maplibre-gl-geocoder) to use any image of your choosing. [MapLibre markers](https://maplibre.org/maplibre-gl-js/docs/API/#markers-and-controls) require an [HTMLElement](https://developer.mozilla.org/en-US/docs/Web/HTML/Element) when passing in custom images.
100211

101212
The following example puts an existing SVG icon into an HTMLElement before being passed to `createAmplifyGeocoder` which creates a [maplibre-gl-geocoder](https://github.com/maplibre/maplibre-gl-geocoder).
@@ -109,6 +220,8 @@ icon.src = myIcon;
109220
const geocoder = createAmplifyGeocoder({ showResultMarkers: { element: icon } });
110221
map.addControl(geocoder);
111222
```
223+
</Block>
224+
</BlockSwitcher>
112225

113226
![A search box on a map with multiple pointers](/images/geocoder-custom-images.png)
114227

src/pages/[platform]/build-a-backend/add-aws-services/geo/maps/index.mdx

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,50 @@ npm add maplibre-gl maplibre-gl-js-amplify
5252
Verify the following:
5353

5454
- `maplibre-gl-js-amplify` version `4.0.0` or above is installed
55-
- Any package bundlers (webpack, rollup, etc) are configured to handle css files. Check out the webpack documentation [here](https://webpack.js.org/loaders/css-loader/).
55+
- Any package bundlers (webpack, rollup, etc) are configured to handle css files. Check out the webpack documentation [here](https://webpack.js.org/loaders/css-loader/).
5656

5757
</Callout>
5858

59+
<BlockSwitcher>
60+
61+
<Block name="API Keys">
62+
63+
You can render your map using an existing or provisioned API key. To generate API keys with Amplify Geo, visit the [set up page](/[platform]/build-a-backend/add-aws-services/geo/set-up-geo/#generate-api-keys) to learn more about setting up API keys.
64+
65+
Start by importing the following libraries into your application:
66+
67+
```ts
68+
import * from 'maplibre-gl-js' as maplibregl;
69+
import 'maplibre-gl/dist/maplibre-gl.css';
70+
```
71+
72+
Next, create and render the [Map](https://maplibre.org/maplibre-gl-js/docs/API/classes/Map/) by creating an instance through .
73+
74+
**Note:** There must be a `div` with an `id="map"` on the DOM before making the call to `maplibregl.Map` in this way.
75+
76+
With the region of your generated API key and the hidden value, you can use the `maplibregl.Map` function to render a map with a customized style and color scheme. Visit the official Amazon Location Service documentation to learn more about [style specifications](https://docs.aws.amazon.com/location/latest/developerguide/map-styles.html).
77+
```javascript
78+
const API_KEY = "Your_API_Key"; // your hidden value (should be v1.public.a1b2c3d4...)
79+
const AWS_REGION = "Region_where_you_created_API_Key";
80+
81+
function initializeMap(mapStyle = "Standard", colorScheme = "Dark") {
82+
const styleUrl = `https://maps.geo.${AWS_REGION}.amazonaws.com/v2/styles/${mapStyle}/descriptor?key=${API_KEY}&color-scheme=${colorScheme}`;
83+
const map = new maplibregl.Map({
84+
container: 'map', // The ID of the map container
85+
style: styleUrl, // The style URL for the map
86+
center: [-123.1187, 49.2819], // Starting center coordinates
87+
zoom: 11, // Initial zoom levels
88+
validateStyle: false // Disable style validation
89+
});
90+
return map; // Return the initialized map
91+
}
92+
93+
const map = initializeMap();
94+
```
95+
</Block>
96+
97+
<Block name="Legacy Resources">
98+
5999
Import the library into your application:
60100

61101
```javascript
@@ -117,6 +157,10 @@ body,
117157

118158
</Callout>
119159

160+
</Block>
161+
162+
</BlockSwitcher>
163+
120164
![A map centered on Vancouver](/images/display-map.png)
121165

122166
## Display markers on map

src/pages/[platform]/build-a-backend/add-aws-services/geo/set-up-geo/index.mdx

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function getStaticProps(context) {
2929

3030
In this guide, you will learn how to set up Geo in your Amplify app. You will set up your backend resources, manage their access, and integrate geo maps, place indices, and geofence collections within your application.
3131

32-
If you have not yet created an Amplift app, visit the [quickstart guide](/[platform]/start/quickstart/).
32+
If you have not yet created an Amplify app, visit the [quickstart guide](/[platform]/start/quickstart/).
3333

3434
Amazon Location Services now offers independent APIs and UI components for maps and location search for your web apps, eliminating the need to provision these resources. You can add maps, location search (place index), and geofencing functionality to your app in just a few lines of code. Learn more about these resources [here](https://docs.aws.amazon.com/location/latest/developerguide/what-is.html).
3535

@@ -111,7 +111,7 @@ This command will deploy a Geofence Collection to your cloud sandbox and generat
111111

112112
## Define resource access permissions
113113

114-
All maps, place indices, and geofence collections requested by Amplify Geo are inaccessible by users or other resources by default. Access to these resources must be explicitly granted within the `access` callback of the `defineMap`, `definePlace`, and `defineCollection` functions. Refer to the [access actions](#resource-access-actions) for all resource types to pick appropriate actions for your API access definitions.
114+
All maps, place indices, and geofence collections requested by Amplify Geo are inaccessible by users or other resources by default. Access to these resources must be explicitly granted within the `access` callback of the `defineMap`, `definePlace`, and `defineCollection` functions. Refer to [this](/[platform]/build-a-backend/add-aws-services/geo/custom-authorization/) page to learn more about access customization and possible access actions.
115115

116116
The following example shows you how you can set up your map and collection access structure for authorized and guest users.
117117

@@ -137,6 +137,64 @@ export const collection = defineCollection({
137137

138138
<InlineFilter filters={['javascript', "angular", "react", "vue", "react-native", "nextjs"]}>
139139

140+
<Callout warning="true">
141+
142+
AWS Location API keys currently do not support geofence collections or their associated APIs. Adding acesss definitions for any API keys using the `allow.apiKey.to()` definition will NOT result in the creation of an API key.
143+
144+
</Callout>
145+
146+
## Generate API Keys
147+
148+
You can use AWS Location API keys to authenticate and authorize anonymous users to use Geo resources. Refer to the [Amazon Location documentation](https://docs.aws.amazon.com/location/latest/developerguide/access.html) to learn more about authentication with API keys.
149+
150+
To generate API keys for your map and location resources, you can configure their generation during resource instantiation.
151+
152+
```ts title="amplify/geo/resource.ts"
153+
export const map = defineMap({
154+
name: 'amplifyMap',
155+
access: (allow) => [
156+
allow.authenticated.to(['get'])
157+
// highlight-next-line
158+
allow.apiKey.to(['get'])
159+
],
160+
// highlight-start
161+
apiKeyProps: {
162+
apiKeyName: 'testMapKey',
163+
noExpiry: true
164+
}
165+
// highlight-end
166+
});
167+
168+
export const collection = defineCollection({
169+
name: 'amplifyCollection',
170+
access: (allow) => [
171+
allow.authenticated.to(['search', 'geocode', 'autocomplete']),
172+
allow.guest.to(['search']),
173+
// highlight-next-line
174+
allow.apiKey.to(['search', 'geocode'])
175+
],
176+
// highlight-start
177+
apiKeyProps: {
178+
apiKeyName: 'testIndexKey',
179+
noExpiry: false
180+
}
181+
// highlight-end
182+
});
183+
```
184+
185+
This will generate AWS Location API keys configured with the restrictions provided in the `access` block. The names of all provisioned API keys will then appear in the `amplify_outputs.json` file. To access their hidden values through AWS SDK/CLI, refer to the instructions below.
186+
187+
Install the latest version of the [AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) and configure it with your AWS account.
188+
189+
```bash title="Terminal" showLineNumbers={false}
190+
aws location describe-key \
191+
--key-name <your-key-name>
192+
```
193+
194+
<Callout>
195+
**Note:** To access information about generated API keys, you must ensure that `geo:DescribeKey` access is granted to your user roles and resources. learn more about setting up access for Location resources [here](https://docs.aws.amazon.com/location/latest/developerguide/set-up.html).
196+
</Callout>
197+
140198
## Configure your application
141199

142200
To display a map in your application, you can use the [Amplify React MapView component](https://ui.docs.amplify.aws/react/components/geo) or the [MapLibre GL](https://github.com/maplibre/maplibre-gl-js) with `maplibre-gl-js-amplify` libraries are required.

0 commit comments

Comments
 (0)