ChildOfCode


Code, Maker, Robotic, Open Source. Knowledge Bases


Titanium Studio Using the Geolocation

Getting Location Information

There are two ways to get location information:

1)Make a one-time request with getCurrentPosition.

2)Register to receive location updates by adding an event listener for the location event.

Get the Geolocation with event listener

Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_BEST;  
Ti.Geolocation.preferredProvider = Ti.Geolocation.PROVIDER_GPS;  
Ti.Geolocation.distanceFilter = 10;


var locationCallback = function(e){  
    if(!e.success || e.error){
       Ti.API.info("error"+JSON.stringify(e.error));
    }else{      
        Ti.API.info("Longitude: " + e.coords.longitude);
        Ti.API.info("Latitude: " + e.coords.latitude);
        Ti.API.info("Accuracy: " + e.coords.accuracy);
        Ti.API.info("Time: " + e.coords.timestamp);
    }
};

//Start button click event - to get current location 
this.onStartButton_Click = function()  
{
    var thisClass = this;
    Ti.Geolocation.addEventListener('location', locationCallback);
};

//SStop button click event - to stop getting current location 
this.onStopButton_Click = function()  
{
    var thisClass = this;
    Ti.Geolocation.removeEventListener('location', locationCallback);
};

Get the Geolocation with getCurrentPosition

Method one

Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_BEST;

Ti.Geolocation.getCurrentPosition(function(e)  
{          
    if(e.success) {
        Ti.API.info("Latitude : " + e.coords.latitude);
        Ti.API.info("Longitude: " + e.coords.longitude);
        Ti.API.info("Accuracy: " + e.coords.accuracy);
        Ti.API.info("Time: " + e.coords.timestamp);
    }else{
        Ti.API.info("error: "+e.error);
    }            
});

Method two

switch(thisClass.Parent.UIViewClass.AccuracyPickerType){  
                case 'ACCURACY_HIGH':
                accuracy = Ti.Geolocation.ACCURACY_HIGH;
                break;

                case 'ACCURACY_BEST':
                accuracy = Ti.Geolocation.ACCURACY_BEST;
                break;

                case 'ACCURACY_LOW':
                accuracy = Ti.Geolocation.ACCURACY_LOW;
                break;
    }

    Ti.Geolocation.accuracy = accuracy;

        thisClass.ReportPosition = function(e) 
        {
            //Ti.API.info('ReportPosition');
            if (!e.success || e.error) {
                alert('error ' + JSON.stringify(e.error));
                return;
            }

            if(e.success) {
                thisClass.GeolocationLongitude = e.coords.longitude;
                thisClass.GeolocationLatitude = e.coords.latitude;
                thisClass.GeolocationAccuracy = e.coords.accuracy;
            }
        };


        Ti.Geolocation.preferredProvider = Ti.Geolocation.PROVIDER_GPS;

        Ti.Geolocation.addEventListener('location', thisClass.ReportPosition);
        Titanium.Geolocation.getCurrentPosition(thisClass.ReportPosition);
        Ti.Geolocation.removeEventListener('location', thisClass.ReportPosition);

Setup in tiapp.xml

tiapp.xml  
=============
<key>NSLocationWhenInUseUsageDescription</key>  
    <string>
        Specify the reason for accessing the user's location information.
        This appears in the alert dialog when asking the user for permission to
        access their location.
    </string>
<key>NSLocationAlwaysUsageDescription</key>  
    <string>
    Specify the reason for accessing the user's location information.
    This appears in the alert dialog when asking the user for permission to
    access their location.
    </string>