

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

# 根據地理ofences 評估裝置位置
<a name="evaluate-geofences"></a>

有兩種方式可以針對地理圍欄評估位置，以產生地理圍欄事件：
+ 您可以連結追蹤器和地理圍欄集合。如需詳細資訊，請參閱 一節：[將追蹤器連結至地理圍欄集合](associate-consumer.md)。
+ 您可以直接請求地理圍欄集合資源，以評估一或多個位置。

如果您也想要追蹤裝置位置歷史記錄或地圖上的顯示位置，請將追蹤器連結至地理圍欄集合。或者，您可能不想評估所有位置更新，也不想將位置資料存放在追蹤器資源中。如果發生上述任一情況，您可以直接請求地理圍欄集合，並根據其地理圍欄評估一或多個裝置位置。

根據地理圍欄評估裝置位置會產生事件。您可以對這些事件做出反應，並將其路由到其他 AWS 服務。如需接收地理圍欄事件時可採取之動作的詳細資訊，請參閱[使用 Amazon EventBridge 對 Amazon Location Service 事件做出反應](https://docs.aws.amazon.com/location/latest/developerguide/location-events.html)。

Amazon Location 事件包含產生它的裝置位置更新屬性，包括時間、位置、準確性和鍵值中繼資料，以及輸入或結束的地理圍欄的一些屬性。如需地理圍欄事件中包含之資料的詳細資訊，請參閱 [Amazon Location Service 的 Amazon EventBridge 事件範例](location-events.md#example-event)。

下列範例使用 AWS CLI或 Amazon Location APIs。

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

**使用 Amazon Location APIs 根據地理圍欄的位置評估裝置位置**

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

下列範例使用 API 請求來評估裝置 *ExampleDevice* 對相關聯地理圍欄集合 *ExampleGeofenceCollection* 的位置。將這些值取代為您自己的地理位置和裝置 IDs。

```
POST /geofencing/v0/collections/ExampleGeofenceCollection/positions HTTP/1.1
Content-type: application/json

{
   "DevicePositionUpdates": [ 
      { 
         "DeviceId": "ExampleDevice",
         "Position": [-123.123, 47.123],
         "SampleTime": "2021-11-30T21:47:25.149Z",
         "Accuracy": {
            "Horizontal": 10.30
         },
         "PositionProperties": {
            "field1": "value1",
            "field2": "value2"
         }
      }
   ]
}
```

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

**使用 AWS CLI 命令根據地理圍欄的位置評估裝置位置**

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

下列範例使用 AWS CLI 來評估 *ExampleDevice* 相對於相關聯地理圍欄集合 *ExampleGeofenceCollection* 的位置。將這些值取代為您自己的地理位置和裝置 IDs。

```
aws location \
    batch-evaluate-geofences \
        --collection-name ExampleGeofenceCollection \
        --device-position-updates '[{"DeviceId":"ExampleDevice","Position":[-123.123,47.123],"SampleTime":"2021-11-30T21:47:25.149Z","Accuracy":{"Horizontal":10.30},"PositionProperties":{"field1":"value1","field2":"value2"}}]'
```

------