Spatial - Post Geofence

Use to get the proximity of a coordinate to a geofence.

Note

Azure Maps Spatial service retirement

The Azure Maps Spatial service is now deprecated and will be retired on 9/30/25. For more information, see End of Life Announcement of Azure Maps Spatial.

The Post Geofence API is an HTTP POST request that retrieveS the proximity of a coordinate to a provided geofence or set of fences. With POST requests, you do not have to upload the fence data in advance, instead you supply the location of the object you are tracking in query parameters as well as the fence or set of fences data in post request body. For more information on on the geofence data format, see Geofencing GeoJSON data. 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).

POST https://{geography}.atlas.microsoft.com/spatial/geofence/json?api-version=2022-08-01&deviceId={deviceId}&lat={lat}&lon={lon}
POST https://{geography}.atlas.microsoft.com/spatial/geofence/json?api-version=2022-08-01&deviceId={deviceId}&lat={lat}&lon={lon}&z={z}&userTime={userTime}&searchBuffer={searchBuffer}&isAsync={isAsync}&mode={mode}

URI Parameters

Name In Required Type Description
format
path True

JsonFormat

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.

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

GeofenceMode

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

z
query

number

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

Request Header

Name Required Type Description
x-ms-client-id

string

Specifies which account is intended for usage in conjunction with the Microsoft Entra ID 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 Microsoft Entra ID security in Azure Maps see the following articles for guidance.

Request Body

Name Required Type Description
features True

GeoJsonFeature[]

Contains a list of valid GeoJSON Feature objects.

type True string:

FeatureCollection

Specifies the GeoJSON type. Must be one of the nine valid GeoJSON object types - Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon, GeometryCollection, Feature and FeatureCollection.

Responses

Name Type Description
200 OK

Geofence

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

ErrorResponse

An unexpected error occurred.

Security

AADToken

These are the Microsoft Entra OAuth 2.0 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 Microsoft Entra ID 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.

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

PostGeofence

Sample request

POST https://us.atlas.microsoft.com/spatial/geofence/json?api-version=2022-08-01&deviceId=unique_device_name_under_account&lat=48.36&lon=-124.63&userTime={userTime}&searchBuffer=50&isAsync=True&mode=EnterAndExit

{
  "type": "FeatureCollection",
  "features": [
    {
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -122.13241226662022,
              47.61701140091722
            ],
            [
              -122.12810106940353,
              47.6169969269402
            ],
            [
              -122.12824948956276,
              47.61907683751349
            ],
            [
              -122.12833297981392,
              47.621929787055336
            ],
            [
              -122.12971398040168,
              47.62184100705295
            ],
            [
              -122.1318413862121,
              47.62195364373008
            ],
            [
              -122.13231034769727,
              47.61716332618121
            ],
            [
              -122.13241226662022,
              47.61701140091722
            ]
          ]
        ]
      },
      "properties": {
        "geometryId": "2",
        "name": "Crossroad Mall"
      }
    },
    {
      "type": "Feature",
      "geometry": {
        "type": "Polygon",
        "coordinates": [
          [
            [
              -122.1534220563239,
              47.60981818546625
            ],
            [
              -122.153451623509,
              47.60628733146004
            ],
            [
              -122.14971782206638,
              47.606250040787046
            ],
            [
              -122.14817354810637,
              47.606391046012305
            ],
            [
              -122.1482735128807,
              47.60983316796356
            ],
            [
              -122.15225500989803,
              47.60982613678752
            ],
            [
              -122.1534220563239,
              47.60981818546625
            ]
          ]
        ]
      },
      "properties": {
        "geometryId": "1",
        "name": "Sammamish High school"
      }
    }
  ]
}

Sample response

{
  "geometries": [
    {
      "deviceId": "unique_device_name_under_account",
      "geometryId": "2",
      "distance": -999,
      "nearestLat": 47.621954,
      "nearestLon": -122.131841
    },
    {
      "deviceId": "unique_device_name_under_account",
      "geometryId": "1",
      "distance": 999,
      "nearestLat": 47.609833,
      "nearestLon": -122.148274
    }
  ],
  "expiredGeofenceGeometryId": [],
  "invalidPeriodGeofenceGeometryId": [],
  "isEventPublished": true
}

Definitions

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

GeoJsonFeature

A valid GeoJSON Feature object type. Please refer to RFC 7946 for details.

GeoJsonFeatureCollection

A valid GeoJSON FeatureCollection object type. Please refer to RFC 7946 for details.

GeoJsonGeometry

A valid GeoJSON geometry object. The type must be one of the seven valid GeoJSON geometry types - Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon and GeometryCollection. Please refer to RFC 7946 for details.

GeoJsonGeometryCollection

A valid GeoJSON GeometryCollection object type. Please refer to RFC 7946 for details.

GeoJsonLineString

A valid GeoJSON LineString geometry type. Please refer to RFC 7946 for details.

GeoJsonMultiLineString

A valid GeoJSON MultiLineString geometry type. Please refer to RFC 7946 for details.

GeoJsonMultiPoint

A valid GeoJSON MultiPoint geometry type. Please refer to RFC 7946 for details.

GeoJsonMultiPolygon

A valid GeoJSON MultiPolygon object type. Please refer to RFC 7946 for details.

GeoJsonPoint

A valid GeoJSON Point geometry type. Please refer to RFC 7946 for details.

GeoJsonPolygon

A valid GeoJSON Polygon geometry type. Please refer to RFC 7946 for details.

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

ErrorAdditionalInfo[]

The error additional info.

code

string

The error code.

details

ErrorDetail[]

The error details.

message

string

The error message.

target

string

The error target.

ErrorResponse

Error response

Name Type Description
error

ErrorDetail

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

GeofenceGeometry[]

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 used when creating a Data registry to upload 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. For more information on the data registry service, see How to create data registry.

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.

GeoJsonFeature

A valid GeoJSON Feature object type. Please refer to RFC 7946 for details.

Name Type Description
featureType

string

The type of the feature. The value depends on the data model the current feature is part of. Some data models may have an empty value.

geometry GeoJsonGeometry:

A valid GeoJSON geometry object. The type must be one of the seven valid GeoJSON geometry types - Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon and GeometryCollection. Please refer to RFC 7946 for details.

id

string

Identifier for the feature.

type string:

Feature

Specifies the GeoJSON type. Must be one of the nine valid GeoJSON object types - Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon, GeometryCollection, Feature and FeatureCollection.

GeoJsonFeatureCollection

A valid GeoJSON FeatureCollection object type. Please refer to RFC 7946 for details.

Name Type Description
features

GeoJsonFeature[]

Contains a list of valid GeoJSON Feature objects.

type string:

FeatureCollection

Specifies the GeoJSON type. Must be one of the nine valid GeoJSON object types - Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon, GeometryCollection, Feature and FeatureCollection.

GeoJsonGeometry

A valid GeoJSON geometry object. The type must be one of the seven valid GeoJSON geometry types - Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon and GeometryCollection. Please refer to RFC 7946 for details.

Name Type Description
type

GeoJsonObjectType

Specifies the GeoJSON type. Must be one of the nine valid GeoJSON object types - Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon, GeometryCollection, Feature and FeatureCollection.

GeoJsonGeometryCollection

A valid GeoJSON GeometryCollection object type. Please refer to RFC 7946 for details.

Name Type Description
geometries GeoJsonGeometry[]:

Contains a list of valid GeoJSON geometry objects. Note that coordinates in GeoJSON are in x, y order (longitude, latitude).

type string:

GeometryCollection

Specifies the GeoJSON type. Must be one of the nine valid GeoJSON object types - Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon, GeometryCollection, Feature and FeatureCollection.

GeoJsonLineString

A valid GeoJSON LineString geometry type. Please refer to RFC 7946 for details.

Name Type Description
coordinates

number[]

Coordinates for the GeoJson LineString geometry.

type string:

LineString

Specifies the GeoJSON type. Must be one of the nine valid GeoJSON object types - Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon, GeometryCollection, Feature and FeatureCollection.

GeoJsonMultiLineString

A valid GeoJSON MultiLineString geometry type. Please refer to RFC 7946 for details.

Name Type Description
coordinates

number[]

Coordinates for the GeoJson MultiLineString geometry.

type string:

MultiLineString

Specifies the GeoJSON type. Must be one of the nine valid GeoJSON object types - Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon, GeometryCollection, Feature and FeatureCollection.

GeoJsonMultiPoint

A valid GeoJSON MultiPoint geometry type. Please refer to RFC 7946 for details.

Name Type Description
coordinates

number[]

Coordinates for the GeoJson MultiPoint geometry.

type string:

MultiPoint

Specifies the GeoJSON type. Must be one of the nine valid GeoJSON object types - Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon, GeometryCollection, Feature and FeatureCollection.

GeoJsonMultiPolygon

A valid GeoJSON MultiPolygon object type. Please refer to RFC 7946 for details.

Name Type Description
coordinates

number[]

Contains a list of valid GeoJSON Polygon objects. Note that coordinates in GeoJSON are in x, y order (longitude, latitude).

type string:

MultiPolygon

Specifies the GeoJSON type. Must be one of the nine valid GeoJSON object types - Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon, GeometryCollection, Feature and FeatureCollection.

GeoJsonPoint

A valid GeoJSON Point geometry type. Please refer to RFC 7946 for details.

Name Type Description
coordinates

number[]

A Position is an array of numbers with two or more elements. The first two elements are longitude and latitude, precisely in that order. Altitude/Elevation is an optional third element. Please refer to RFC 7946 for details.

type string:

Point

Specifies the GeoJSON type. Must be one of the nine valid GeoJSON object types - Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon, GeometryCollection, Feature and FeatureCollection.

GeoJsonPolygon

A valid GeoJSON Polygon geometry type. Please refer to RFC 7946 for details.

Name Type Description
coordinates

number[]

Coordinates for the GeoJson Polygon geometry type.

type string:

Polygon

Specifies the GeoJSON type. Must be one of the nine valid GeoJSON object types - Point, MultiPoint, LineString, MultiLineString, Polygon, MultiPolygon, GeometryCollection, Feature and FeatureCollection.

JsonFormat

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

Name Type Description
json

string

The JavaScript Object Notation Data Interchange Format