Backendless Support
 

Adding geopoints with related data objects

The data-to-geo relations are bidirectional. It means just like a data object may reference a geo point (or more than one) as a relation, a geo point may reference a data object or a collection of in its metadata. 

Consider the example below. 

The code saves a geo point which represents the Eiffel Tower. The geopoint references a data object which represents the tower's main engineer Gustavo Eiffel saved in the Architect table.

NOTICE: the "gustavoEiffel" object is a plain Java object (a POJO) which is saved in the Data Service storage in Backendless. The relation between the geo point and the data object is handled through the geopoint's metadata:

                                     FOR Objective-C, Swift and JavaScript implementations CLICK HERE

JAVA 

Synchronous API sample (Plain Java only):

Architect gustaveEiffel = new Architect();
gustaveEiffel.setName( "Gustave Eiffel" );
Calendar calendar = Calendar.getInstance();
calendar.set( 1923, 11, 27 );
gustaveEiffel.setBirthday( calendar.getTime() );
gustaveEiffel.setNationality( "French" );
GeoPoint eiffelTower = new GeoPoint( 48.85815, 2.29452 );
eiffelTower.addCategory( "towers" );
eiffelTower.addCategory( "placesToVisit" );
eiffelTower.addMetadata( "name", "Eiffel Tower" );
eiffelTower.addMetadata( "architect", gustaveEiffel );
GeoPoint savedPoint = Backendless.Geo.savePoint( eiffelTower );
System.out.println( "Geo point has been saved - " + savedPoint.getObjectId() );
Asynchronous API sample (Android and Plain Java):
Architect gustaveEiffel = new Architect();
gustaveEiffel.setName( "Gustave Eiffel" );
Calendar calendar = Calendar.getInstance();
calendar.set( 1923, 11, 27 );
gustaveEiffel.setBirthday( calendar.getTime() );
gustaveEiffel.setNationality( "French" );
GeoPoint eiffelTower = new GeoPoint( 48.85815, 2.29452 );
eiffelTower.addCategory( "towers" );
eiffelTower.addCategory( "placesToVisit" );
eiffelTower.addMetadata( "name", "Eiffel Tower" );
eiffelTower.addMetadata( "architect", gustaveEiffel );
Backendless.Geo.savePoint( eiffelTower, new AsyncCallback<GeoPoint>()
{
 @Override
 public void handleResponse( GeoPoint geoPoint )
 {
 System.out.println( "Geo point has been saved - " + geoPoint.getObjectId() );
 }
 @Override
 public void handleFault( BackendlessFault backendlessFault )
 {
 System.out.println( backendlessFault.getMessage() );
 }
} );

The Architect class:

package com.mbaas.sample;
import java.util.Date;
public class Architect
{
 private String nationality;
 private String name;
 private Date birthday;
 public void setName( String name )
 {
 this.name = name;
 }
 public String getName()
 {
 return name;
 }
 public void setBirthday( Date birthday )
 {
 this.birthday = birthday;
 }
 public Date getBirthday()
 {
 return birthday;
 }
 public void setNationality( String nationality )
 {
 this.nationality = nationality;
 }
 public String getNationality()
 {
 return nationality;
 }
}

Once a developer runs the code and open the Geolocation screen in Backendless, it will appear the following: 

If a developer clicks the Architect object link in the geopoint's metadata column, the console displays the following popup, which makes it very easy to see the related data object: 

Alternatively, if a developer navigates to the Data screen and select the Architect table, it will show the saved data object: 

Review related topic:

Is article helpful?