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  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

Synchronous cal

Backendless.data.withClass<BackendlessUser>().findFirst().then((user) {
  GeoPoint geoPoint = GeoPoint.fromLatLng(48.85, 2.35);
  Backendless.geo.savePoint(geoPoint).then((savedGeoPoint) {
    Backendless.data.withClass<BackendlessUser>().setRelation(user.getObjectId(), "location:GeoPoint:1", children: [savedGeoPoint.objectId]);
  });
});

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

Backendless.data.withClass<BackendlessUser>().findFirst().then((user) {
  GeoPoint geoPoint1 = GeoPoint.fromLatLng(48.85, 2.35);
  Backendless.geo.savePoint(geoPoint1).then((savedGeoPoint1) {
    Backendless.data.withClass<BackendlessUser>().addRelation(user.getObjectId(), "location:GeoPoint:n", children: [savedGeoPoint1.objectId]);
  });

  GeoPoint geoPoint2 = GeoPoint.fromLatLng(40.40, 3.68);
  Backendless.geo.savePoint(geoPoint2).then((savedGeoPoint2) {
    Backendless.data.withClass<BackendlessUser>().addRelation(user.getObjectId(), "location:GeoPoint:n", children: [savedGeoPoint2.objectId]);
  });
});

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.