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
// 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
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:
buildAmazonLocationMaplibreGeocoderEssa classe cria uma instância do
AmazonLocationMaplibreGeocderque é o ponto de entrada para todas as outras chamadas.Usando chamadas de API
GeoPlacesClientautônomas (o cliente é instância deGeoPlacesClient):const amazonLocationMaplibreGeocoder = buildAmazonLocationMaplibreGeocoder(client, { enableAll: true });Usando chamadas de API
LocationClientconsolidadas (o cliente é instância deLocationClient):const amazonLocationMaplibreGeocoder = buildAmazonLocationMaplibreGeocoder(client, { enableAll: true, placesIndex: placeIndex, });getPlacesGeocoderRetorna 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);