本文属于机器翻译版本。若本译文内容与英语原文存在差异,则一律以英文原文为准。
查看最终的 Amazon 定位申请
此应用程序的最终源代码包含在本部分中。您也可以在上
您还可以找到使用 Amazon Cognito 而不是API密钥的应用程序版本。 GitHub
- Overview
-
选择每个选项卡,查看本快速入门教程中文件的最终源代码。
文件如下:
quickstart.html — 应用程序的框架,包括地图和搜索结果的HTML元素持有者。
main.css——应用程序的样式表。
main.js——应用程序的脚本,用于对用户进行身份验证、创建地图和在
click
事件上进行搜索。
- quickstart.html
-
快速入门应用程序的HTML框架。
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Quick start tutorial</title> <!-- Styles --> <link href="https://unpkg.com/maplibre-gl@3.x/dist/maplibre-gl.css" rel="stylesheet" /> <link href="main.css" rel="stylesheet" /> </head> <body> ... <footer>This is a simple Amazon Location Service app. Pan and zoom. Click to see details about entities close to a point.</footer> <!-- JavaScript dependencies --> <script src="https://unpkg.com/maplibre-gl@3.x/dist/maplibre-gl.js"></script> <script src="https://unpkg.com/@aws/amazon-location-client@1.x/dist/amazonLocationClient.js"></script> <script src="https://unpkg.com/@aws/amazon-location-utilities-auth-helper@1.x/dist/amazonLocationAuthHelper.js"></script> <!-- JavaScript for the app --> <script src="main.js"></script> </body> </html>
- main.css
-
快速入门应用程序的样式表。
* { box-sizing: border-box; font-family: Arial, Helvetica, sans-serif; } body { margin: 0; } header { background: #000000; padding: 0.5rem; } h1 { margin: 0; text-align: center; font-size: 1.5rem; color: #ffffff; } main { display: flex; min-height: calc(100vh - 94px); } #map { flex: 1; } aside { overflow-y: auto; flex: 0 0 30%; max-height: calc(100vh - 94px); box-shadow: 0 1px 1px 0 #001c244d, 1px 1px 1px 0 #001c2426, -1px 1px 1px 0 #001c2426; background: #f9f9f9; padding: 1rem; } h2 { margin: 0; } pre { white-space: pre-wrap; font-family: monospace; color: #16191f; } footer { background: #000000; padding: 1rem; color: #ffffff; }
- main.js
-
快速入门应用程序的代码。中的文字
red
应替换为相应的 Amazon Location 对象名称。// Amazon Location Service resource names: const mapName = "
explore.map
"; const placesName = "explore.place
"; const region = "your_region
"; const apiKey = "v1.public.a1b2c3d4...
// Initialize a map async function initializeMap() { // Initialize the map const mlglMap = new maplibregl.Map({ container: "map", // HTML element ID of map element center: [-77.03674, 38.891602], // Initial map centerpoint zoom: 16, // Initial map zoom style: `https://maps.geo.${region}.amazonaws.com/maps/v0/maps/${mapName}/style-descriptor?key=${apiKey}`, // Defines the appearance of the map and authenticates using an API key }); // Add navigation control to the top left of the map mlglMap.addControl(new maplibregl.NavigationControl(), "top-left"); return mlglMap; } async function main() { // Create an authentication helper instance using an API key const authHelper = await amazonLocationAuthHelper.withAPIKey(apiKey); // Initialize map and Amazon Location SDK client const map = await initializeMap(); const client = new amazonLocationClient.LocationClient({ region, ...authHelper.getLocationClientConfig(), // Provides configuration required to make requests to Amazon Location }); // Variable to hold marker that will be rendered on click let marker; // On mouse click, display marker and get results: map.on("click", async function (e) { // Remove any existing marker if (marker) { marker.remove(); } // Render a marker on clicked point marker = new maplibregl.Marker().setLngLat([e.lngLat.lng, e.lngLat.lat]).addTo(map); // Set up parameters for search call let params = { IndexName: placesName, Position: [e.lngLat.lng, e.lngLat.lat], Language: "en", MaxResults: "5", }; // Set up command to search for results around clicked point const searchCommand = new amazonLocationClient.SearchPlaceIndexForPositionCommand(params); try { // Make request to search for results around clicked point const data = await client.send(searchCommand); // Write JSON response data to HTML document.querySelector("#response").textContent = JSON.stringify(data, undefined, 2); // Display place label in an alert box alert(data.Results[0].Place.Label); } catch (error) { // Write JSON response error to HTML document.querySelector("#response").textContent = JSON.stringify(error, undefined, 2); // Display error in an alert box alert("There was an error searching."); } }); } main();
接下来做什么
您已经完成了快速入门教程,应该对如何使用 Amazon Location Service 来构建应用程序有所了解。要了解更多关于 Amazon Location 的信息,您可以查看以下资源:
-
获取有关如何使用 Amazon Location 功能的更多信息
-
查看使用 Amazon Location 的代码示例,了解如何扩展此示例并构建更复杂的应用程序
添加搜索
创建安卓应用程序