Backendless Support
 

Searching for geopoints in a radius

The profound multiple functionality of Backendless GeoService provides various APIs for accomplishing the search process. Alongside the other options, a developer obtains the API for searching geo points in a circular shaped area, or in radius. Searching for geopoints in radius must be confined to a specific geo category. The API will return geopoints located within specified distance (radius) from a central point, all the points will belong to the specified category.

Consider the sample code below.

                                       For Objective-C and Swift implementations CLICK HERE

 Asynchronous sample (Android and Plain Java):

BackendlessGeoQuery geoQuery = new BackendlessGeoQuery();
geoQuery.setLatitude( 32.555 );
geoQuery.setLongitude( -97.667 );
geoQuery.addCategory( "geoservice_sample" );
geoQuery.setIncludeMeta( true );
geoQuery.setRadius( 270d );
geoQuery.setUnits( Units.MILES );
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.setLatitude( 32.555 );
geoQuery.setLongitude( -97.667 );
geoQuery.addCategory( "geoservice_sample" );
geoQuery.setIncludeMeta( true );
geoQuery.setRadius( 270d );
geoQuery.setUnits( Units.MILES );
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 );
}
The program output is:
GeoPoint - GeoPoint{objectId='6CB5FECA-B2C9-3002-FFC4-17D8D069A400', latitude=32.73569, longitude=-97.10807, categories=[geoservice_sample], metadata={city=ARLINGTON}, distance=34.87}
GeoPoint - GeoPoint{objectId='D60EFC78-673D-0DA8-FFD7-53C8D8BF8D00', latitude=32.78306, longitude=-96.80667, categories=[geoservice_sample], metadata={city=DALLAS}, distance=52.519}
GeoPoint - GeoPoint{objectId='AD1C546F-DD65-C4B7-FFA6-57ACD06B6C00', latitude=33.15067, longitude=-96.82361, categories=[geoservice_sample], metadata={city=FRISCO}, distance=64.025}
GeoPoint - GeoPoint{objectId='1905CA4C-1C4B-1156-FF2A-C505999DA300', latitude=33.01984, longitude=-96.69889, categories=[geoservice_sample], metadata={city=PLANO}, distance=64.83}
GeoPoint - GeoPoint{objectId='C594A668-B9A2-4D24-FF5A-D4F700DECB00', latitude=30.26715, longitude=-97.74306, categories=[geoservice_sample], metadata={city=AUSTIN}, distance=158.313}
GeoPoint - GeoPoint{objectId='3729180E-E50A-674C-FFF2-CB27C4003C00', latitude=35.46756, longitude=-97.51643, categories=[geoservice_sample], metadata={city=OKLAHOMA CITY}, distance=201.644}
GeoPoint - GeoPoint{objectId='00CF08C4-6C6D-8563-FF94-5E7FFD298B00', latitude=29.42412, longitude=-98.49363, categories=[geoservice_sample], metadata={city=SAN ANTONIO}, distance=222.036}
GeoPoint - GeoPoint{objectId='8C946475-BA0A-CB8B-FF46-8D6E628E2F00', latitude=29.76328, longitude=-95.36327, categories=[geoservice_sample], metadata={city=HOUSTON}, distance=236.376}

The code above works with the geoservice_sample category. This is a category created when a developer request Backendless to install sample geodata. While developing this sample the Backendless Console has been used to verify the code results. A developer can perform searches in radius using the console which visualizes the geopoints located in the circular area it is intended to define. Below is a screenshot from accomplished sample testing:

To run searches in radius in console a developer should act as the following:

  1. Login to Backendless console, select an app and click the Geolocation icon.
  2. Select a geo category where it is desirable to perform a search.
  3. Click the Map-driven navigation toggle located under the map.
  4. Click the Search in radius toggle.
  5. The map displays a circle which can be resized or moved - use the points on the circumference and the central one to resize and move. As a developer move/resize the circle, Backendless returns the geopoints located within the area and the console renders them on the map. The console also provides information about the selected radius and the coordinates of the central point.

Review related topics:

Is article helpful?