# 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](/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`.

```sfdocs-code {"lang":"graphql", "title": "Query records with compound geolocation fields", "src": "" }
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.

```sfdocs-code {"lang":"sql", "title": "", "src": "" }
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.

```sfdocs-code {"lang":"graphql", "title": "Find accounts within 500 miles of a specified lat/long ", "src": "" }
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.

```sfdocs-code {"lang":"sql", "title": "", "src": "" }
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](/docs/platform/graphql/guide/order.md).

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

```sfdocs-code {"lang":"graphql", "title": "Find accounts closest to a specified lat/long in ascending order", "src": "" }
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.

```sfdocs-code {"lang":"sql", "title": "", "src": "" }
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.

```sfdocs-code {"lang":"graphql", "title": "Find accounts within 500 miles of Seattle and order by distance", "src": "" }
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.

```sfdocs-code {"lang":"sql", "title": "", "src": "" }
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.

```sfdocs-code {"lang":"graphql", "title": "", "src": "" }
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`.

```sfdocs-code {"lang":"graphql", "title": "", "src": "" }
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](/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)
