Skip to content

User to Geo Relations

Backendless Geo Service manages application's geo location data and provides APIs to work with geopoints. Backendless supports integration between user objects managed by User Service and geopoints for the scenarios when a logical connection between the two entity types must exist in an application.

Relations between a user object and a geopoint are built the same way as between a data object and a geo point. The User-to-Geo integration is implemented through object relations. The Users table schema may declare a table column with a special data type - "GeoPoint Relationship". As a result, the user objects in the table may contain a reference to one or more geopoints. Backendless provides an API to establish a relationship between a user object and geopoints. Additionally, when a user object is retrieved by using the data retrieval API, any related geopoints can be retrieved by using the same mechanisms used for loading data relations. The user-to-geo relation is bidirectional, it means a geopoint may reference a user object in its metadata. You can learn more about it in the Relations with Data Objects section of the Geolocation service documentation.

A relation between a user object and a geopoint (or a collection of) can be established by using either the "code first" or the "schema first" approaches. With the former, the relationship is established through an API call. If the relationship is not declared in the schema, Backendless creates it based on the information in the API. With the "schema first" approach, application developer can declare a user-to-geo property first. In either one of these approaches, once a relationship is declared, user objects and geopoints may be linked together by using the Backendless console as well. For information on how to declare Users to Geo relation columns and how to link User objects with GeoPoints using Backendless console, see the Relations with Geo Objects of the Data Service documentation.

Important

To create a relation both the user object and geopoint(s) must already exist in the Backendless database. It is not possible to create a relation with an object which has not been previously saved.

Creating User-to-Geo Relations with the API

Creating a relationship between a user object and a geopoint (or a collection of) uses the same API as setting/adding related objects to a data object. In the case of the user-to-geo relations, the related entity is a geopoint or a collection of geopoints.

The example below demonstrates how to assign coordinates to a user's location, that is link a user object with one or several geopoints. A user object is retrieved for the example purposes (to have an object the example will be working with).

One-to-One Relation

Blocking API

BackendlessUser user = Backendless.Data.of( BackendlessUser.class ).findFirst();
GeoPoint geoPoint = new GeoPoint( 48.85, 2.35 );
user.setProperty( "location", geoPoint );
Backendless.Data.of( BackendlessUser.class ).save( user );

Non-Blocking API

final AsyncCallback<BackendlessUser> saveUserCallback = new AsyncCallback<BackendlessUser>()
{
  @Override
  public void handleResponse( BackendlessUser user )
  {
    // user object has been updated with geopoint
  }

  @Override
  public void handleFault( BackendlessFault fault )
  {
  }
};

AsyncCallback<BackendlessUser> findUserCallback = new AsyncCallback<BackendlessUser>()
{
  @Override
  public void handleResponse( BackendlessUser user )
  {
    GeoPoint geoPoint = new GeoPoint( 48.85, 2.35 );
    user.setProperty( "location", geoPoint );
    Backendless.Data.of( BackendlessUser.class ).save( user, saveUserCallback );
  }

  @Override
  public void handleFault( BackendlessFault fault )
  {
  }
};

Backendless.Data.of( BackendlessUser.class ).findFirst( findUserCallback );

Backendless Console can be used to verify the new relation. The geopoint will show up as a property in the location column of the user object.

One-to-Many Relation

Blocking API

GeoPoint geoPoint1 = new GeoPoint( 48.85, 2.35 );
GeoPoint geoPoint2 = new GeoPoint( 40.40, 3.68 );
List<GeoPoint> locations = new ArrayList<GeoPoint>();
locations.add( geoPoint1 );
locations.add( geoPoint2 );
user.setProperty( "locations", locations );
Backendless.Data.of( BackendlessUser.class ).save( user );

Non-Blocking API

final AsyncCallback<BackendlessUser> saveUserCallback = new AsyncCallback<BackendlessUser>()
{
  @Override
  public void handleResponse( BackendlessUser user )
  {
    // user object has been updated with geopoint
  }

  @Override
  public void handleFault( BackendlessFault fault )
  {

  }
};

AsyncCallback<BackendlessUser> findUserCallback = new AsyncCallback<BackendlessUser>()
{
  @Override
  public void handleResponse( BackendlessUser user )
  {
    GeoPoint geoPoint1 = new GeoPoint( 48.85, 2.35 );
    GeoPoint geoPoint2 = new GeoPoint( 40.40, 3.68 );
    List<GeoPoint> locations = new ArrayList<GeoPoint>();
    locations.add( geoPoint1 );
    locations.add( geoPoint2 );
    user.setProperty( "locations", locations );
    Backendless.Data.of( BackendlessUser.class ).save( user, saveUserCallback );
  }

  @Override
  public void handleFault( BackendlessFault fault )
  {
  }
};

Backendless.Data.of( BackendlessUser.class ).findFirst( findUserCallback );

Backendless Console can be used to verify the new relation. The geopoint will show up as a property in the locations column of the user object.