Skip to content

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 kilometers
mi( X ) - the distance value X is expressed in miles
yd( 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.

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.

You do not need to declare/write any custom classes when using the Map approach. Data records are represented as java.util.Map objects. Column names become map property names and the object values are corresponding property values. === "Custom Class"

The Friend class definition:

import com.backendless.geo.GeoPoint;

public class Friend
{
  private String name;
  private String phoneNumber;
  private GeoPoint coordinates;

  public String getName()
  {
    return name;
  }

  public void setName( String name )
  {
    this.name = name;
  }

  public String getPhoneNumber()
  {
    return phoneNumber;
  }

  public void setPhoneNumber( String phoneNumber )
  {
    this.phoneNumber = phoneNumber;
  }

  public GeoPoint getCoordinates()
  {
    return coordinates;
  }

  public void setCoordinates( GeoPoint coordinated )
  {
    this.coordinates = coordinated;
  }
}

Run the following query/code to store a data object representing Bob with a link to his home in Austin, TX:

HashMap bob = new HashMap();
bob.put( "name", "Bob" );
bob.put( "phoneNumber", "512-555-1212" );
bob = Backendless.Data.of( "Friend" ).save( bob );

GeoPoint geopoint = new GeoPoint( 29.76328, -95.36327 );
geopoint.addCategory( "Home" );
geopoint.addMetadata( "description", "Bob's home" );
geopoint = Backendless.Geo.savePoint( geopoint );

ArrayList<GeoPoint> location = new ArrayList<GeoPoint>();
location.add( geopoint );
Backendless.Data.of( "Friend" ).setRelation( bob, "coordinates:GeoPoint:1", location );
Friend bob = new Friend();
bob.setName( "Bob" );
bob.setPhoneNumber( "512-555-1212" );
bob = Backendless.Data.of( Friend.class ).save( bob );

GeoPoint geopoint = new GeoPoint( 29.76328, -95.36327 ) );
geopoint.addCategory( "Home" );
geopoint.addMetadata( "description", "Bob's home" );
geopoint = Backendless.Geo.savePoint( geopoint );

ArrayList<GeoPoint> location = new ArrayList<GeoPoint>();
location.add( geopoint );
Backendless.Data.of( Friend.class ).setRelation( bob, "coordinates:GeoPoint:1", location );

Run the following query/code to store a data object representing Jane with a link to her home in Houston, TX:

HashMap jane = new HashMap();
jane.put( "name", "Jane" );
jane.put( "phoneNumber", "281-555-1212" );
jane = Backendless.Data.of( "Friend" ).save( jane );

GeoPoint geopoint = new GeoPoint( 29.76328, -95.36327 )
geopoint.addCategory( "Home" );
geopoint.addMetadata( "description", "Jane's home" );
geopoint = Backendless.Geo.savePoint( geopoint );

ArrayList<GeoPoint> location = new ArrayList<GeoPoint>();
location.add( geopoint );
Backendless.Data.of( "Friend" ).setRelation( jane, "coordinates:GeoPoint:1", location );
Friend jane = new Friend();
jane.setName( "Jane" );
jane.setPhoneNumber( "281-555-1212" );
jane = Backendless.Data.of( Friend.class ).save( jane );

GeoPoint geopoint = new GeoPoint( 29.76328, -95.36327 ) );
geopoint.addCategory( "Home" );
geopoint.addMetadata( "description", "Jane's home" );
geopoint = Backendless.Geo.savePoint( geopoint );

ArrayList<GeoPoint> location = new ArrayList<GeoPoint>();
location.add( geopoint );
Backendless.Data.of( Friend.class ).setRelation( jane, "coordinates:GeoPoint:1", location );

Run the following query/code to store a data object representing Fred with a link to his home in San Antonio, TX:

HashMap fred = new HashMap();
fred.put( "name", "Fred" );
fred.put( "phoneNumber", "210-555-1212" );
fred = Backendless.Data.of( "Friend" ).save( fred );

GeoPoint geopoint = new GeoPoint( 29.42412, -98.49363 )
geopoint.addCategory( "Home" );
geopoint.addMetadata( "description", "Fred's home" );
geopoint = Backendless.Geo.savePoint( geopoint );

ArrayList<GeoPoint> location = new ArrayList<GeoPoint>();
location.add( geopoint );
Backendless.Data.of( "Friend" ).setRelation( fred, "coordinates:GeoPoint:1", location );
Friend fred = new Friend();
fred.setName( "Fred" );
fred.setPhoneNumber( "210-555-1212" );
fred = Backendless.Data.of( Friend.class ).save( fred );

GeoPoint geopoint = new GeoPoint( 29.42412, -98.49363 ) );
geopoint.addCategory( "Home" );
geopoint.addMetadata( "description", "Fred's home" );
geopoint = Backendless.Geo.savePoint( geopoint );

ArrayList<GeoPoint> location = new ArrayList<GeoPoint>();
location.add( geopoint );
Backendless.Data.of( Friend.class ).setRelation( fred, "coordinates:GeoPoint:1", location );

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:

distance-search

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:

String whereClause = "distance( 30.26715, -97.74306, " +
                     "coordinates.latitude, coordinates.longitude ) < mi(200)";
DataQueryBuilder queryBuilder = DataQueryBuilder.create();
queryBuilder.setWhereClause( whereClause ).setRelationsDepth( 1 );
List<Map> friends = Backendless.Data.of( "Friend" ).find( queryBuilder );

String format = "%s lives at %f, %f tagged as '%s'";

for( Map friend : friends )
{
  GeoPoint coordinates = (GeoPoint) friend.get( "coordinates" );
  Log.i( "MYAPP", String.format( format, friend.get( "name" ), 
                                 coordinates.getLatitude(), 
                                 coordinates.getLongitude(),   
                                (String) coordinates.getMetadata( "description" ) ) );
}
String whereClause = "distance( 30.26715, -97.74306, " + 
                     "coordinates.latitude, coordinates.longitude ) < mi(200)";
DataQueryBuilder queryBuilder = DataQueryBuilder.create();
queryBuilder.setWhereClause( whereClause ).setRelationsDepth( 1 );
List<Friend> friends = Backendless.Data.of( Friend.class ).find( queryBuilder );

String format = "%s lives at %f, %f tagged as '%s'";

for( Friend friend : friends )
{
  GeoPoint coordinates = friend.getCoordinates();
  Log.i( "MYAPP", String.format( format, friend.getName(), 
                                 coordinates.getLatitude(), 
                                 coordinates.getLongitude(),   
                                 (String) coordinates.getMetadata( "description" ) ) );
}

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.