Backendless Support
 

Loading data objects with related geopoints using API

Backendless Geolocation Service allows a developer to declare relationships between an object in a data table schema and to create relationships between an object in a data table schema and geopoints. Worth mentioning the fact that an app developer can accomplish both operations mentioned above with the API.

In this article the feature of retrieving a data object which has a related geopoint within Backendless Geolocation Service is precisely highlighted.

Considering the object below one can notice that the Address table has the location column of the GEOPOINT RELATIONSHIP type:

There are three data objects in the table and one of them has a related geopoint. The geopoint is shown on the screenshot below. Notice the geopoint's metadata ( city: NEW YORK CITY):  

An object from the Address table should be an instance of the following class. The package name is irrelevant - it can be anything.

NOTICE: the location column from the table is represented through the com.backendless.geo.GeoPoint class:

package com.mbaas.sample;
import com.backendless.geo.GeoPoint;
public class Address
{
 public String street;
 public String city;
 public GeoPoint location;
}

The following code retrieves Address objects using an SQL-query. Additionally, the query options include a request for the related "location" property.

As a result, when the server returns the Address objects, they will have the location property pre-initialized.

This approach is described more precisely in the " Loading related data objects – the ‘one-step/dynamic’ approach" article.

Asynhronous API sample: (Android and Plain Java):

BackendlessDataQuery dataQuery = new BackendlessDataQuery();
dataQuery.setWhereClause( "location.city = 'NEW YORK CITY'" );
QueryOptions queryOptions = new QueryOptions();
queryOptions.addRelated( "location" );
dataQuery.setQueryOptions( queryOptions );
Backendless.Data.of( Address.class ).find( dataQuery, new AsyncCallback<BackendlessCollection<Address>>()
{
 @Override
 public void handleResponse( BackendlessCollection<Address> nycPeople )
 {
 Iterator<Address> iterator = nycPeople.getCurrentPage().iterator();
 while( iterator.hasNext() )
 {
 Address address = iterator.next();
 System.out.println( "City - " + address.city );
 System.out.println( "Location (Geo Point) - " + address.location );
 }
 }
 @Override
 public void handleFault( BackendlessFault backendlessFault )
 {
 System.out.println( "Server reported an error - " + backendlessFault.getMessage() );
 }
} );
Synchronous API sample (Plain Java only):
BackendlessDataQuery dataQuery = new BackendlessDataQuery();
dataQuery.setWhereClause( "location.city = 'NEW YORK CITY'" );
QueryOptions queryOptions = new QueryOptions();
queryOptions.addRelated( "location" );
dataQuery.setQueryOptions( queryOptions );
BackendlessCollection<Address> addresses = Backendless.Data.of( Address.class ).find( dataQuery );
Iterator<Address> iterator = addresses.getCurrentPage().iterator();
while( iterator.hasNext() )
{
 Address address = iterator.next();
 System.out.println( "City - " + address.city );
 System.out.println( "Location (Geo Point) - " + address.location );
}
The program output is:
City - New York
Location (Geo Point) - GeoPoint{objectId='2C2487C9-AAC5-1F57-FFFE-6BD608C79800', latitude=40.71427, longitude=-74.00597, categories=[geoservice_sample], metadata={city=NEW YORK CITY}, distance=null}

Review related articles:

Is article helpful?