iOS
...
Parse Swift SDK
Data Objects

Geoqueries

9min

Geoqueries

Introduction

We use the term Geoqueries to refer to the type of queries where their conditions involve ParseGeoPoint type fields. It is recommended to use the ParseGeoPoint struct to store geographic location data in a Back4App Database. The ParseSwift SDK provides a set of methods that allows us to query data according to conditions applied on ParseGeoPointdata type.

Prerequisites

To complete this tutorial, you will need:

Goal

To understand how to query data using conditions on geographic location data.

1 - Quick review about the Query<U> class

Any query performed on a Back4App Database is done via the generic class Query<U>. The generic parameter U (conforming to the ParseObject protocol) is the data type of the objects we are trying to retrieve from the database.

Given a data type like MyObject, we retrieve these objects from a Back4App Database in the following way

Swift


You can read more about the Query<U> class here at the official documentation.

2 - Save some data on Back4App

Before we begin to execute queries, we should store some sample data on a Back4App Database. By following the Quickstart guide, you can configure and link your sample iOS App to your Back4App database. For this guide, we will store information about cities. We use the following struct to organize a city’s information:

Swift


Now, we proceed to store the sample data. This step can be implemented using Swift or directly from your app’s console on the Back4App platform.

Swift
Back4App's console


3 - Query the data

Sorting the results

With the sample data saved, we can start performing different query types.

For our first example, we will select all the cities and sort them depending on how far they are from a reference geopoint. We implement this query by passing a constraint to the Query<City> object. The method near(key:geoPoint:) available via the ParseSwift SDK allows us to construct such constraint. As arguments, we pass the field’s name (usually referred to as key) containing the reference geoPoint.

Swift


Selecting results within a given region

Suppose we want to select cities within a certain region. We can achieve this with a constraint created by the method withinKilometers(key:geoPoint:distance:). As arguments, we pass the field’s name containing the city’s location, the region’s center (a ParseGeoPoint data type) and the maximum distance (in km) a city can be from this region’s center. To select all cities that are at most 3000km away from Kingston - Jamaica, we can do it in the following way

Swift


Additionally, when the distance is given in miles instead of kilomenters, we can use the withinMiles(key:geoPoint:distance:sorted:) method.

A less common method, withinRadians(key:geoPoint:distance:sorted:), is also available if the distance is given in radians. Its use is very similar to the previous methods.

Selecting results within a given polygon

In the previous example, we selected cities within a region represented by a circular region. In case we require to have a non-circular shape for the region, the ParseSwift SDK does allow us to construct such regions from their vertices.

Now, the goal for this example is to select cities within a five vertex polygon. These vertices are expressed using the ParseGeoPoint struct. Once we have created the vertices, we instantiate a ParsePolygon. This polygon is then passed to the withinPolygon(key:polygon:) method (provided by the ParseSwift SDK) to construct the constraint that will allow us to select cities within this polygon.

Swift


Conclusion

Nowadays doing operations on location data to offer custom services is very important. Back4App together with the ParseSwift SDK makes it easy to implement those kinds of operations.