Back to blog home page

Handling Geolocation on the Backend

Posted by on Jan 26, 2016

We’re pleased to announce a new feature in Backand: geolocation support in the database. Backand now lets you save locations in the database with latitude/longitude (Geography Points) and the ability to perform advanced filters and queries on it. For example, you can filter all the rows that are within a certain distance from a particular location (e.g., the location of the device).

Geography Points

Backand lets you store geography points as fields as part of any object. A geography point is an array of latitude and longitude that represents a location on a map. With a Backand geography query you can easily find distance between points or objects that match a distance to a specific location.

Add a Point

To add new point field, open Backand Model, select add field with point type. To POST new data point for latitude 10 and longitude 20 just send the following array:

Query Point

To query a distance within a geography point you can use any of the following commands:

To get all the restaurants within 25 Miles of the San Francisco Marina District [37.8019859, -122.4414805]:

The above noSql is translated into MySQL syntax using the new ST_Distance() function:

Query Point with Parameters

In order to query get geography points dynamically, use Query with parameters:

  1. Add this Input Parameters: lan, lon, dist
  2. In the Query use tokens (“{{lan}}”, “{{lon}}” and “{{dist}}”) to represent the input parameters:

{ “object”: “restaurants”, “q”: { “location” : { “$withinMiles” : [[“{{lan}}”, “{{lon}}”], “{{dist}}”] } } }

Filter Point

Using a Backand NoSQL filter you can filter object based on a distance to a specific point using the $within command.

Filter all the restaurants within 25 Miles of the San Francisco Marina District [37.8019859, -122.4414805]:

Try it now: login to your Backand account.