Backendless Support
 

Searching for geopoints in a rectangular area

Within the features of GeoService Backendless provides various options for search of geopoints. Alongside with the search for geopoints in a radius, Backendless provides the API for searching for geopoints in a rectangular area. 

This API is very similar to the one for search in radius, except the app must define the "view port" or the rectangular area on the map where the search must occur. 

Consider the following example below.

                                          For Objective-C and Swift implementations CLICK HERE

Asynchronous sample (Android and Plain Java):
BackendlessGeoQuery geoQuery = new BackendlessGeoQuery();
geoQuery.setSearchRectangle( new double[]{49.615, -152.944, 25.648, -21.636} );
geoQuery.addCategory( "geoservice_sample" );
geoQuery.setIncludeMeta( true );
Backendless.Geo.getPoints( geoQuery, new AsyncCallback<BackendlessCollection<GeoPoint>>()
{
 @Override
 public void handleResponse( BackendlessCollection<GeoPoint> geoPointCollection )
 {
 Iterator<GeoPoint> iterator = geoPointCollection.getCurrentPage().iterator();
 while( iterator.hasNext() )
 {
 GeoPoint geoPoint = iterator.next();
 System.out.println( "GeoPoint - " + geoPoint );
 }
 }
 @Override
 public void handleFault( BackendlessFault backendlessFault )
 {
 System.out.println( "Server reported an error - " + backendlessFault.getMessage() );
 }
} );
Synchronous sample (Plain Java only):
BackendlessGeoQuery geoQuery = new BackendlessGeoQuery();
geoQuery.setSearchRectangle( new double[]{49.615, -152.944, 25.648, -21.636} );
geoQuery.addCategory( "geoservice_sample" );
geoQuery.setIncludeMeta( true );
BackendlessCollection<GeoPoint> geoPointCollection = Backendless.Geo.getPoints( geoQuery );
Iterator<GeoPoint> iterator = geoPointCollection.getCurrentPage().iterator();
while( iterator.hasNext() )
{
 GeoPoint geoPoint = iterator.next();
 System.out.println( "GeoPoint - " + geoPoint );
}
NOTICE: the the code above sets the search rectangle as two pairs of coordinates. The first two numbers in the setSearchRectangle method are the latitude and longitude coordinates of the North-West corner of the search rectangle. The other two numbers are the coordinates of the South-East corner.

Backendless console makes it easy to identify the coordinates of the search rectangle and also provides a way to run rectangle-based search for diagnostics purposes. 

In order to run a rectangle-based search in console it is necessary to follow the next steps:

  1. Login to Backendless console, select an app and click the Geolocation icon.
  2. Select the geo category for which it is necessary to accomplish the search.
  3. Click the Map-driven navigation toggle located under the map.
  4. As a developer pans and zooms on the map, the Console runs rectangle-based geopoint searches and displays the returned geopoints on the map. Also, the map displays the coordinates of the North-West and South-East corners to make it easier for developers to replicate the same behavior in an API call.

 Review related topic:

Is article helpful?