

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 如何在地圖上新增控制項
<a name="how-to-add-control-on-map"></a>

Amazon Location Service 可讓您將多個控制項新增至地圖，包括導覽、地理位置、全螢幕、擴展和歸因控制項。
+ **導覽控制**：包含縮放按鈕和羅盤。
+ **地理位置控制**：提供使用瀏覽器地理位置 API 在地圖上尋找使用者的按鈕。
+ **全螢幕控制**：包含用於切換地圖進入和退出全螢幕模式的按鈕。
+ **縮放控制**：顯示地圖上距離與地面上對應距離的比率。
+ **屬性控制**：顯示地圖的屬性資訊。根據預設，屬性控制項會展開 （無論地圖寬度為何）。

您可以將控制項新增至地圖的任何角落：左上角、左下角、右下角或右上角。

## 新增地圖控制項
<a name="add-map-controls"></a>

在下列範例中，您將新增上面列出的地圖控制項。

### 映射控制程式碼範例
<a name="web-code-example"></a>

------
#### [ index.html ]

```
<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Map Controls</title>
        <meta property="og:description" content="Initialize a map in an HTML element with MapLibre GL JS." />
        <meta charset='utf-8'>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel='stylesheet' href='https://unpkg.com/maplibre-gl@5.x/dist/maplibre-gl.css' />
        <link rel='stylesheet' href='style.css' />
        <script src='https://unpkg.com/maplibre-gl@5.x/dist/maplibre-gl.js'></script>
    </head>
    <body>
        <!-- Map container -->
        <div id="map"></div>
        <script>
            const apiKey = "<API_KEY>";
            const mapStyle = "Standard"; // e.g., Standard, Monochrome, Hybrid, Satellite
            const awsRegion = "eu-central-1"; // e.g., us-east-2, us-east-1, us-west-2, etc.
            const styleUrl = `https://maps.geo.${awsRegion}.amazonaws.com/v2/styles/${mapStyle}/descriptor?key=${apiKey}`;

            const map = new maplibregl.Map({
                container: 'map', // container id
                style: styleUrl, // style URL
                center: [-123.13203602550998, 49.28726257639254], // starting position [lng, lat]
                zoom: 10, // starting zoom
                attributionControl: false, // hide default attributionControl in bottom left
            });

            // Adding controls to the map
            map.addControl(new maplibregl.NavigationControl()); // Zoom and rotation controls
            map.addControl(new maplibregl.FullscreenControl()); // Fullscreen control
            map.addControl(new maplibregl.GeolocateControl()); // Geolocation control
            map.addControl(new maplibregl.AttributionControl(), 'bottom-left'); // Attribution in bottom-left
            map.addControl(new maplibregl.ScaleControl(), 'bottom-right'); // Scale control in bottom-right
        </script>
    </body>
</html>
```

------
#### [ style.css ]

```
body { margin: 0; padding: 0; }
html, body, #map { height: 100%; }
```

------

## 開發人員秘訣
<a name="developer-tips"></a>

### 導覽控制選項
<a name="navigationcontrol-options"></a>

```
new maplibregl.NavigationControl({ 
    showCompass: true, // show or hide compass (default: true)
    showZoom: true // show or hide zoom controls (default: true)
});
```

### 地理位置控制選項
<a name="geolocatecontrol-options"></a>

```
new maplibregl.GeolocateControl({ 
    positionOptions: { enableHighAccuracy: true }, // default: false
    trackUserLocation: true // default: false
});
```

### 屬性控制選項
<a name="attributioncontrol-options"></a>

```
new maplibregl.AttributionControl({
    compact: true, // compact (collapsed) mode (default: false)
});
```

### 擴展控制選項
<a name="scalecontrol-options"></a>

```
new maplibregl.ScaleControl({ 
    maxWidth: 100, // width of the scale (default: 50)
    unit: 'imperial' // imperial or metric (default: metric)
});
```

### 全螢幕控制選項
<a name="fullscreencontrol-options"></a>

```
map.addControl(new maplibregl.FullscreenControl({
    container: document.querySelector('body') // container for fullscreen mode
}));
```