Skip to content

Instantly share code, notes, and snippets.

@jonalter
Created June 24, 2011 16:36
Show Gist options
  • Select an option

  • Save jonalter/1045163 to your computer and use it in GitHub Desktop.

Select an option

Save jonalter/1045163 to your computer and use it in GitHub Desktop.
Geo Analytics Example
Titanium.UI.setBackgroundColor('#fff');
Ti.Geolocation.preferredProvider = "gps";
Ti.Geolocation.accuracy = Titanium.Geolocation.ACCURACY_BEST;
Ti.Geolocation.distanceFilter = 0;
Ti.Geolocation.purpose = "GPS demo";
// create tab group
var win = Ti.UI.createWindow({
layout: 'vertical'
});
var label = Ti.UI.createLabel({
text: 'location unknown',
height: 80,
width: 300,
top: 20,
color: 'black'
});
var geoButton = Ti.UI.createButton({
title: "Get location",
height: 50,
width: 250,
top: 20
});
geoButton.addEventListener('click', function(e){
Titanium.Geolocation.getCurrentPosition(function(e){
if (!e.success || e.error)
{
alert('error:' + JSON.stringify(e.error) );
return;
}
// alert('Current Position lat:'+e.coords.latitude+' long: '+e.coords.longitude);
locationCallback(e);
});
});
var locationCallback = function(e){
var lng = e.coords.longitude;
var lat = e.coords.latitude;
label.text = 'lat: '+lat+' lng: '+lng;
label.color = 'red';
setTimeout(function(){
label.color = 'black';
},100);
Ti.Analytics.featureEvent('location.change');
};
var geoAddListenerButton = Ti.UI.createButton({
title: "Add Geo Listener",
height: 50,
width: 250,
top: 20
});
geoButton.addEventListener('click', function(e){
// Should have a getLocation immediately before addEventListener for location.
// I left this out to demonstrate how it works.
// Simply click "Get location" right before "Add Geo Listener".
Titanium.Geolocation.addEventListener('location', locationCallback);
});
var geoRemoveListenerButton = Ti.UI.createButton({
title: "Remove Geo Listener",
height: 50,
width: 250,
top: 20
});
geoRemoveListenerButton.addEventListener('click', function(e){
Titanium.Geolocation.removeEventListener('location', locationCallback);
});
var testOneButton = Ti.UI.createButton({
title: "Test One",
height: 50,
width: 250,
top: 20
});
testOneButton.addEventListener('click', function(e){
Ti.Analytics.featureEvent('button.one');
});
var testTwoButton = Ti.UI.createButton({
title: "Test Two",
height: 50,
width: 250,
top: 20
});
testTwoButton.addEventListener('click', function(e){
Ti.Analytics.featureEvent('button.two');
});
win.add(label);
win.add(geoButton);
win.add(geoAddListenerButton);
win.add(geoRemoveListenerButton);
win.add(testOneButton);
win.add(testTwoButton);
win.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment