Skip to content

Instantly share code, notes, and snippets.

@mauropm
Created December 20, 2013 23:52
Show Gist options
  • Save mauropm/8063518 to your computer and use it in GitHub Desktop.
Save mauropm/8063518 to your computer and use it in GitHub Desktop.
This code emulates an app that will send it's geolocation once. You can easily modified it to make it send the user's location every time the app opens or the app resumes.
// We create global variables that will
// save the latest geolocation
myGeolocation = {};
myGeolocation.lon = 0;
myGeolocation.lat = 0;
// This is the call function of the getCurrentPosition
// Note that you are storing the current lat and long
// so you can use it anywhere else in the code.
// You can even throw another event within this function
// to warn other parts of the program that will need to update
// the coordinates or sync with the new geolocated ads.
function updateGeolocation(e){
myGeolocation.lat = e.coords.latitude;
myGeolocation.lon = e.coords.longitude;
label.text = "Current location: lat " + myGeolocation.lat + "\n lon " + myGeolocation.lon;
alert(myGeolocation.lat + " " + myGeolocation.lon);
}
// This is your app event listener. You can call it from the
// app events, like "resumed/resume" so you can update the location
// when the user reopens the app.
Ti.App.addEventListener('getCurrentPosition',function(){
Ti.Geolocation.getCurrentPosition(updateGeolocation);
});
//You can use the resume event for updating the current
// location - think when the user reopens the app after a
// flight
Ti.App.addEventListener('resume',function(){
Ti.App.fireEvent('getCurrentPosition');
});
// a base window for our ui
var win = Ti.UI.createWindow({
backgroundColor:'white',
});
// a button to force the event getCurrentPosition
var button = Ti.UI.createButton({
title:'Press for getCurrentPosition',
top:20,
left:20,
});
// the label that will be used to update the current lat,lon
var label = Ti.UI.createLabel({
text:"Current location: lat " + myGeolocation.lat + ", lon " + myGeolocation.lon,
top:40,
left:20,
});
// We add the listener to the button so we start
// the getCurrentPosition event.
button.addEventListener('click',function(){
Ti.App.fireEvent('getCurrentPosition');
});
win.add(label);
win.add(button);
win.open();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment