

本文属于机器翻译版本。若本译文内容与英语原文存在差异，则一律以英文原文为准。

# 使用亚马逊对亚马逊定位服务事件做出反应 EventBridge
<a name="location-events"></a>

Amazon EventBridge 是一种无服务器事件总线，它使用来自诸如 Amazon Location 之类的 AWS 服务的数据有效地将应用程序连接在一起。 EventBridge 从 Amazon Location 接收事件，并将这些数据路由到目标，例如 AWS Lambda。您可以设置路由规则来确定发送数据的目的地，以便构建能够实时响应的应用程序架构。

默认情况下，只有地理围栏`EXIT`事件（`ENTER`以及设备进入或离开地理围栏区域时的事件）才会发送到。 EventBridge 您也可以为跟踪器资源启用所有已筛选的位置更新事件。有关更多信息，请参阅 [为跟踪器启用更新事件](#enable-update-events)。

有关更多信息，请参阅 *Amazon EventBridge 用户指南中的*[事件和事件模式](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-events.html)。

**Topics**
+ [为跟踪器启用更新事件](#enable-update-events)
+ [为 Amazon Location 创建活动规则](#create-event-rule)
+ [亚马逊定位服务（Amazon Location Service） EventBridge 的事件示例](#example-event)

## 为跟踪器启用更新事件
<a name="enable-update-events"></a>

默认情况下，Amazon Location 仅`ENTER`向发送`EXIT`地理围栏事件。 EventBridge您可以启用所有筛选过的位置`UPDATE`事件，以便将跟踪器发送到该跟踪器 EventBridge。您可以在[创建](https://docs.aws.amazon.com/location/latest/APIReference/API_WaypointTracking_CreateTracker.html)或[更新](https://docs.aws.amazon.com/location/latest/APIReference/API_WaypointTracking_UpdateTracker.html)跟踪器时执行此操作。

例如，要使用更新现有跟踪器 AWS CLI，您可以使用以下命令（使用您的跟踪器资源名称代替*MyTracker*）。

```
aws location update-tracker --tracker-name MyTracker --event-bridge-enabled
```

要关闭跟踪器的位置事件，您必须使用 API 或 Amazon Location Service 控制台。

## 为 Amazon Location 创建活动规则
<a name="create-event-rule"></a>

您可以为[每个事件总线创建多达 300 条规则](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-quota.html)， EventBridge 以配置为响应 Amazon Location 事件而采取的操作。

例如，您可以为地理围栏事件创建规则，在该规则中，当在地理围栏边界内检测到电话时，将发送推送通知。

**为 Amazon Location 事件创建规则**

使用以下值[创建基于 Amazon 位置事件的 EventBridge 规则](https://docs.aws.amazon.com/eventbridge/latest/userguide/eb-create-rule-visual.html)：
+ 对于**规则类型**，选择**具有事件模式的规则**。
+ 在**事件模式**框中，添加以下模式。

  ```
  {
    "source": ["aws.geo"],
    "detail-type": ["Location Geofence Event"]
  }
  ```

  要创建跟踪器位置更新规则，您可以改用以下模式：

  ```
  {
    "source": ["aws.geo"],
    "detail-type": ["Location Device Position Event"]
  }
  ```

  您可以选择通过添加 `detail` 标签来仅指定 `ENTER` 或 `EXIT` 事件（如果您的规则是针对跟踪器位置更新，则只有一个 `EventType`，因此无需对其进行筛选）：

  ```
  {
    "source": ["aws.geo"],
    "detail-type": ["Location Geofence Event"],
    "detail": {
      "EventType": ["ENTER"]
    }
  }
  ```

  您也可以选择根据位置的属性或地理围栏进行筛选：

  ```
  {
    "source": ["aws.geo"],
    "detail-type": ["Location Geofence Event"],
    "detail": {
      "EventType": ["ENTER"],
      "GeofenceProperties": {
        "Type": "LoadingDock"
      },
      "PositionProperties": {
        "VehicleType": "Truck"
      }
    }
  }
  ```
+ 对于**选择目标**，选择目标从 Amazon Location Service 收到事件时要执行的目标操作。

  例如，使用 Amazon Simple Notification Service (SNS) 主题在事件发生时发送电子邮件或短信。您首先需要使用 Amazon SNS 控制台创建 Amazon SNS 主题。想要了解更多信息，请参阅[使用 Amazon SNS 发送通知](https://docs.aws.amazon.com/sns/latest/dg/sns-user-notifications.html)。

**警告**  
最佳做法是确认事件规则已成功应用，否则您的自动操作可能无法按预期启动。要验证您的事件规则，请为事件规则启动条件。例如，模拟进入地理围栏区域的设备。

您也可以从 Amazon Location 捕获所有事件，只需排除 `detail-type` 部分即可。例如：

```
{
  "source": [
    "aws.geo"
  ]
}
```

**注意**  
同一事件可以多次交付。您可以使用事件 ID 对收到的事件进行重复数据删除。

## 亚马逊定位服务（Amazon Location Service） EventBridge 的事件示例
<a name="example-event"></a>

以下是通过调用 `BatchUpdateDevicePosition` 启动的进入地理围栏的事件示例。

```
{
  "version": "0",
  "id": "aa11aa22-33a-4a4a-aaa5-example",
  "detail-type": "Location Geofence Event",
  "source": "aws.geo",
  "account": "636103698109",
  "time": "2020-11-10T23:43:37Z",
  "region": "eu-west-1",
  "resources": [
    "arn:aws:geo:eu-west-1:0123456789101:geofence-collection/GeofenceEvents-GeofenceCollection_EXAMPLE",
    "arn:aws:geo:eu-west-1:0123456789101:tracker/Tracker_EXAMPLE"
  ],
  "detail": {
    "EventType": "ENTER",
    "GeofenceId": "polygon_14",
    "DeviceId": "Device1-EXAMPLE",
    "SampleTime": "2020-11-10T23:43:37.531Z",
    "Position": [
      -123.12390073297821,
      49.23433613216247
    ],
    "Accuracy": {
      "Horizontal": 15.3
    },
    "GeofenceProperties": {
      "ExampleKey1": "ExampleField1",
      "ExampleKey2": "ExampleField2"
    },
    "PositionProperties": {
      "ExampleKey1": "ExampleField1",
      "ExampleKey2": "ExampleField2"
    }
  }
}
```

以下是通过调用 `BatchUpdateDevicePosition` 启动的退出地理围栏的事件示例。

```
{
  "version": "0",
  "id": "aa11aa22-33a-4a4a-aaa5-example",
  "detail-type": "Location Geofence Event",
  "source": "aws.geo",
  "account": "123456789012",
  "time": "2020-11-10T23:41:44Z",
  "region": "eu-west-1",
  "resources": [
    "arn:aws:geo:eu-west-1:0123456789101:geofence-collection/GeofenceEvents-GeofenceCollection_EXAMPLE",
    "arn:aws:geo:eu-west-1:0123456789101:tracker/Tracker_EXAMPLE"
  ],
  "detail": {
    "EventType": "EXIT",
    "GeofenceId": "polygon_10",
    "DeviceId": "Device1-EXAMPLE",
    "SampleTime": "2020-11-10T23:41:43.826Z",
    "Position": [
      -123.08569321875426,
      49.23766166742559
    ],
    "Accuracy": {
      "Horizontal": 15.3
    },
    "GeofenceProperties": {
      "ExampleKey1": "ExampleField1",
      "ExampleKey2": "ExampleField2"
    },
    "PositionProperties": {
      "ExampleKey1": "ExampleField1",
      "ExampleKey2": "ExampleField2"
    }
  }
}
```

以下是通过调用 `BatchUpdateDevicePosition` 启动的位置更新事件的示例。

```
{
  "version": "0",
  "id": "aa11aa22-33a-4a4a-aaa5-example",
  "detail-type": "Location Device Position Event",
  "source": "aws.geo",
  "account": "123456789012",
  "time": "2020-11-10T23:41:44Z",
  "region": "eu-west-1",
  "resources": [
    "arn:aws:geo:eu-west-1:0123456789101:tracker/Tracker_EXAMPLE"
  ],
  "detail": {
    "EventType": "UPDATE",
    "TrackerName": "tracker_2",
    "DeviceId": "Device1-EXAMPLE",
    "SampleTime": "2020-11-10T23:41:43.826Z",
    "ReceivedTime": "2020-11-10T23:41:39.235Z",
    "Position": [
      -123.08569321875426,
      49.23766166742559
    ],
    "Accuracy": {
      "Horizontal": 15.3
    },
    "PositionProperties": {
      "ExampleKey1": "ExampleField1",
      "ExampleKey2": "ExampleField2"
    }
  }
}
```