Usar o plug-in do geocodificador GL do MapLibre para Amazon Location - Amazon Location Service

Usar o plug-in do geocodificador GL do MapLibre para Amazon Location

O plug-in de geocodificador do MapLibre para Amazon Location foi projetado para facilitar a incorporação da funcionalidade do Amazon Location em suas aplicações JavaScript ao trabalhar com renderização e geocodificação de mapas usando a biblioteca maplibre-gl-geocoder.

Instalação

Instale o plug-in do geocodificador do MapLibre para Amazon Location no NPM para uso com os 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 do GeoPlaces

Este exemplo usa o SDK para JavaScript V3 da AWS para obter um GeoPlacesClient para fornecer à biblioteca e o AuthHelper para autenticar o GeoPlacesClient. Ele ativa todas as 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 do GeoPlaces

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 GeoPlacesClient do Amazon Location. O cliente é baseado no SDK para JavaScript V3 da AWS e permite fazer chamadas para o 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 arquivo JavaScript:

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

Abaixo estão listadas as funções usadas no plug-in do geocodificador do MapLibre para 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 objeto IControl pronto para uso 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);