Search by distance¶
With the ability to link data objects with geo points, you can search for data objects by distance. This type of search returns a paged set of data objects which satisfy the
whereClause condition and located within the specified distance. Distance-based search uses a special function in
whereClause of the search request. The syntax of the function is:
distance( center point latitude, center point longitude, columnname which contains geo point.latitude, columnname which contains geo point.longitude )<operator> units-function(value)
||Possible values are <, >, =, >=, <=|
||Defines the units of measure for the distance. Possible values are:
For example, the following
whereClause expression searches for data objects located within 200 miles from the point at
30.26715, -97.74306. Each data object must have the
coordinates property of type GeoPoint.
distance( 30.26715, -97.74306, coordinates.latitude, coordinates.longitude ) < mi(200)
The following example demonstrates a search-by-distance query. The example uses three data objects stored in the
Friend table: Bob, Jane, and Fred who respectively live in Austin, Houston, San Antonio. The search query in the example finds all friends who live within the specified distance. Before running the search query, create the objects in the data storage with the corresponding geo points.
The application must be setup with demo data in order to run the example and see the
distance operator in action. See the setup code in the expandable block below:
Setting up the app with demo data.
Run the following query/code to store a data object representing Bob with a link to his home in Austin, TX: Run the following query/code to store a data object representing Jane with a link to her home in Houston, TX: Run the following query/code to store a data object representing Fred with a link to his home in San Antonio, TX:
Once the data is in the database and the geo location storage, you can verify it in Backendless Console by opening the Geolocation screen and selecting the Home geocategory:
Suppose you need to get all objects located within 300 miles radius from
Beaumont, TX, which has the GPS coordinates of
30.084, -94.145. The following code/query performs that distance-based search:
The search returns all data objects within the specified distance. Each data object has the
Coordinates property containing the coordinates of a geo point associated with this data object.