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โ
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();
of related fieldโ
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();