function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Geolocation bounded box SOQL queries

3 questions:

1) How can I get a list of records that reside within a certain bounded box (aka extent) on the map?
2) How can I get a list of records that intersect with a straight line on the map?
3) If my Geolocation fields are not standard x-y coordinates (spatial reference different than 4326) how does SOQL figure out the DISTANCE clause?

Hi , 

See the article below, 

Searching for Data based on Location
Now the fun stuff.  Having geocoded some contacts based on the Sears (err, uh … Willis) Tower here in Chicago as well as the Salesforce HQ in downtown San Francisco, we can hop into the also-new-to-Winter13 query editor within Developer Console and do the following search:

SELECT Name FROM Contact WHERE DISTANCE(Location__c, GEOLOCATION(37.7945391,-122.3947166), 'mi') < 1
That SOQL statement will do the distance calculation for us and return our one contact within a mile tagged for those coordinates (One Market Street in San Francisco):

If I flip that lesser than over to a greater than, we get everyone outside of a mile from Salesforce HQ, namely:

Retrieve records with locations saved in geolocation or address fields as individual latitude and longitude values by appending “__latitude__s” or “__longitude__s” to the field name, instead of the usual “__c”. For example:
SELECT Name, Location__latitude__s, Location__longitude__s
FROM Warehouse__c
This query finds all of the warehouses that are stored in the custom object Warehouse. The results include each warehouse’s latitude and longitude values individually. Note the use of the separate field components for Location__c, to select the latitude and longitude components individually.
SOQL executed using the SOAP or REST APIs can SELECT the compound field, instead of the individual elements. Compound fields are returned as structured data, rather than primitive values. For example:
SELECT Name, Location__c
FROM Warehouse__c

WHERE Clause
Retrieve records with locations within or outside of a certain radius with distance conditions in the WHERE clause of the query. Use the following functions to construct an appropriate distance condition.
Calculates the distance between two locations in miles or kilometers.
Usage: DISTANCE(mylocation1, mylocation2, 'unit') and replace mylocation1 and mylocation2 with two location fields, or a location field and a value returned by the GEOLOCATION function. Replace unit with mi (miles) or km (kilometers).
Returns a geolocation based on the provided latitude and longitude. Must be used with the DISTANCE function.
Usage: GEOLOCATION(latitude, longitude) and replace latitude and longitude with the corresponding geolocation, numerical code values.
Compare two field values, or a field value with a fixed location. For example:
SELECT Name, Location__c
FROM Warehouse__c
WHERE DISTANCE(Location__c, GEOLOCATION(37.775,-122.418), 'mi') < 20