Skip to main content

Distance

Perform location based calculations.

SOQL.of(Account.SObjectType)
.whereAre(
SOQL.Filter.with(
SOQL.Distance.of(Account.BillingAddress)
.between(72.0, -135.0)
.mi()
).lessThan(5)
).toList();

Methods​

The following are methods for Distance.

FIELDS

COMPERATORS

UNITS

FIELDS​

of​

  • DISTANCE(BillingAddress, GEOLOCATION(72.0,-136.0), 'mi')

Signature

Distance of(SObjectField ofField)

Example

SELECT Id
FROM Account
WHERE DISTANCE(BillingAddress, GEOLOCATION(72.0,-136.0), 'mi') < 5
SOQL.of(Account.SObjectType)
.whereAre(
SOQL.Filter.with(
SOQL.Distance.of(Account.BillingAddress)
.between(72.0,-136.0)
.mi()
).lessThan(5)
)
.toList();

COMPERATORS​

between​

  • DISTANCE(BillingAddress, GEOLOCATION(72.0,-136.0), 'mi')

Signature

Distance between(Location loc)
Distance between(Decimal latitude, Decimal longitude)

Example

SELECT Id
FROM Account
WHERE DISTANCE(BillingAddress, GEOLOCATION(72.0,-136.0), 'mi') < 5
Location loc = Location.newInstance(72.0,-136.0);
SOQL.of(Account.SObjectType)
.whereAre(
SOQL.Filter.with(
SOQL.Distance.of(Account.BillingAddress)
.between(loc)
.mi()
).lessThan(5)
)
.toList();

UNITS​

mi​

Return the distance in miles.

Signature

Distance mi()

Example

SELECT Id
FROM Account
WHERE DISTANCE(BillingAddress, GEOLOCATION(72.0,-136.0), 'mi') < 5
SOQL.of(Account.SObjectType)
.whereAre(
SOQL.Filter.with(
SOQL.Distance.of(Account.BillingAddress)
.between(72.0,-136.0)
.mi()
).lessThan(5)
)
.toList();

km​

Return the distance in kilometers.

Signature

Distance km()

Example

SELECT Id
FROM Account
WHERE DISTANCE(BillingAddress, GEOLOCATION(72.0,-136.0), 'km') < 5
SOQL.of(Account.SObjectType)
.whereAre(
SOQL.Filter.with(
SOQL.Distance.of(Account.BillingAddress)
.between(72.0,-136.0)
.km()
).lessThan(5)
)
.toList();