Geo Point Swift Code

I’ve been reading about and learning the Geolocation features at Backendless. As a learning exercise I rewrote a most of the Objective-C code to Swift Code. I’m not saying it’s perfect but it may help someone looking for a starting points. I hope it helps someone out.

class GeolocationViewController : UIViewController

{

    // Backendless Property

    let backendless = Backendless.sharedInstance()

 

    func backendlessGeolocation()

    {

        let myString = "BackendlessGeolocation"

        let responder = Responder(responder: self, selResponseHandler: #selector(success), selErrorHandler: #selector(error))

        

        // +++++++++++++++++++++++++++++++++++ Adding a Geo Category +++++++++++++++++++++++++++++++++++

        

        // Synchronous

        self.backendless.geoService.addCategory(myString)

        

        // Asynchronous

        self.backendless.geoService.addCategory(myString, responder: responder)

        

        self.backendless.geoService.addCategory(myString, response: { (result : GeoCategory!) -> () in

            

            print("The category was added: \(result)")

            }, error: { (fault : Fault!) -> () in

         

                print("There was an error adding the category: \(fault)")

        })

        

        // +++++++++++++++++++++++++++++++++++ Deleting a Geo Category +++++++++++++++++++++++++++++++++++




        // Synchronous

        Types.tryblock( { () -> Void in

            

            self.backendless.geoService.deleteCategory(myString)

            

            print("The Geo Category was removed from Backendless: \(myString)")

            }, catchblock: { (fault) -> () in

                

                let error = fault as! Fault

                print("There was an error removing the Geo Categories: \(error)")

        })




        // Asynchronous

        self.backendless.geoService.deleteCategory(myString, responder: responder)

        

        self.backendless.geoService.deleteCategory(myString, response: { (result : AnyObject!) -> () in

            

            print("The category was deleted: \(result)")

            }, error: { (fault : Fault!) -> () in

         

                print("There was an error deleting the category: \(fault)")

        })

        

        // +++++++++++++++++++++++++++++++++++ Retrieving Geo Categories +++++++++++++++++++++++++++++++++++




        // Synchronous

        Types.tryblock( { () -> Void in

            

            let categoryArray = self.backendless.geoService.getCategories()

            

            for category in categoryArray

            {

                print("The category is: \(category)")

            }

            }, catchblock: { (fault) -> () in

                

                let error = fault as! Fault

                print("There was an error retrieving the Geo Categories: \(error)")

        })

        

        // Asynchronous

        self.backendless.geoService.getCategories(responder)

        

        self.backendless.geoService.getCategories({ (result : [String]!) -> () in

            

            for category in result

            {

                print("The category is: \(category)")

            }

            }, error: { (fault : Fault!) -> () in

                

                print("There was an error retrieving the categories: \(fault)")

        })

        

        // +++++++++++++++++++++++++++++++++++ Adding a Geo Point +++++++++++++++++++++++++++++++++++

        

        let geoPointData = GEO_POINT(latitude: 32.81, longitude: -96.80)

        let geoPoint = GeoPoint(point: geoPointData)

        

        // Synchronous

        Types.tryblock( { () -> Void in

            

            self.backendless.geoService.savePoint(geoPoint)

            print("The GeoPoint was added to Backendless: \(geoPoint)")

            }, catchblock: { (fault) -> () in

                

                let error = fault as! Fault

                print("There was an error deleting the GeoPoint: \(error)")

        })




        // Asynchronous

        self.backendless.geoService.savePoint(geoPoint, responder: responder)

        

        self.backendless.geoService.savePoint(geoPoint, response: { (result : GeoPoint!) -> () in

            

            print("The GeoPoint was added to Backendless: \(result)")

            }, error: { (fault : Fault!) -> () in

                

                print("There was an error adding the GeoPoint to Backendless \(fault)")

        })

        

        // +++++++++++++++++++++++++++++++++++ Updating a Geo Point +++++++++++++++++++++++++++++++++++




        let geoPointObjectId = geoPoint.objectId

        

        // +++++++++++++++++++++++++++++++++++ Deleting a Geo Point +++++++++++++++++++++++++++++++++++




        // Synchronous

        Types.tryblock( { () -> Void in

            

            self.backendless.geoService.removePoint(geoPoint)

            print("The GeoPoint was removed from Backendless: \(geoPoint.objectId)")

            }, catchblock: { (fault) -> () in

                

                let error = fault as! Fault

                print("There was an error deleting the GeoPoint: \(error)")

        })

        

        // Asynchronous

        self.backendless.geoService.removePoint(geoPoint, responder: responder)

        

        self.backendless.geoService.removePoint(geoPoint, response: { (result : AnyObject!) -> () in

            

            print("The GeoPoint was removed: \(result)")

            }, error: { (fault : Fault!) -> () in

                

                print("There was an error deleting the GeoPoint: \(fault)")

        })

        

        // +++++++++++++++++++++++++++++++++++ Search in a Category +++++++++++++++++++++++++++++++++++




        // Synchronous

        Types.tryblock( { () -> Void in

            

            let query = BackendlessGeoQuery(categories: ["Restaurants"])

            self.backendless.geoService.getPoints(query)

            

            }, catchblock: { (fault) -> () in

                

                let error = fault as! Fault

                print("There was an error searching for a Category: \(error)")

        })

        

        // Asynchronous

        let query = BackendlessGeoQuery(categories: ["Restaurants"])

        

        self.backendless.geoService.getPoints(query, responder: responder)

        

        self.backendless.geoService.getPoints(query, response: { (result : BackendlessCollection!) -> () in

            

            print("Here are the Geo Categories: \(result)")

            }, error: { (fault : Fault!) -> () in

                

                print("There was an error retrieving the Geo Cetegories: \(fault)")

        })




        // +++++++++++++++++++++++++++++++++++ Search in Radius +++++++++++++++++++++++++++++++++++

   

        let radiusQuery = BackendlessGeoQuery(point: geoPointData, radius: 10000, units: METERS)

        

        // Synchronous

        Types.tryblock( { () -> Void in

            

            let query = BackendlessGeoQuery(categories: ["Restaurants"])

            self.backendless.geoService.getPoints(query)

            

            }, catchblock: { (fault) -> () in

                

                let error = fault as! Fault

                print("There was an error searching for a Category: \(error)")

        })




        // Asynchronous

        self.backendless.geoService.getPoints(radiusQuery, responder: responder)

        

        self.backendless.geoService.getPoints(radiusQuery, response: { (result : BackendlessCollection!) -> () in

            

            print("Here are the Geo Points in the defined radius: \(result)")

            }, error: { (fult : Fault!) -> () in

                

                print("There was an error getting the Geo Points in the defined radius.")

        })

    }

    

    func success()

    {

        print("Success!")

    }

    

    func error()

    {

        print("Error!")

    }

}



Not to sound dumb, but how would you use this? Would the code instantly work or do you have to use a bridging header? I’m building an application in swift that needs to use GeoLocation, and want to know if there are any prerequisites for this code to work. Thanks!

The reason I added this was because the docs did not have the Swift version of this code. If you are using a Swift project then you will not need a bridging header file for this code. If you have an Objective-C project then I would recommend using the Objective-C code provided in the docs. I hope this helps.