Create an iOS Swift app to send your location to a Rails API and display on Google Maps

In this post I'll share a proof of concept project I worked on to create a Swift iOS application to track a user's location and send the data back to a Rails API. I thought it would be interesting to tinker with a lot of geolocation data and display it back to the user. For this post I decided to simply display the data on a Google Map using the directions API.

Now that the backend is ready to receive the location payloads, we'll move onto the iOS code. I created a new project in Xcode: iOS Single View application, product name: "Location Tracker", Language: Swift, Devices: Universal.

In the application's Capabilities section, I enabled Location updates in the background modes. In the Info section, under Custom iOS Target Properties, section: Required background modes, "App registers for location updates" should be included. In addition, I added keys "Privacy - Location Always Usage Description" and "Privacy - Location When In Use Usage Description" with the values "Location is required."

I dumped all the location services code in the ViewController.swift file:

At this point, I plugged in my iPhone 6 and built/ran the app for my device. After accepting the request to run location services, it opened to a white screen and started sending my location to the Rails API.

Part 3: Google Maps integration

For sake of generating a neat screenshot for this post, and hiding my personal GPS coordinates, I decided to seed some data. Edit file: db/seeds.rb