---
schema_version: "1"
id: 31d209b121c7da29d653fe0114a0948653e6745c315074efb3f5fb066c82939a
title: Location-Based Filters
collection: developer
version: current
locale: en-us
portal_url: https://docs.salesforce.com/developer/current/en-us/graphql-api/graphql/guides/filter/filter-location.md
canonical_url: https://developer.salesforce.com/docs/platform/graphql/guide/filter-location.html
source_site: developer.salesforce.com
content_type: documentation
content_hash: aeb0cbe881c9e553bdc36ff98281a8bc9c98c171f0876580c23ff206023d6fc8
products:
  - GraphQL API
---
# 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](https://developer.salesforce.com/docs/platform/graphql/guide/filter-fields.md#geolocation-field-types). 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.

## Query Records with Geolocation Fields

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`.

```
query accounts {
  uiapi {
    query {
      Account{
        edges {
          node {
            Id
            Name {
              value
            }
            Location__Latitude__s {
              value
            },
            Location__Longitude__s {
              value
            }
          }
        }
      }
    }
  }
}
```

The previous query is similar to the following SOQL statement.

```
SELECT Name, Location__latitude__s, Location__longitude__s
FROM Account
```

## Filter Using `where`

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.

```
Account(where: {
  Location__c: {
    lt: {
        latitude: 47.6062
        longitude: 122.3321
        radius: 500
        unit: MI
      }
    }
  }) {
        edges {
          node {
            DisplayValue
          }
        }
      }
```

The previous query is similar to the following SOQL statement.

```
WHERE DISTANCE(Location__c, GEOLOCATION(47.6062,-122.3321), 'mi') < 500
```

## Order Results Using `orderBy`

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](https://developer.salesforce.com/docs/platform/graphql/order.md).

Here's a basic query that orders the results in ascending order and null records at the end.

```
query {
  uiapi {
    query {
      Account(orderBy:{
        Location__c:{
          distance:{
            latitude:47.6062
            longitude:122.3321
          },
          nulls:LAST
          order:ASC
        }
      }) {
        edges {
          node {
            Id
            Name {
              value
            },
            Location__Latitude__s {
              value
            },
            Location__Longitude__s {
              value
            }
          }
        }
      }
    }
  }
}
```

The previous query is similar to the following SOQL statement.

```
SELECT Name, Location__latitude__s, Location__longitude__s
FROM Account
ORDER BY DISTANCE(Location__c, GEOLOCATION(47.606, 122.332), 'mi') ASC NULLS LAST
```

The following example adds the `orderBy` argument to a `where` argument.

```
query {
  uiapi {
    query {
      Account(where: {
        Location__c: {
          lt: {
             Latitude: 47.6062
             Longitude: 122.3321
             radius: 500
             unit: MI
           }
         }
       },
       orderBy: {
         Location__c: {
           distance: {
             latitude: 47.6062
             longitude: 122.3321
           }
          }
        }) {
          edges {
            node {
              Id
              Name {
                value
              },
              Location__Latitude__s {
                value
              },
              Location__Longitude__s {
                value
              }
            }
          }
        }
      }
   }
}
```

The previous query is similar to the following SOQL statement.

```
SELECT Id, Name
FROM Account
WHERE DISTANCE(Location__c, GEOLOCATION(47.6062,-122.3321), 'mi') < 500
ORDER BY DISTANCE(Location__c, GEOLOCATION(47.6062,-122.3321), 'mi')
```

## Location-Based Query Limitations

When you query geolocation compound fields, use the constituent fields in the query.

```
edges{
  node{
    Name {
      value
    },
    Location__c { # don't do this
      value
    }
  }
}
```

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`.

```
query AccountLocations {
  uiapi {
    query {
      Account (where: { Location__Latitude__s: { ne: null }}){
        edges {
          node {
            Id
            Name {
              value
            }
            Location__c { # Geolocation compound field
              Location__Latitude__s { # Constituent field
                value
              },
              Location__Longitude__s { # Constituent field
                value
              }
            }
          }
        }
      }
    }
  }
}
```

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.

## See Also

[Field Operators: Geolocation Field Types](https://developer.salesforce.com/docs/platform/graphql/guide/filter-fields.md#geolocation-field-types)

[Object Reference: Geolocation Compound Field](https://developer.salesforce.com/docs/atlas.en-us.object_reference.meta/object_reference/compound_fields_geolocation.htm)

[SOQL and SOSL Reference: Location-Based SOQL Queries](https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_geolocate.htm?language=en&q=implicit)

[SOQL SELECT Syntax: Order By](https://developer.salesforce.com/docs/atlas.en-us.soql_sosl.meta/soql_sosl/sforce_api_calls_soql_select_orderby.htm)
