本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
添加地理圍欄
地理圍欄包含形成封閉邊界的點和頂點,它定義了感興趣的區域。地理圍欄集合存儲和管理一個或多個地理圍欄。
Amazon 位置地理圍欄集合存儲使用稱為 Ge oJSON(RFC 7946)的標準地理空間數據格式定義的地理圍欄。您可以免費使用諸如 geojson.io 之類的工具以圖形方式繪製地理圍欄並保存輸出的 GeoJSON 文件。
Amazon Location 不支援跨越反經絡的孔、多重多邊形、順時針多邊形和地理圍欄的多邊形。
建立地理圍欄集合
使用 Amazon 位置主控台、或 Amazon 位置 API 建立地理圍欄集合以存放和管理地理圍欄。 AWS CLI
- Console
-
若要使用 Amazon 位置主控台建立地理圍欄集合
-
在 https://console.aws.amazon.com/location/ 打開 Amazon 定 Location Service 控制台。
-
在左側導覽窗格中,選擇「地理圍欄集合」。
-
選擇 [建立地理圍欄集合]。
-
填寫下列方塊:
-
在以 CloudWatch 作為目標的EventBridge 規則下,您可以創建一個可選 EventBridge 規則以開始對地理圍欄事件做出反應。這使 Amazon 位置能夠將事件發佈到 Amazon CloudWatch 日誌。
-
(選用) 在 Tags (標籤) 底下,輸入標籤 Key (金鑰) 與 Value (值)。這將為您的新地理圍欄集合添加一個標籤。如需詳細資訊,請參閱 標記您的 Amazon Location Service 資源。
-
(選擇性) 在客戶受管金鑰加密底下,您可以選擇新增客戶管理的金鑰。這會新增您透過預設 AWS 擁有的加密建立、擁有和管理的對稱客戶管理金鑰。如需詳細資訊,請參閱加密靜態資料。
-
選擇 [建立地理圍欄集合]。
- API
-
若要使用 Amazon 位置 API 建立地理圍欄集合
使用來自 Amazon 位置地理圍欄 API 的CreateGeofenceCollection
操作。
下列範例會使用 API 要求來建立名為的地理圍欄集合。ExampleGeofenceCollection
地理圍欄集合與客戶管理的 AWS KMS 金鑰相關聯,以加密客戶資料。
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
-
使用指令建立地理圍欄集合的步驟 AWS CLI
使用 create-geofence-collection
命令。
下列範例會使用建立名 AWS CLI 為的地理圍欄集合。ExampleGeofenceCollection
地理圍欄集合與客戶管理的 AWS KMS 金鑰相關聯,以加密客戶資料。
aws location \
create-geofence-collection \
--collection-name "ExampleGeofenceCollection
" \
--description "Shopping center geofence collection" \
--kms-key-id "1234abcd-12ab-34cd-56ef-1234567890ab
" \
--tags Tag1=Value1
繪製地理圍欄
現在您已經建立了地理圍欄集合,您可以定義地理圍欄。地理圍欄被定義為多邊形或圓。要繪製多邊形地理圍欄,您可以使用 GeoJSON 編輯工具,例如 geojson.io。
若要將地理圍欄建立為圓,您必須定義圓的中心點和半徑。例如,如果您想要建立地理圍欄,以便在設備距離特定位置 50 公尺以內時收到通知,則可以使用該位置的緯度和經度,並將半徑指定為 50 公尺。
使用 Amazon 定 Location Service API,您還可以使用鍵值對的形式將中繼資料新增至地理圍欄。這些對於存儲地理圍欄的信息(例如其類型或其他應用程序特定的信息)非常有用。您可以在何時使用此中繼資料使用 Amazon 對 Amazon 定 Location Service 事件做出反應 EventBridge。
添加多邊形地理圍欄
本節介紹創建多邊形地理圍欄
使用 GeoJSON 工具繪製地理圍欄
現在您已經建立了地理圍欄集合,您可以使用 GeoJSON 編輯工具 (例如 geojson.io) 來定義地理圍欄。
若要建立 GeoJSON 檔案
-
打開一個 GeoJSON 編輯工具。例如,巨星 .io。
-
選擇繪製多邊形圖標並繪製您感興趣的區域。
-
選擇儲存,然後從下拉式選單中選擇 GeoJSON。
將 GeoJSON 地理圍欄放在地理圍欄集合中
您可以使用產生的 GeoJSON 檔案,使用 Amazon 定 Location Service 主控台、或 Amazon 定位 API 來上傳 AWS CLI地理圍欄:
- Console
-
使用 Amazon 定位服務主控台將地理圍欄新增至 Location Service 圍欄集合
- API
-
使用 Amazon 位置 API 添加地理圍欄
使用來自 Amazon 位置地理圍欄 API 的PutGeofence
操作。
下列範例會使用 API 要求,將識別碼 GEOFENCE-EXAMPLE1
的地理圍欄新增至名為的地理圍欄集合。ExampleGeofenceCollection
它還使用鍵Type
和值指定單個地理圍欄元數據屬性。loadingArea
PUT /geofencing/v0/collections/ExampleGeofenceCollection
/geofence/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
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
-
使用指令將地理圍欄加入至地理圍欄集合的步驟 AWS CLI
使用 put-geofence
命令。
下列範例會使用將地理圍欄新增 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]]]'
{
"CreateTime": "2020-11-11T00:16:14.487000+00:00",
"GeofenceId": "ExampleGeofenceTriangle
",
"UpdateTime": "2020-11-11T00:19:59.894000+00:00"
}
添加循環地理圍欄
本節介紹創建循環地理圍欄。您必須知道要做為圓心點的緯度和經度,以及圓的半徑(以米為單位)。您可以使用 Amazon 位置 API 或. AWS CLI
- API
-
使用 Amazon 位置 API 新增循環地理圍欄
使用來自 Amazon 位置地理圍欄 API 的PutGeofence
操作。
下列範例會使用 API 要求,將識別碼 GEOFENCE-EXAMPLE2
的地理圍欄新增至名為的地理圍欄集合:ExampleGeofenceCollection
PUT /geofencing/v0/collections/ExampleGeofenceCollection
/geofence/GEOFENCE-EXAMPLE2
Content-type: application/json
{
"Geometry": {
"Circle": {
"Center": [-5.716667, -15.933333],
"Radius": 50
}
}
}
- AWS CLI
-
使用指令將圓形地理圍欄加入至地理圍欄集合的步驟 AWS CLI
使用 put-geofence
命令。
下列範例會使用將地理圍欄新增 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
在此範例中,圓圈 .json 檔案包含圓形幾何圖形的 JSON。
{
"Circle": {
"Center": [-74.006975, 40.717127],
"Radius": 287.7897969218057
}
}