Search by distance¶
With the ability to store spatial data in the database,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)
where:
Argument | Description |
---|---|
<operator> |
Possible values are <, >, =, >=, <= |
units-function |
Defines the units of measure for the distance. Possible values are:ft( X ) - the distance value X is expressed in feet km( X ) - the distance value X is expressed in kilometersmi( X ) - the distance value X is expressed in milesyd( X ) - the distance value X is expressed in yards |
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. It is important that the entire whereClause which contains the distance
function is URL-encoded.
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:
curl -H Content-Type:"application/json" \
-X POST \
-v "http://xxxx.backendless.app/data/Person" \
-d "{\"name\":\"Bob\", \"age\":33, \"phoneNumber\":\"512-555-1212\", \"coordinates\": {\"___class\":\"GeoPoint\",\"latitude\":30.26715,\"longitude\":-97.74306,\"categories\":[\"Home\"],\"metadata\":{\"description\":\"Bob's home\"}}}"
curl -H Content-Type:"application/json" \
-X POST \
-v "http://xxxx.backendless.app/data/Person" \
-d "{\"name\":\"Jane\", \"age\":28, \"phoneNumber\":\"281-555-1212\", \"coordinates\": {\"___class\":\"GeoPoint\",\"latitude\":29.76328,\"longitude\":-95.36327,\"categories\":[\"Home\"],\"metadata\":{\"description\":\"Jane's home\"}}}"
curl \
-H Content-Type:"application/json" \
-X POST \
-v "http://xxxx.backendless.app/data/Person" \
-d "{\"name\":\"Fred\", \"age\":35, \"phoneNumber\":\"210-555-1212\", \"coordinates\": {\"___class\":\"GeoPoint\",\"latitude\":29.42412,\"longitude\":-98.49363,\"categories\":[\"Home\"],\"metadata\":{\"description\":\"Fred's home\"}}}"
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:
curl -H Content-Type:"application/json" \
"http://xxxx.backendless.app/data/Person?where=distance(%2030.084%2C%20-94.145%2C%20coordinates.latitude%2C%20coordinates.longitude%20)%20%3C%20mi(300)"
The search result is an array of objects sorted in the ascending order:
[
{
"phoneNumber": "210-555-1212",
"created": 1481454024000,
"name": "Fred",
"___class": "Person",
"coordinates": null,
"location": null,
"ownerId": null,
"updated": null,
"age": 35,
"objectId": "BD612F56-C21E-0B2E-FF0D-F1B0D153A800",
"__meta": "..skipped for brevity.."
},
{
"phoneNumber": "512-555-1212",
"created": 1481453836000,
"name": "Bob",
"___class": "Person",
"coordinates": null,
"location": null,
"ownerId": null,
"updated": null,
"age": 33,
"objectId": "84EAFDA6-FDEB-C645-FF7B-2E8530394700",
"__meta": "..skipped for brevity.."
},
{
"phoneNumber": "281-555-1212",
"created": 1481453944000,
"name": "Jane",
"___class": "Person",
"coordinates": null,
"location": null,
"ownerId": null,
"updated": null,
"age": 28,
"objectId": "10888A63-5997-B3A0-FF59-E4A922854300",
"__meta": "..skipped for brevity.."
}
]
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.