Backendless Support
 

Retrieving geopoints for a category

This topic covers the issue of the API retrieved geopoints for a category. 

Similar to data objects, Backendless returns geopoints using paged data. 

Consider the following sample code:

                   

JAVA

Asynchronous API (Android and Plain Java):

private static void loadGeoPointsAsync()
{
 AsyncCallback<BackendlessCollection<GeoPoint>> getPointsCallback = new AsyncCallback<BackendlessCollection<GeoPoint>>()
 {
 int totalPoints = 0;
 int pointsLoaded = 0;
 @Override
 public void handleResponse( BackendlessCollection<GeoPoint> points )
 {
 totalPoints = points.getTotalObjects();
 if( pointsLoaded == 0 )
 pointsLoaded = points.getCurrentPage().size();
 System.out.println( "Total points in category " + points.getTotalObjects() );
 if( pointsLoaded < totalPoints )
 {
 Iterator<GeoPoint> iterator=points.getCurrentPage().iterator();
 while( iterator.hasNext() )
 {
 GeoPoint geoPoint=iterator.next();
 System.out.println( "GeoPoint: latitude - " + geoPoint.getLatitude() + ", longitude - " + geoPoint.getLongitude() );
 System.out.println( "\tmetadata: " );
 for( Map.Entry entry : geoPoint.getMetadata().entrySet() )
 System.out.println( "\t\t" + entry.getKey() + " - " + entry.getValue() );
 }
 points.nextPage( this );
 pointsLoaded += points.getCurrentPage().size();
 }
 }
 @Override
 public void handleFault( BackendlessFault backendlessFault )
 {
 }
 };
 BackendlessGeoQuery geoQuery = new BackendlessGeoQuery();
 geoQuery.addCategory( "geoservice_sample" );
 geoQuery.setIncludeMeta( true );
 Backendless.Geo.getPoints( geoQuery, getPointsCallback );
}
Synchronous API (plain Java only):
private static void loadGeoPointsSync()
{
 BackendlessGeoQuery geoQuery = new BackendlessGeoQuery();
 geoQuery.addCategory( "geoservice_sample" );
 geoQuery.setIncludeMeta( true );
 BackendlessCollection<GeoPoint> points = Backendless.Geo.getPoints( geoQuery );
 int totalPoints = points.getTotalObjects();
 int pointsLoaded = points.getCurrentPage().size();
 System.out.println( "Total points in category " + points.getTotalObjects() );
 while( pointsLoaded < totalPoints )
 {
 Iterator<GeoPoint> iterator=points.getCurrentPage().iterator();
 while( iterator.hasNext() )
 {
 GeoPoint geoPoint=iterator.next();
 System.out.println( "GeoPoint: latitude - " + geoPoint.getLatitude() + ", longitude - " + geoPoint.getLongitude() );
 System.out.println( "\tmetadata: " );
 for( Map.Entry entry : geoPoint.getMetadata().entrySet() )
 System.out.println( "\tt" + entry.getKey() + " - " + entry.getValue() );
 }
 points = points.nextPage();
 pointsLoaded += points.getCurrentPage().size();
 }
}

The API relies on creating a query object which identifies the category from which the geopoints should be loaded. Additionally, a flag is set requesting the server to return metadata for every geopoint (the metadata is not returned by default).

The code produces the following output (reduces for brevity since there are 332 points in the sample data set.
Total points in category 332
GeoPoint: latitude - -37.814, longitude - 144.96332
metadata: 
city - MELBOURNE
GeoPoint: latitude - -34.93333, longitude - 138.6
metadata: 
city - ADELAIDE
GeoPoint: latitude - -33.86785, longitude - 151.20732
metadata: 
city - SYDNEY
GeoPoint: latitude - -31.93333, longitude - 115.83333
metadata: 
city - PERTH
GeoPoint: latitude - -27.48333, longitude - 153.01667
metadata: 
city - SOUTH BRISBANE
....

Review related topic:

Is article helpful?