Backendless Support
 

Saving a geopoint with API

The geopoints in a Backendless application would not be the ones from the sample data set - it is used only to make it easier to get started with Backendless GeoLocation. 

Adding geopoints to a developer's Backendless backend can be done either with the API or by using data import. 

This topic demonstrates the first approach - saving geopoints with API.

From the introduction to Backendless Geolocation it is obvious that a geopoint consists of the following:

  • A pair of GPS coordinates: latitude and longitude.
  • A category the geopoint belongs to.
  • Optional metadata which is a collection of arbitrary name/value pairs.

The sample code below saves two geopoints in your Backendless backend. One point which represents Dallas, TX on the map, is added using the synchronous API. The other one represents Houston, TX and it is added with the asynchronous API call. 

Notice that both geopoints are added to the "cities" category, which will be created dynamically on the server if it does not exist. There are also some metadata such as the city name, population size and a URL of an image of the city skyline:

                                         FOR Objective-C and Swift CLICK HERE

JAVA

Asynchronous call (Plain Java and Android):

private static void addGeoPointAsync()
{
 AsyncCallback<geopoint> addPointCallback = new AsyncCallback<geopoint>()
 {
 @Override
 public void handleResponse( GeoPoint savedGeoPoint )
 {
 System.out.println( "ASYNC: geo point saved. Object ID - " + savedGeoPoint.getObjectId() );
 }
 @Override
 public void handleFault( BackendlessFault backendlessFault )
 {
 System.out.println( "Server returned an error " + backendlessFault.getMessage() );
 }
 };
 GeoPoint houstonTX = new GeoPoint( 29.76429, -95.38370 );
 houstonTX.addCategory( "cities" );
 houstonTX.addMetadata( "city", "Houston" );
 houstonTX.addMetadata( "population", 2196000 );
 houstonTX.addMetadata( "photo", "<a href="<a href=" http:="" en.wikipedia.org="" wiki="" houston#mediaviewer="" file:uptown_houston.jpg""=""></a><a href="<a href=" http:="" en.wikipedia.org="" wiki="" houston#mediaviewer="" f...<="" a="">">http://en.wikipedia.org/wiki/Houston#mediaviewer/F...</a>"><a href="http://en.wikipedia.org/wiki/Houston#mediaviewer/F...">http://en.wikipedia.org/wiki/Houston#mediaviewer/F...</a> "=""><a href="<a href=" http:="" en.wikipedia.org="" wiki="" houston#mediaviewer="" f..."="">http://en.wikipedia.org/wiki/Houston#mediaviewer/F...</a>"><a href="http://en.wikipedia.org/wiki/Houston#mediaviewer/F...">http://en.wikipedia.org/wiki/Houston#mediaviewer/F...</a> );
 Backendless.Geo.savePoint( houstonTX, addPointCallback );
}</geopoint></geopoint>

Asynchronous call (Plain Java and Android):

private static void addGeoPointSync()
{
 GeoPoint dallasTX = new GeoPoint( 32.803468, -96.769879 );
 dallasTX.addCategory( "cities" );
 dallasTX.addMetadata( "city", "Dallas" );
 dallasTX.addMetadata( "population", 1258000 );
 dallasTX.addMetadata( "photo", "<a href="<a href=" http:="" en.wikipedia.org="" wiki="" dallas#mediaviewer="" file:xvixionx_29_april_2006_dallas_skyline.jpg""=""></a><a href="<a href=" http:="" en.wikipedia.org="" wiki="" dallas#mediaviewer="" fi...<="" a="">">http://en.wikipedia.org/wiki/Dallas#mediaviewer/Fi...</a>"><a href="http://en.wikipedia.org/wiki/Dallas#mediaviewer/Fi...">http://en.wikipedia.org/wiki/Dallas#mediaviewer/Fi...</a> "=""><a href="<a href=" http:="" en.wikipedia.org="" wiki="" dallas#mediaviewer="" fi..."="">http://en.wikipedia.org/wiki/Dallas#mediaviewer/Fi...</a>"><a href="http://en.wikipedia.org/wiki/Dallas#mediaviewer/Fi...">http://en.wikipedia.org/wiki/Dallas#mediaviewer/Fi...</a> );
 GeoPoint savedGeoPoint = Backendless.Geo.savePoint( dallasTX );
 System.out.println( "SYNC: geo point saved. Object ID - " + savedGeoPoint.getObjectId() );
}

After a developer runs the code above, it becomes possible to verify that the geopoints are added to the backend using Backendless console:

  1. Login to console, select the app and click the Geolocation icon.
  2. Click the cities category, the console should show the points as in the screenshot below: 

Review related topics:

Is article helpful?