

Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.

# Utilice el complemento Amazon Location MapLibre Geocoder GL
<a name="dev-maplibre-geocoder"></a>

El complemento Amazon Location MapLibre geocoder está diseñado para facilitar la incorporación de la funcionalidad Amazon Location en sus JavaScript aplicaciones cuando trabaja con la representación de mapas y la geocodificación mediante la biblioteca. [maplibre-gl-geocoder](https://github.com/maplibre/maplibre-gl-geocoder)

## Instalación
<a name="geocoder-installation"></a>

Instale el complemento Amazon Location MapLibre Geocoder desde NPM para usarlo con módulos. Escriba este comando:

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

Puede importar también archivos HTML y CSS para usarlos directamente en el navegador con un 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 con módulo: SDK independiente GeoPlaces
<a name="geocoder-module"></a>

En este ejemplo, se utiliza el [AWS SDK de la JavaScript versión 3](https://github.com/aws/aws-sdk-js-v3) para proporcionar un GeoPlacesClient elemento a la biblioteca y [AuthHelper](https://github.com/aws-geospatial/amazon-location-utilities-auth-helper-js)para autenticar el. GeoPlacesClient Lo habilita todo APIs para el 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 con un navegador: SDK independiente GeoPlaces
<a name="geocoder-browser"></a>

En este ejemplo, se utiliza el cliente de Amazon Location para realizar una solicitud que se autentica mediante una clave de API.

**nota**  
Algunos de estos ejemplos utilizan la ubicación de Amazon GeoPlacesClient. Este cliente se basa en el [AWS SDK de la JavaScript versión 3](https://github.com/aws/aws-sdk-js-v3) y permite realizar llamadas a Amazon Location mediante un script al que se hace referencia en un archivo HTML.

Incluya lo siguiente en un archivo HTML:

```
<!-- 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>
```

Incluya lo siguiente en un JavaScript archivo:

```
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());
```

## Funciones
<a name="geocoder-functions"></a>

A continuación se enumeran las funciones que se utilizan en el complemento Amazon Location MapLibre Geocoder:
+ `buildAmazonLocationMaplibreGeocoder`

  Esta clase crea una instancia del `AmazonLocationMaplibreGeocder`, que es el punto de entrada para las demás llamadas.

  Uso de llamadas a la API de `GeoPlacesClient` independientes (el cliente es una instancia de `GeoPlacesClient`):

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

  Uso de llamadas a la API de `LocationClient` consolidadas (el cliente es una instancia de `LocationClient`):

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

  Devuelve un ready-to-use IControl objeto que se puede añadir directamente a un 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);
  ```