Backendless Support
 

Using SQL queries for geopoint searches

Backendless Service Platform provides a developer a unique mBaaS feature to use SQL queries when searching for data. 

The geolocation data managed by Backendless is not an exception. 

A geopoint may include metadata, which is an arbitrary collection of key/value pairs. Geopoints may be searched for using SQL-based queries. A query must be the "where" part of a traditional SQL statement. It can reference the metadata properties as if they are table columns.

The example below uses the sample data which can be installed into any Backendless backend. The data is a collection of geopoints representing cities around the world. Each geopoint contains metadata with the name of the city. 

The sample below runs the following SQL query:

city in ('AUSTIN', 'DALLAS')

The query searches for all database where the metadata property name is "city" and the value may be either 'AUSTIN' or 'DALLAS':

Asynchronous sample (Android and Plain Java):
BackendlessGeoQuery geoQuery = new BackendlessGeoQuery();
geoQuery.addCategory( "geoservice_sample" );
geoQuery.setIncludeMeta( true );
geoQuery.setWhereClause( "city in ('AUSTIN', 'DALLAS')" );
Backendless.Geo.getPoints( geoQuery, new AsyncCallback<BackendlessCollection<GeoPoint>>()
{
 @Override
 public void handleResponse( BackendlessCollection<GeoPoint> geoPointCollection )
 {
 Iterator<GeoPoint> iterator = geoPointCollection.getCurrentPage().iterator();
 while( iterator.hasNext() )
 {
 GeoPoint geoPoint = iterator.next();
 System.out.println( "GeoPoint - " + geoPoint );
 }
 }
 @Override
 public void handleFault( BackendlessFault backendlessFault )
 {
 System.out.println( "Server reported an error - " + backendlessFault.getMessage() );
 }
} );
Synchronous sample (Plain Java only):
BackendlessGeoQuery geoQuery = new BackendlessGeoQuery();
geoQuery.addCategory( "geoservice_sample" );
geoQuery.setIncludeMeta( true );
geoQuery.setWhereClause( "city in ('AUSTIN', 'DALLAS')" );
BackendlessCollection<GeoPoint> geoPointCollection = Backendless.Geo.getPoints( geoQuery );
Iterator<GeoPoint> iterator = geoPointCollection.getCurrentPage().iterator();
while( iterator.hasNext() )
{
 GeoPoint geoPoint = iterator.next();
 System.out.println( "GeoPoint - " + geoPoint );
}

The program produces the following output:

GeoPoint - GeoPoint{objectId='55DEE179-A7EE-9491-FF5A-6644F8E10200', latitude=43.66663, longitude=-92.97464, categories=[geoservice_sample], metadata={city=AUSTIN}, distance=null}
GeoPoint - GeoPoint{objectId='D60EFC78-673D-0DA8-FFD7-53C8D8BF8D00', latitude=32.78306, longitude=-96.80667, categories=[geoservice_sample], metadata={city=DALLAS}, distance=null}
GeoPoint - GeoPoint{objectId='C594A668-B9A2-4D24-FF5A-D4F700DECB00', latitude=30.26715, longitude=-97.74306, categories=[geoservice_sample], metadata={city=AUSTIN}, distance=null}

Review related topic:

Is article helpful?