Skip to content

Instantly share code, notes, and snippets.

@MotiurRahman
Last active December 30, 2015 03:19
Show Gist options
  • Save MotiurRahman/7768547 to your computer and use it in GitHub Desktop.
Save MotiurRahman/7768547 to your computer and use it in GitHub Desktop.
Android and IOS: User Locations tracking apps (last 100 location)
/* Here if you enable your location service of your set and internet connection it takes the locatio from GPS and save in a
file and it will be shown on a tableView row*/
var location = require('ui/common/UserLocation');
var userLocation = new location();
userLocation.open();
exports.addValue = function(place) {
var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'file.txt');
f.write(place + '\n');
};
exports.add = function(place) {
var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'file.txt');
f.write(place + '\n', true);
};
exports.readData = function() {
var f = Ti.Filesystem.getFile(Ti.Filesystem.applicationDataDirectory, 'file.txt');
var contents = f.read();
Ti.API.info('Output as a blob: ' + contents);
// useful if contents are binary
Ti.API.info('Output text of the file: ' + contents.text);
return contents.text;
};
// This is the main view where show your current location. In a tableView row.
function geoLoction() {
var win = Titanium.UI.createWindow({
title : 'User Location',
backgroundColor : '#000',
navBarHidden : false,
layout : 'vertical'
});
// Create a Label.
var userLocation = Ti.UI.createLabel({
text : 'user Location',
color : '#FFF',
font : {
fontSize : 20
},
height : Ti.UI.SIZE,
width : Ti.UI.SIZE,
top : 10,
});
// Add to the parent view.
win.add(userLocation);
var reverseGeoLabel = Titanium.UI.createLabel({
text : 'Your Current Location',
font : {
fontSize : 15,
fontWeight : 'bold'
},
color : '#fff',
top : 10,
left : 10,
height : Ti.UI.SIZE,
width : Ti.UI.SIZE
});
win.add(reverseGeoLabel);
var reverseGeo = Titanium.UI.createLabel({
text : '',
font : {
fontSize : 12
},
color : '#fff',
top : 10,
left : 10,
height : Ti.UI.SIZE,
width : Ti.UI.SIZE
});
win.add(reverseGeo);
var tableView = Ti.UI.createTableView({
top : 10
});
// Add to the parent view.
win.add(tableView);
var dataArray = function() {
var data = [];
var File = require('lib/file');
var locationData = File.readData().split('\n');
Ti.API.info('locationDataLength=' + locationData.length);
var sum = 0;
for (var i = locationData.length - 2; i > 0; i--) {
data.push(locationData[i]);
sum++;
if (sum == 100) {
break;
}
};
return data;
};
var locationData = dataArray();
if (locationData.length > 0) {
for (var i = 0; i < locationData.length; i++) {
tableView.appendRow({
title : i+'.'+locationData[i],
backgroundColor : 'transparent',
color : "red"
});
};
}
setInterval(function() {
updateLocation();
}, 7000);
updateLocation();
function updateLocation() {
if (Ti.Geolocation.locationServicesEnabled) {
Ti.Geolocation.purpose = 'Get Current Location';
Ti.Geolocation.accuracy = Ti.Geolocation.ACCURACY_BEST;
Ti.Geolocation.distanceFilter = 10;
Ti.Geolocation.preferredProvider = Ti.Geolocation.PROVIDER_GPS;
Titanium.Geolocation.purpose = 'Get Current Location';
Titanium.Geolocation.addEventListener('location', function(e) {
if (e.error) {
Ti.API.error('Error: ' + e.error);
} else {
var longitude = e.coords.longitude;
var latitude = e.coords.latitude;
Ti.Geolocation.reverseGeocoder(latitude, longitude, function(e) {
var places = e.places;
//reverseGeo.text = places[0].address;
var place = places[0].address;
reverseGeo.setText(place);
var File = require('lib/file');
if (File.readData().length > 0) {
File.add(place);
} else {
File.addValue(place);
}
});
}
});
} else {
alert('Please enable location services');
}
}
return win;
}
module.exports = geoLoction;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment