Service Board > Max for Developers > Max Groovy APIs > SMQL > SMQL Geospatial Queries
SMQL Geospatial Queries
distance_within
You can use the distance_within function to select all records of a specified object that have a geocode field location within the distance specified as the last parameter in meters from the point passed as a parameter.
Format
distance_within(path_to_field_of_datatype_geocode, latitude, longitude, distanceInMeters)
* 
You must add a field with the Geocode data type to the relevant object and input records with latitude and longitude coordinates in the newly added field.
Parameters
Parameter Name
Description
Example
path_to_field_of_datatype_geocode
Full SQML path to a field with the Geocode data type that is included in the object being queried.
svmx_geocode_field
latitude, longitude
Geographical coordinates in latitude and longitude decimal degrees from which distance is calculated. You can pass the coordinates as one String (example 1), two double values (example 2), or two separate Strings (example 3).
(1)
-37.4433, -68.2233
(2)
23.2232, -0.3234
(3)
-12.3444, 65.1888
distanceInMeters
The distance value to compare against, which is a double value expressed in meters.
1000.33, 1230000, 264.1
Result
Returns true and selects each row in which the field value of path_to_field_of_datatype_geocode falls within the distance specified in distanceInMeters from the point specified with (latitude, longitude)to the location specified in the relevant field value.
Examples
To work with the following examples, import the Database class.
import com.servicemax.core.Database;
You can query records by specifying the geocode field and point coordinates as a single String:
def records = Database.query("select * from io_cities_for_test_object where distance_within(io_geocode_field, '37.7749,-122.4194', 1000000.0)");
def record = records.get(0);
record.io_name // this is, for example San Francisco
You can also specify coordinates as two separate doubles:
For more information:
Was this helpful?