

As traduções são geradas por tradução automática. Em caso de conflito entre o conteúdo da tradução e da versão original em inglês, a versão em inglês prevalecerá.

# Use o plugin Amazon Location MapLibre Geocoder GL
<a name="dev-maplibre-geocoder"></a>

O plug-in do MapLibre geocodificador Amazon Location foi projetado para facilitar a incorporação da funcionalidade Amazon Location em seus JavaScript aplicativos, ao trabalhar com renderização de mapas e geocodificação usando a biblioteca. [maplibre-gl-geocoder](https://github.com/maplibre/maplibre-gl-geocoder)

## Instalação
<a name="geocoder-installation"></a>

Instale o plug-in Amazon Location MapLibre geocoder do NPM para uso com módulos. Digite este comando:

```
npm install @aws/amazon-location-for-maplibre-gl-geocoder
```

Você também pode importar arquivos HTML e CSS para uso diretamente no navegador, com um script:

```
<script src="https://cdn.jsdelivr.net/npm/@aws/amazon-location-for-maplibre-gl-geocoder@2"></script>
<link
  href="https://cdn.jsdelivr.net/npm/@aws/amazon-location-for-maplibre-gl-geocoder@2/dist/amazon-location-for-mlg-styles.css"
  rel="stylesheet"
/>
```

## Uso com módulo - SDK autônomo GeoPlaces
<a name="geocoder-module"></a>

Este exemplo usa o [AWS SDK para JavaScript V3](https://github.com/aws/aws-sdk-js-v3) para obter um GeoPlacesClient para fornecer à biblioteca e [AuthHelper](https://github.com/aws-geospatial/amazon-location-utilities-auth-helper-js)para autenticar o. GeoPlacesClient Ele habilita tudo APIs para o geocodificador.

```
// Import MapLibre GL JS
import maplibregl from "maplibre-gl";
// Import from the AWS JavaScript SDK V3
import { GeoPlacesClient } from "@aws-sdk/client-geo-places";
// Import the utility functions
import { withAPIKey } from "@aws/amazon-location-utilities-auth-helper";
// Import the AmazonLocationMaplibreGeocoder
import {
  buildAmazonLocationMaplibreGeocoder,
  AmazonLocationMaplibreGeocoder,
} from "@aws/amazon-location-for-maplibre-gl-geocoder";

const apiKey = "<API Key>";
const mapName = "Standard";
const region = "<Region>"; // region containing Amazon Location API Key

// Create an authentication helper instance using an API key and region
const authHelper = await withAPIKey(apiKey, region);

const client = new GeoPlacesClient(authHelper.getClientConfig());

// Render the map
const map = new maplibregl.Map({
  container: "map",
  center: [-123.115898, 49.295868],
  zoom: 10,
  style: `https://maps.geo.${region}.amazonaws.com/v2/styles/${mapStyle}/descriptor?key=${apiKey}`,
});

// Gets an instance of the AmazonLocationMaplibreGeocoder Object.
const amazonLocationMaplibreGeocoder = buildAmazonLocationMaplibreGeocoder(client, { enableAll: true });

// Now we can add the Geocoder to the map.
map.addControl(amazonLocationMaplibreGeocoder.getPlacesGeocoder());
```

## Uso com um navegador - SDK autônomo GeoPlaces
<a name="geocoder-browser"></a>

Este exemplo usa o cliente do Amazon Location para fazer uma solicitação que se autentica usando a chave de API.

**nota**  
Alguns desses exemplos usam o Amazon Location GeoPlacesClient. Esse cliente é baseado no [AWS SDK para JavaScript V3](https://github.com/aws/aws-sdk-js-v3) e permite fazer chamadas para a Amazon Location por meio de um script referenciado em um arquivo HTML.

Inclua o código a seguir no arquivo:

```
<!-- Import the Amazon Location For Maplibre Geocoder -->
<script src="https://cdn.jsdelivr.net/npm/@aws/amazon-location-for-maplibre-gl-geocoder@2"></script>
<link
  href="https://cdn.jsdelivr.net/npm/@aws/amazon-location-for-maplibre-gl-geocoder@2/dist/amazon-location-for-mlg-styles.css"
  rel="stylesheet"
/>
<!-- Import the Amazon GeoPlacesClient -->
<script src="https://cdn.jsdelivr.net/npm/@aws/amazon-location-client@1"></script>
```

Inclua o seguinte em um JavaScript arquivo:

```
const apiKey = "<API Key>";
const mapStyle = "Standard";
const region = "<Region>"; // region containing Amazon Location API key

// Create an authentication helper instance using an API key and region
const authHelper = await amazonLocationClient.withAPIKey(apiKey, region);

const client = new amazonLocationClient.GeoPlacesClient(authHelper.getClientConfig());

// Render the map
const map = new maplibregl.Map({
  container: "map",
  center: [-123.115898, 49.295868],
  zoom: 10,
  style: `https://maps.geo.${region}.amazonaws.com/v2/styles/${mapStyle}/descriptor?key=${apiKey}`,
});

// Initialize the AmazonLocationMaplibreGeocoder object
const amazonLocationMaplibreGeocoderObject = amazonLocationMaplibreGeocoder.buildAmazonLocationMaplibreGeocoder(
  client,
  { enableAll: true },
);

// Use the AmazonLocationWithMaplibreGeocoder object to add a geocoder to the map.
map.addControl(amazonLocationMaplibreGeocoderObject.getPlacesGeocoder());
```

## Funções
<a name="geocoder-functions"></a>

Abaixo estão listadas as funções usadas no plug-in de MapLibre geocodificador Amazon Location:
+ `buildAmazonLocationMaplibreGeocoder`

  Essa classe cria uma instância do `AmazonLocationMaplibreGeocder` que é o ponto de entrada para todas as outras chamadas.

  Usando chamadas de API `GeoPlacesClient` autônomas (o cliente é instância de `GeoPlacesClient`):

  ```
  const amazonLocationMaplibreGeocoder = buildAmazonLocationMaplibreGeocoder(client, { enableAll: true });
  ```

  Usando chamadas de API `LocationClient` consolidadas (o cliente é instância de `LocationClient`):

  ```
  const amazonLocationMaplibreGeocoder = buildAmazonLocationMaplibreGeocoder(client, {
    enableAll: true,
    placesIndex: placeIndex,
  });
  ```
+ `getPlacesGeocoder`

  Retorna um ready-to-use IControl objeto que pode ser adicionado diretamente a um mapa.

  ```
  const geocoder = getPlacesGeocoder();
  
  // Initialize map see: <insert link to initializing a map instance here>
  let map = await initializeMap();
  
  // Add the geocoder to the map.
  map.addControl(geocoder);
  ```