使用 Amazon Location MapLibre 地理编码器 GL 插件 - Amazon Location Service

使用 Amazon Location MapLibre 地理编码器 GL 插件

借助 Amazon Location MapLibre 地理编码器插件,您能够在使用 maplibre-gl-geocoder 库进行地图渲染和地理编码时,更轻松地将 Amazon Location 功能整合到 JavaScript 应用程序中。

安装

从 NPM 安装 Amazon Location MapLibre 地理编码器插件,以便与模块一起使用。键入以下命令:

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

您也可以使用以下脚本导入 HTML 和 CSS 文件,以便直接在浏览器中使用:

<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" />

与模块一起使用 - 独立的 GeoPlaces SDK

此示例使用适用于 JavaScript V3 的 AWS SDK 获取 GeoPlacesClient 以提供给库,并使用 AuthHelper 对 GeoPlacesClient 进行身份验证。它为地理编码器启用所有 API。

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

与浏览器结合使用 - 独立的 GeoPlaces SDK

此示例使用 Amazon Location 客户端发出通过 API 密钥进行身份验证的请求。

注意

其中一些示例使用 Amazon Location GeoPlacesClient。该客户端基于适用于 JavaScript V3 的 AWS SDK,允许通过 HTML 文件中引用的脚本调用 Amazon Location。

在 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>

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

函数

下面列出了 Amazon Location MapLibre 地理编码器插件中使用的函数:

  • buildAmazonLocationMaplibreGeocoder

    该类创建 AmazonLocationMaplibreGeocder 的一个实例,此实例是其他所有调用的入口点。

    使用独立的 GeoPlacesClient API 调用(客户端是 GeoPlacesClient 的实例):

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

    使用整合的 LocationClient API 调用(客户端是 LocationClient 的实例):

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

    返回可以直接添加到地图中的即用型 IControl 对象。

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