本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
使用 Amazon Location 新增地理圍欄
Geofences 包含形成封閉界限的點和頂點,該邊界定義了感興趣的區域。Geofence 集合會存放和管理一或多個地理圍欄。
Amazon Location 地理圍欄集合會使用稱為 GeoJSON (RFC 7946) 的標準地理空間資料格式來存放由 定義的地理圍欄。您可以使用 geojson.io 等工具,以圖形方式繪製地理圍欄並儲存輸出 GeoJSON 檔案。
建立地理柵欄集合。
使用 Amazon Location 主控台、 AWS CLI或 Amazon Location APIs 建立地理圍欄集合來存放和管理地理圍欄。
- Console
-
使用 Amazon Location 主控台建立地理圍欄集合
-
開啟位於 https://https://console.aws.amazon.com/location/ 的 Amazon Location Service 主控台。
-
在左側導覽窗格中,選擇 Geofence 集合。
-
選擇建立地理圍欄集合。
-
填寫下列方塊:
-
在以 CloudWatch 做為目標的 EventBridge 規則下,您可以建立選用的 EventBridge 規則,以開始對地理圍欄事件做出反應。這讓 Amazon Location 能夠將事件發佈到 Amazon CloudWatch Logs 中。
-
(選用) 在 Tags (標籤) 底下,輸入標籤 Key (金鑰) 與 Value (值)。這會將標籤新增至新的地理圍欄集合。如需詳細資訊,請參閱使用標籤管理資源。
-
(選用) 在客戶受管金鑰加密下,您可以選擇新增客戶受管金鑰。這會新增您透過預設 AWS 擁有的加密建立、擁有和管理的對稱客戶受管金鑰。如需詳細資訊,請參閱加密靜態資料。
-
選擇建立地理圍欄集合。
- API
-
使用 Amazon Location APIs 建立地理圍欄集合
從 Amazon Location Geofences APIs使用 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。
若要建立圓形地理圍欄,請使用 位置的緯度定義中心點,並設定以公尺為單位的半徑。如需詳細資訊,請參閱新增循環地理圍欄。
您也可以使用 Amazon Location Service APIs,以鍵/值對的形式將中繼資料新增至您的地理。這些可用於儲存有關地理圍欄的資訊,例如其類型,或專屬於您應用程式的其他資訊。您可以在 時使用此中繼資料使用 Amazon EventBridge 對 Amazon Location Service 事件做出反應。
新增多邊形地理圍欄
多邊形地理圍欄使用 GeoJSON 座標陣列來定義區域。當您需要追蹤進出不規則形狀地理界限的項目時,請使用此格式。
使用 GeoJSON 工具繪製地理圍欄
建立 GeoJSON 檔案
-
開啟 GeoJSON 編輯工具,例如 geojson.io。
-
選擇繪製多邊形圖示並繪製您的感興趣區域。
-
選擇儲存,然後從下拉式選單中選擇 GeoJSON。
將 GeoJSON 地理圍欄放在地理圍欄集合中
您可以使用產生的 GeoJSON 檔案,使用 Amazon Location Service 主控台 AWS CLI、 或 Amazon Location APIs 上傳您的地理圍欄。
- Console
-
使用 Amazon Location Service 主控台將地理圍欄新增至地理圍欄集合
- API
-
使用 Amazon Location APIs 新增地理圍欄
從 Amazon Location Geofences APIs 使用 PutGeofence操作。
下列範例使用 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操作新增多個地理圍欄。
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]]]'
新增循環地理圍欄
若要建立圓形地理圍欄,您必須知道中心點的緯度和經度,以及以公尺為單位的半徑。當您需要根據接近特定點觸發事件時,請使用循環地理圍欄,例如商店、倉儲或指標。您可以使用 Amazon Location 主控台、Amazon Location APIs 或 建立循環地理圍欄 AWS CLI。
- Console
-
使用 Amazon Location Service 主控台將循環地理圍欄新增至地理圍欄集合
- API
-
使用 Amazon Location APIs 新增循環地理圍欄
從 Amazon Location Geofences APIs 使用 PutGeofence操作。
下列範例使用 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
-
使用 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
其中 circle.json包含圓形幾何。
{
"Circle": {
"Center": [-74.006975, 40.717127],
"Radius": 287.7897969218057
}
}