Location-Based Filters

AVAILABLE API VERSION
API v57.0 and later
Compound fields available in API v59.0 and later

Run a query on a geolocation field for a supported object using the GeolocationInput type. The geolocation custom field types stores location values on records using latitude and longitude values. You can create queries that calculate the distance between two location values or between a location value and a latitude-longitude coordinate.

To create a location-based filter, use the where and orderBy arguments.

A compound geolocation field includes latitude and longitude values. To work with compound geolocation fields, reference the individual components of the returned value. For example, if your geolocation field name is Location, use the Location__Latitude__s and Location__Longitude__s.

The previous query is similar to the following SOQL statement.

Pass in the where argument to filter records based on a geolocation compound field. Let's say you have a custom geolocation field Location__c on accounts. This where argument finds accounts that are within 500 miles of 47.6062°, -122.3321° (Seattle, WA) using the lt function.

The previous query is similar to the following SOQL statement.

Given a set of records with geolocation fields, you can sort them by distance using the distance filter type with the orderBy argument. You can also order the results in ascending or descending order.

The geolocation field support ordering using the OrderByClause type.

Here's a basic query that orders the results in ascending order and null records at the end.

The previous query is similar to the following SOQL statement.

The following example adds the orderBy argument to a where argument.

The previous query is similar to the following SOQL statement.

When you query geolocation compound fields, use the constituent fields in the query.

Provide a direct reference to the internal primitive data types. For example, use Location__Latitude__s and Location__Longitude__s if you have a compound geolocation field Location__c.

Similar to how SOQL calculates and compares distances, you can’t check locations or distances for equality with GraphQL API. You can only determine whether a location is farther or closer than another location, or one distance is greater or smaller than another.

Field Operators: Geolocation Field Types

Object Reference: Geolocation Compound Field

SOQL and SOSL Reference: Location-Based SOQL Queries

SOQL SELECT Syntax: Order By