Spatial - Get Geofence

Applies to: see pricing tiers.

The Geofence Get API allows you to retrieve the proximity of a coordinate to a geofence that has been uploaded to the Data service. You can use the Data Upload API to upload a geofence or set of fences. See Geofencing GeoJSON data for more details on the geofence data format. To query the proximity of a coordinate, you supply the location of the object you are tracking as well as the ID for the fence or set of fences, and the response will contain information about the distance from the outer edge of the geofence. A negative value signifies that the coordinate is inside of the fence while a positive value means that it is outside of the fence.

This API can be used for a variety of scenarios that include things like asset tracking, fleet management, or setting up alerts for moving objects.

The API supports integration with Event Grid. The isAsync parameter is used to enable integration with Event Grid (disabled by default). To test this API, you can upload the sample data from Post Geofence API examples(Request Body) via Data Upload API and replace the [udid] from the sample request below with the udid returned by Data Upload API.

Geofencing InnerError code

In geofencing response error contract, innererror is an object containing service specific information about the error. code is a property in innererror which can map to a specific geofencing error type. The table belows shows the code mapping between all the known client error type to the corresponding geofencing error message.

innererror.code error.message
NullDeviceId Device Id should not be null.
NullUdid Udid should not be null.
UdidWrongFormat Udid should be acquired from user data ingestion API.
InvalidUserTime Usertime is invalid.
InvalidSearchBuffer Searchbuffer is invalid.
InvalidSearchRange The value range of searchbuffer should be from 0 to 500 meters.
InvalidLatLon Lat and/or lon parameters are invalid.
InvalidIsAsyncValue The IsAsync parameter is invalid.
InvalidModeValue The mode parameter invalid.
InvalidJson Geofencing data is not a valid json file.
NotSupportedGeoJson Geofencing data can't be read as a Feature or FeatureCollections.
InvalidGeoJson Geofencing data is invalid.
NoUserDataWithAccountOrSubscription Can't find user geofencing data with provided account-id and/or subscription-id.
NoUserDataWithUdid Can't find user geofencing data with provided udid.
GET https://{geography}.atlas.microsoft.com/spatial/geofence/json?api-version=2022-08-01&deviceId={deviceId}&udid={udid}&lat={lat}&lon={lon}
GET https://{geography}.atlas.microsoft.com/spatial/geofence/json?api-version=2022-08-01&deviceId={deviceId}&udid={udid}&lat={lat}&lon={lon}&z={z}&userTime={userTime}&searchBuffer={searchBuffer}&isAsync={isAsync}&mode={mode}

URI Parameters

Name In Required Type Description
format
path True

Desired format of the response. Only json format is supported.

geography
path True
  • string

Location of the Azure Maps Account. Valid values are us (East US, West Central US, West US 2) and eu (North Europe, West Europe). This parameter is required when a udid is supplied in the request. For example, if the Azure Maps Account is located in East US, only requests to us geography will be accepted.

api-version
query True
  • string

Version number of Azure Maps API.

deviceId
query True
  • string

ID of the device

lat
query True
  • number

The latitude of the location being passed. Example: 48.36.

lon
query True
  • number

The longitude of the location being passed. Example: -124.63.

udid
query True
  • string

The unique id returned from Data Upload API after uploading a valid GeoJSON FeatureCollection object. Please refer to RFC 7946 for details. All the feature's properties should contain geometryId, which is used for identifying the geometry and is case-sensitive.

isAsync
query
  • boolean

If true, the request will use async event mechanism; if false, the request will be synchronized and do not trigger any event. The default value is false.

mode
query

Mode of the geofencing async event mechanism.

searchBuffer
query
  • number

The radius of the buffer around the geofence in meters that defines how far to search inside and outside the border of the fence against the coordinate that was provided when calculating the result. The minimum value is 0, and the maximum is 500. The default value is 50.

userTime
query
  • string
date-time

The user request time. If not presented in the request, the default value is DateTime.Now.

z
query
  • number

The sea level in meter of the location being passed. If this parameter is presented, 2D extrusion is used. Example: 200.

Request Header

Name Required Type Description
x-ms-client-id
  • string

Specifies which account is intended for usage in conjunction with the Azure AD security model. It represents a unique ID for the Azure Maps account and can be retrieved from the Azure Maps management plane Account API. To use Azure AD security in Azure Maps see the following articles for guidance.

Responses

Name Type Description
200 OK

OK The X-Correlation-id header value is present in an async call's response and the Event Grid event data. It helps correlate the async call’s response with the corresponding Event Grid event.

Headers

  • X-Correlation-id: string
Other Status Codes

An unexpected error occurred.

Security

AADToken

These are the Azure Active Directory OAuth2 Flows. When paired with Azure role-based access control it can be used to control access to Azure Maps REST APIs. Azure role-based access controls are used to designate access to one or more Azure Maps resource account or sub-resources. Any user, group, or service principal can be granted access via a built-in role or a custom role composed of one or more permissions to Azure Maps REST APIs.

To implement scenarios, we recommend viewing authentication concepts. In summary, this security definition provides a solution for modeling application(s) via objects capable of access control on specific APIs and scopes.

Notes

  • This security definition requires the use of the x-ms-client-id header to indicate which Azure Maps resource the application is requesting access to. This can be acquired from the Maps management API.

The Authorization URL is specific to the Azure public cloud instance. Sovereign clouds have unique Authorization URLs and Azure Active directory configurations. * The Azure role-based access control is configured from the Azure management plane via Azure portal, PowerShell, CLI, Azure SDKs, or REST APIs. * Usage of the Azure Maps Web SDK allows for configuration based setup of an application for multiple use cases.

  • Currently, Azure Active Directory v1.0 or v2.0 supports Work, School, and Guests but does not support Personal accounts.

Type: oauth2
Flow: implicit
Authorization URL: https://login.microsoftonline.com/common/oauth2/authorize

Scopes

Name Description
https://atlas.microsoft.com/.default https://atlas.microsoft.com/.default

subscription-key

This is a shared key that is provisioned when creating an Azure Maps resource through the Azure management plane via Azure portal, PowerShell, CLI, Azure SDKs, or REST APIs.

With this key, any application is authorized to access all REST APIs. In other words, these can currently be treated as master keys to the account which they are issued for.

For publicly exposed applications, our recommendation is to use server-to-server access of Azure Maps REST APIs where this key can be securely stored.

Type: apiKey
In: header

SAS Token

This is a shared access signature token is created from the List SAS operation on the Azure Maps resource through the Azure management plane via Azure portal, PowerShell, CLI, Azure SDKs, or REST APIs.

With this token, any application is authorized to access with Azure role-based access controls and fine-grain control to the expiration, rate, and region(s) of use for the particular token. In other words, the SAS Token can be used to allow applications to control access in a more secured way than the shared key.

For publicly exposed applications, our recommendation is to configure a specific list of allowed origins on the Map account resource to limit rendering abuse and regularly renew the SAS Token.

Type: apiKey
In: header

Examples

GetGeofence

Sample Request

GET https://us.atlas.microsoft.com/spatial/geofence/json?api-version=2022-08-01&deviceId=unique_device_name_under_account&udid=f6495f62-94f8-0ec2-c252-45626f82fcb2&lat=48.36&lon=-124.63&userTime=2022-08-21T17:32:28Z&searchBuffer=50&isAsync=True&mode=EnterAndExit

Sample Response

{
  "geometries": [
    {
      "deviceId": "unique_device_name_under_account",
      "udId": "f6495f62-94f8-0ec2-c252-45626f82fcb2",
      "geometryId": "2",
      "distance": 999,
      "nearestLat": 47.621954,
      "nearestLon": -122.131841
    },
    {
      "deviceId": "unique_device_name_under_account",
      "udId": "f6495f62-94f8-0ec2-c252-45626f82fcb2",
      "geometryId": "1",
      "distance": -999,
      "nearestLat": 47.609833,
      "nearestLon": -122.148274
    }
  ],
  "expiredGeofenceGeometryId": [
    "5"
  ],
  "invalidPeriodGeofenceGeometryId": [
    "3",
    "4"
  ],
  "isEventPublished": true
}

Definitions

ErrorAdditionalInfo

The resource management error additional info.

ErrorDetail

The error detail.

ErrorResponse

Error response

Geofence

This object is returned from a geofence proximity call.

GeofenceGeometry

The geofencing geometry.

GeofenceMode

Mode of the geofencing async event mechanism.

JsonFormat

Desired format of the response. Only json format is supported.

ErrorAdditionalInfo

The resource management error additional info.

Name Type Description
info
  • object

The additional info.

type
  • string

The additional info type.

ErrorDetail

The error detail.

Name Type Description
additionalInfo

The error additional info.

code
  • string

The error code.

details

The error details.

message
  • string

The error message.

target
  • string

The error target.

ErrorResponse

Error response

Name Type Description
error

The error object.

Geofence

This object is returned from a geofence proximity call.

Name Type Description
expiredGeofenceGeometryId
  • string[]

Lists of the geometry ID of the geofence which is expired relative to the user time in the request.

geometries

Lists the fence geometries that contain the coordinate position or overlap the searchBuffer around the position.

invalidPeriodGeofenceGeometryId
  • string[]

Lists of the geometry ID of the geofence which is in invalid period relative to the user time in the request.

isEventPublished
  • boolean

True if at least one event is published to the Azure Maps event subscriber, false if no event is published to the Azure Maps event subscriber. This will only be presented in response when 'isAsync' query parameter is set to true.

GeofenceGeometry

The geofencing geometry.

Name Type Description
deviceId
  • string

ID of the device.

distance
  • number

Distance from the coordinate to the closest border of the geofence (in meters except when special values -999/999 are used). Positive means the coordinate is outside of the geofence. If the coordinate is outside of the geofence, but more than the value of searchBuffer away from the closest geofence border, then the value is 999. Negative means the coordinate is inside of the geofence. If the coordinate is inside the polygon, but more than the value of searchBuffer away from the closest geofencing border, then the value is -999. A value of 999 means that there is great confidence the coordinate is well outside the geofence. A value of -999 means that there is great confidence the coordinate is well within the geofence.

geometryId
  • string

The unique id identifies a geometry.

nearestLat
  • number

Latitude of the nearest point of the geometry.

nearestLon
  • number

Longitude of the nearest point of the geometry.

nearestZ
  • number

Sea level in meter of the nearest point on the 2D extrusion geometry. This will only be presented in response when value is provided for 'zInMeter' in the request.

udId
  • string

The unique id returned from Data Upload API after uploading a valid GeoJSON FeatureCollection object. Please refer to RFC 7946 for details. All the feature's properties should contain geometryId, which is used for identifying the geometry and is case-sensitive.

GeofenceMode

Mode of the geofencing async event mechanism.

Name Type Description
All
  • string

Publish all the query results to Azure Maps account event subscription.

EnterAndExit
  • string

Only publish result when user location is considered as crossing geofencing boarder.

JsonFormat

Desired format of the response. Only json format is supported.

Name Type Description
json
  • string

The JavaScript Object Notation Data Interchange Format