

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

# 使用 Amazon Location 新增地理圍欄
<a name="add-geofences"></a>

Geofences 包含形成封閉界限的點和頂點，該邊界定義了感興趣的區域。Geofence 集合會存放和管理一或多個地理圍欄。

Amazon Location 地理圍欄集合會使用稱為 [GeoJSON (RFC 7946)](https://geojson.org/) 的標準地理空間資料格式來存放由 定義的地理圍欄。您可以使用 [geojson.io](http://geojson.io/) 等工具，以圖形方式繪製地理圍欄並儲存輸出 GeoJSON 檔案。

**注意**  
Amazon Location 不支援順時針多邊形 （依照 [RFC 7946 §3.1.6 的要求，使用逆時針繞組順序](https://www.rfc-editor.org/rfc/rfc7946#section-3.1.6)) 以及跨越經絡 (180° 經度線） 的地理圍欄。

**Topics**
+ [建立地理柵欄集合。](#create-geofence-collection)
+ [繪製地理圍欄](#draw-geofences)
+ [新增多邊形地理圍欄](#draw-polygon-geofences)
+ [新增循環地理圍欄](#draw-circle-geofences)

## 建立地理柵欄集合。
<a name="create-geofence-collection"></a>

使用 Amazon Location 主控台、 AWS CLI或 Amazon Location APIs 建立地理圍欄集合來存放和管理地理圍欄。

------
#### [ Console ]

1. 開啟位於 https：//[https://console.aws.amazon.com/location/](https://console.aws.amazon.com/location/home) 的 Amazon Location Service 主控台。

1. 在左側導覽窗格中，選擇 **Geofence 集合**。

1. 選擇**建立地理圍欄集合**。

1. 填寫下列方塊：
   + ****名稱 **** – 輸入唯一的名稱。例如 {{ExampleGeofenceCollection}}。最多 100 個字元。有效項目包括英數字元、連字號、句點和底線。
   + ****描述**** – 輸入選用的描述來區分您的資源。

1. 在以 ** CloudWatch 做為目標的 EventBridge 規則**下，您可以建立選用的 EventBridge 規則，以開始[對地理圍欄事件做出反應](location-events.md)。這讓 Amazon Location 能夠將事件發佈到 Amazon CloudWatch Logs 中。

1. (選用) 在 **Tags (標籤)** 底下，輸入標籤 **Key (金鑰)** 與 **Value (值)**。這會將標籤新增至新的地理圍欄集合。如需詳細資訊，請參閱[使用標籤管理資源](manage-resources.md)。

1. （選用） 在**客戶受管金鑰加密**下，您可以選擇**新增客戶受管金鑰**。這會新增您透過預設 AWS 擁有的加密建立、擁有和管理的對稱客戶受管金鑰。如需詳細資訊，請參閱[加密靜態資料](encryption-at-rest.md)。

1. 選擇**建立地理圍欄集合**。

------
#### [ API ]

從 Amazon Location Geofences APIs使用 `[CreateGeofenceCollection](https://docs.aws.amazon.com/location/latest/APIReference/API_WaypointGeofencing_CreateGeofenceCollection.html)`操作。

下列範例使用 API 請求來建立名為 {{ExampleGeofenceCollection}} 的地理圍欄集合。地理圍欄集合與客戶[受管 AWS KMS 金鑰相關聯，以加密客戶資料](encryption-at-rest.md)。

```
POST /geofencing/v0/collections
Content-type: application/json

{
   "CollectionName": "{{ExampleGeofenceCollection}}",
   "Description": "Geofence collection 1 for shopping center",
   "KmsKeyId": "{{1234abcd-12ab-34cd-56ef-1234567890ab}}",
   "Tags": {
      "Tag1" : "Value1"
   }
}
```

------
#### [ AWS CLI ]

使用 `[create-geofence-collection](https://docs.aws.amazon.com/cli/latest/reference/location/create-geofence-collection.html)` 命令。

下列範例使用 AWS CLI 建立名為 {{ExampleGeofenceCollection}} 的地理圍欄集合。地理圍欄集合與客戶[受管 AWS KMS 金鑰相關聯，以加密客戶資料](encryption-at-rest.md)。

```
aws location \
  create-geofence-collection \
  --collection-name "{{ExampleGeofenceCollection}}" \
  --description "Shopping center geofence collection" \
  --kms-key-id "{{1234abcd-12ab-34cd-56ef-1234567890ab}}" \
  --tags Tag1=Value1
```

------

**注意**  
帳單取決於您的用量。使用其他 AWS 服務可能會產生費用。如需詳細資訊，請參閱 [Amazon Location Service 定價](https://aws.amazon.com/location/pricing/)。

## 繪製地理圍欄
<a name="draw-geofences"></a>

現在您已建立地理圍欄集合，您可以定義地理圍欄。地理圍欄定義為多邊形或圓形。若要繪製多邊形地理圍欄，您可以使用 GeoJSON 編輯工具，例如 [geojson.io](http://geojson.io/)。

若要建立圓形地理圍欄，請使用 位置的緯度定義中心點，並設定以公尺為單位的半徑。如需詳細資訊，請參閱[新增循環地理圍欄](#draw-circle-geofences)。

您也可以使用 Amazon Location Service APIs，以鍵/值對的形式將中繼資料新增至您的地理。這些可用於儲存有關地理圍欄的資訊，例如其類型，或專屬於您應用程式的其他資訊。您可以在 時使用此中繼資料[使用 Amazon EventBridge 對 Amazon Location Service 事件做出反應](location-events.md)。

## 新增多邊形地理圍欄
<a name="draw-polygon-geofences"></a>

多邊形地理圍欄使用 GeoJSON 座標陣列來定義區域。當您需要追蹤進出不規則形狀地理界限的項目時，請使用此格式。

### 使用 GeoJSON 工具繪製地理圍欄
<a name="draw-geofences-geojson"></a>

1. 開啟 GeoJSON 編輯工具，例如 [geojson.io](http://geojson.io/)。

1. 選擇**繪製多邊形**圖示並繪製您的感興趣區域。

1. 選擇**儲存**，然後從下拉式選單中選擇 **GeoJSON**。

### 將 GeoJSON 地理圍欄放在地理圍欄集合中
<a name="put-geofences"></a>

您可以使用產生的 GeoJSON 檔案，使用 Amazon Location Service 主控台 AWS CLI、 或 Amazon Location APIs 上傳您的地理圍欄。

------
#### [ Console ]

1. 開啟位於 https：//[https://console.aws.amazon.com/location/](https://console.aws.amazon.com/location/home) 的 Amazon Location Service 主控台。

1. 在左側導覽窗格中，選擇 **Geofence 集合**。

1. 從**地理圍欄集合**清單中，選取目標地理圍欄集合的名稱連結。

1. 在**地理圍欄**下，選擇**建立地理圍欄**。

1. 在**新增地理圍**欄視窗中，將 GeoJSON 拖放到視窗中。

1. 選擇**新增地理圍欄**。

------
#### [ API ]

從 Amazon Location Geofences APIs 使用 `[PutGeofence](https://docs.aws.amazon.com/location/latest/APIReference/API_WaypointGeofencing_PutGeofence.html)`操作。

下列範例使用 API 請求，將 ID 為 {{GEOFENCE-EXAMPLE1}} 的地理圍欄新增至名為 {{ExampleGeofenceCollection}} 的地理圍欄集合。它也會指定具有索引鍵 `Type`和值 的地理圍欄中繼資料屬性`loadingArea`。

```
PUT /geofencing/v0/collections/{{ExampleGeofenceCollection}}/geofences/{{GEOFENCE-EXAMPLE1}}
Content-type: application/json

{
   "GeofenceProperties": {
      "Type" : "loadingArea"
   },
   "Geometry": {
      "Polygon": [
        [
            [-5.716667, -15.933333],
            [-14.416667, -7.933333],
            [-12.316667, -37.066667],
            [-5.716667, -15.933333]
        ]
      ]
   }
}
```

或者，您可以使用 `[BatchPutGeofence](https://docs.aws.amazon.com/location/latest/APIReference/API_WaypointGeofencing_BatchPutGeofence.html)`操作新增多個地理圍欄。

```
POST /geofencing/v0/collections/{{ExampleGeofenceCollection}}/put-geofences
Content-type: application/json

{
   "Entries": [
      {
         "GeofenceProperties": {
            "Type" : "loadingArea"
         },
         "GeofenceId": "{{GEOFENCE-EXAMPLE1}}",
         "Geometry": {
            "Polygon": [
               [
                  [-5.716667, -15.933333],
                  [-14.416667, -7.933333],
                  [-12.316667, -37.066667],
                  [-5.716667, -15.933333]
               ]
            ]
         }
      }
   ]
}
```

------
#### [ AWS CLI ]

使用 `[put-geofence](https://docs.aws.amazon.com/cli/latest/reference/location/put-geofence.html)` 命令。

下列範例使用 AWS CLI 將地理圍欄新增至名為 {{ExampleGeofenceCollection}} 的地理圍欄集合。

```
aws location \
    put-geofence \
        --collection-name {{ExampleGeofenceCollection}} \
        --geofence-id {{ExampleGeofenceTriangle}} \
        --geofence-properties '{"Type": "loadingArea"}' \
        --geometry 'Polygon=[[[-5.716667, -15.933333],[-14.416667, -7.933333],[-12.316667, -37.066667],[-5.716667, -15.933333]]]'
```

------

## 新增循環地理圍欄
<a name="draw-circle-geofences"></a>

若要建立圓形地理圍欄，您必須知道中心點的緯度和經度，以及以公尺為單位的半徑。當您需要根據接近特定點觸發事件時，請使用循環地理圍欄，例如商店、倉儲或指標。您可以使用 Amazon Location 主控台、Amazon Location APIs 或 建立循環地理圍欄 AWS CLI。

------
#### [ Console ]

1. 開啟位於 https：//[https://console.aws.amazon.com/location/](https://console.aws.amazon.com/location/home) 的 Amazon Location Service 主控台。

1. 在左側導覽窗格中，選擇 **Geofence 集合**。

1. 從**地理圍欄集合**清單中，選取目標地理圍欄集合的名稱連結。

1. 在**地理圍欄**下，選擇**建立地理圍欄**。

1. 在**新增地理圍欄**視窗中，選擇**圓形**，然後輸入以公尺為單位的****緯度、**經度**和**半徑**中心。

1. 選擇**新增地理圍欄**。

------
#### [ API ]

從 Amazon Location Geofences APIs 使用 `[PutGeofence](https://docs.aws.amazon.com/location/latest/APIReference/API_WaypointGeofencing_PutGeofence.html)`操作。

下列範例使用 API 請求，將 ID 為 {{GEOFENCE-EXAMPLE2}} 的地理圍欄新增至名為 {{ExampleGeofenceCollection}} 的地理圍欄集合。

```
PUT /geofencing/v0/collections/{{ExampleGeofenceCollection}}/geofences/{{GEOFENCE-EXAMPLE2}}
Content-type: application/json

{
   "Geometry": {
      "Circle": {
        "Center": [-5.716667, -15.933333],
        "Radius": 50
      }
   }
}
```

------
#### [ AWS CLI ]

使用 `[put-geofence](https://docs.aws.amazon.com/cli/latest/reference/location/put-geofence.html)` 命令。

下列範例使用 AWS CLI 將循環地理圍欄新增至名為 {{ExampleGeofenceCollection}} 的地理圍欄集合。

```
aws location \
    put-geofence \
        --collection-name {{ExampleGeofenceCollection}} \
        --geofence-id {{ExampleGeofenceCircle}} \
        --geometry 'Circle={Center=[-5.716667, -15.933333], Radius=50}'
```

**注意**  
您也可以將複雜幾何的 JSON 放入自己的檔案中，如下列範例所示。  

```
aws location \
    put-geofence \
        --collection-name {{ExampleGeofenceCollection}} \
        --geofence-id {{ExampleGeofenceCircle}} \
        --geometry file://circle.json
```
其中 `circle.json`包含圓形幾何。  

```
{
    "Circle": {
        "Center": [-74.006975, 40.717127],
        "Radius": 287.7897969218057
    }
}
```

------