Spatial - Get Geofence

Search Geofence Get API

Applies to: S1 Pricing tier.

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://atlas.microsoft.com/spatial/geofence/json?api-version=1.0&deviceId={deviceId}&udId={udId}&lat={lat}&lon={lon}
GET https://atlas.microsoft.com/spatial/geofence/json?subscription-key={subscription-key}&api-version=1.0&deviceId={deviceId}&udId={udId}&lat={lat}&lon={lon}&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.

subscription-key
query
  • string

One of the Azure Maps keys provided from an Azure Map Account. Refer to the subscription-key security definition.

api-version
query True
  • string

Version number of Azure Maps API. Current version is 1.0

deviceId
query True
  • string

ID of the device

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.

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.

userTime
query
  • string

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

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.

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.

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 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
400 Bad Request

Bad request: one or more parameters were incorrectly specified or are mutually exclusive.

401 Unauthorized

Access denied due to invalid subscription key or invalid Azure Active Directory bearer token. Make sure to provide a valid key for an active Azure subscription and Maps resource. Otherwise, verify the WWW-Authenticate header for error code and description of the provided AAD bearer token.

Headers

  • WWW-Authenticate: string
403 Forbidden

Permission, capacity, or authentication issues.

404 Not Found

Not Found: the requested resource could not be found, but it may be available again in the future.

500 Internal Server Error

An error occurred while processing the request. Please try again later.

Security

azure_auth

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.

Note

  • 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 tokens are supported.

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

subscription-key

This is a shared key which 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: query

Examples

Geofence

Sample Request

GET https://atlas.microsoft.com/spatial/geofence/json?subscription-key=[subscription-key]&api-version=1.0&deviceId=unique_device_name_under_account&udId=[udId]&lat=48.36&lon=-124.63&userTime=2018-09-10T10:00:00&searchBuffer=50&isAsync=True&mode=EnterAndExit

Sample Response

{
  "geometries": [
    {
      "deviceId": "unique_device_name_under_account",
      "udId": "[udId]",
      "geometryId": "2",
      "distance": 999,
      "nearestLat": 47.621954,
      "nearestLon": -122.131841
    },
    {
      "deviceId": "unique_device_name_under_account",
      "udId": "[udId]",
      "geometryId": "1",
      "distance": -999,
      "nearestLat": 47.609833,
      "nearestLon": -122.148274
    }
  ],
  "expiredGeofenceGeometryId": [
    "5"
  ],
  "invalidPeriodGeofenceGeometryId": [
    "3",
    "4"
  ],
  "isEventPublished": true
}
{
  "error": {
    "code": "BadRequest",
    "message": "Can't find user geofencing data with provided udid.",
    "innererror": {
      "code": "NoUserDataWithUdid"
    }
  }
}
{
  "error": {
    "code": "401 Unauthorized",
    "message": "Access denied due to invalid subscription key. Make sure to provide a valid key for an active subscription."
  }
}
{
  "error": {
    "code": "403 Forbidden",
    "message": "Permission, capacity, or authentication issues."
  }
}
{
  "error": {
    "code": "404 NotFound",
    "message": "Not Found: the requested resource could not be found, but it may be available again in the future."
  }
}
{
  "error": {
    "code": "500 InternalServerError",
    "message": "An error occurred while processing the request. Please try again later."
  }
}

Definitions

Error

This object is returned when an error occurs in the Maps API

ErrorResponse

This response object is returned when an error occurs in the Maps API

GeofenceGeometry

The geofencing geometry.

GeofenceResponse

This object is returned from a geofence proximity call.

JsonFormat

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

Mode

Mode of the geofencing async event mechanism.

Error

This object is returned when an error occurs in the Maps API

Name Type Description
code
  • string

The HTTP status code.

message
  • string

If available, a human readable description of the error.

ErrorResponse

This response object is returned when an error occurs in the Maps API

Name Type Description
error

This object is returned when an error occurs in the Maps API

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. 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.

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.

GeofenceResponse

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.

JsonFormat

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

Name Type Description
json
  • string

The JavaScript Object Notation Data Interchange Format

Mode

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.