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 sobject field​

Specify the geolocation field used in the distance calculation.

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();

Specify a parent relationship geolocation field used in the distance calculation.

Signature

Distance of(String relationshipName, SObjectField ofField)

Example

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

COMPERATORS​

between location​

Specify the target geolocation using a Location instance.

Signature

Distance between(Location loc)

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();

between latitude longitude​

Specify the target geolocation using latitude and longitude values.

Signature

Distance between(Decimal latitude, Decimal longitude)

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();

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();