Using getGps to integrate location tracking into an application

Overview

The getGPS API allows you to track your device’s location. The GPS returns your latitude and longitude which can be used to locate the phone on a map or return the nearest address from a mapping service.

Prerequisites

  1. Umajin App Creator downloaded and installed.
  2. Any text editor.
  3. Completed the registerAction tutorial.

Pro Tip: using Visual Studio Code as your editor will allow you to integrate debugging.

Instructions

Step 1) Create a new blank app in the Umajin App Creator to use as a testing platform for our new Feed Component.

Step 2) Create a new JavaScript document and name it something that relates to your desired feed. For our example, we will be extracting the latitude and longitude coordinates of our device’s location. Ours will be named “location.js”

Step 3) Now we need to prepare our Feed by registering an Action. This step is described in detail in our registerAction tutorial. The function that will be called to perform the Action is the first parameter and we will call it getLocation. The second will remain empty. The third, our label, will be Get Location, and this will require two Components, one Text Component for the latitude coordinate, and one Text Component for the longitude coordinate. It is important to note that these Components are initialized as global so that they can be referenced outside of the getLocation function.

Step 4) Now we can write our getLocation function. This will require only one line of code within the function heading. It will take our two Components, latitude and longitude as parameters. The getGps API takes in two parameters as well. The first is a function that will be called if getting the location is a success. The second will be called if there is an error in the process.

Step 5) When the location is successfully extracted, we want our page to update. This will happen with the use of our updateFunction.

Step 6) If the location is not successfully extracted, we need to alert the user with our errorFunction.

Step 7) Make sure that your JavaScript file is saved under in the scripts folder.

Open Umajin → File → Open Resources Folder → scripts

Step 8) Create a basic format to test your location.js. Ours contains four Text Components, two of which will be mapped to display our coordinates.