Where am I? A geolcation/gps project

If you want to create a location based app you need to know the GPS position of the user. In order to get them we can use the geolocation services of your phone. Since this is sensible data we need to ask the user for permission.

You add those permission into tiapp xml and request them at runtime using Ti APIs.

XML setup

Add these parts to the tiapp.xml

iOS block:

            <string>[Why do you need the location?]</string>
            <string>[Why do you need the location?]</string>

Android block:

<android xmlns:android="http://schemas.android.com/apk/res/android">
        <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
        <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

Modules block:

    <module platform="android">ti.playservices</module>

Check for location permission at runtime:

function getGeoPermission(clb) {
    var hasLocationPermissions = Ti.Geolocation.hasLocationPermissions(Ti.Geolocation.AUTHORIZATION_WHEN_IN_USE);
    Ti.API.info("Already got the permission? " + hasLocationPermissions);

    if (hasLocationPermissions) {
        Ti.API.info("has permission");
    Ti.Geolocation.requestLocationPermissions(Ti.Geolocation.AUTHORIZATION_WHEN_IN_USE, function(e) {
        if (e.success) {
            // yes
        } else if (OS_ANDROID) {
            // no
        } else {
            // no

    // callback function - inside here we will have GPS permissions

Now the app is able to use the GPS functions and we can add a listener to get the location.

Location event

Especially on Android it can a bit until you have a GPS fix and get the position. Also if you want to track the user while he is moving you will need a eventlistener. The location event will fire when the location is available or changes:

// add this into the getGeoPermission() callback:

Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_HIGH;
if (Ti.Geolocation.locationServicesEnabled) {
    if (OS_ANDROID) {
        // android
    } else {
        // ios
        Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_BEST;
        Ti.Geolocation.distanceFilter = 2;
        Ti.Geolocation.preferredProvider = Ti.Geolocation.PROVIDER_GPS;
        Ti.Geolocation.pauseLocationUpdateAutomatically = true;
        Ti.Geolocation.activityType = Ti.Geolocation.ACTIVITYTYPE_FITNESS;
    // add location listener
    Ti.Geolocation.addEventListener('location', onLocation);

    // try to get the current location (might be an old chacked one or empty)

function onLocation(returnObject){
    // will fire when the location updates
    Ti.API.info("location: " + JSON.stringify(returnObject));

The returnObject of the location event contains a coords node with latitude and longitude and other useful information like a timestamp or the accuracy

var errorCode = returnObject.error;
var lat = returnObject.coords.latitude;
var lon = returnObject.coords.longitude;

Heading event

The Ti.Gelocation features another useful event for geo based apps: heading. With this you will get the direction your phone is heading to.

Ti.Geolocation.addEventListener('heading', onHeading);

function onHeading(returnObject){


More information and properties can be found in the official documentation.