Skip to content

Instantly share code, notes, and snippets.

@ecito
Created August 3, 2010 07:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ecito/506000 to your computer and use it in GitHub Desktop.
Save ecito/506000 to your computer and use it in GitHub Desktop.
// create tab group
var tabGroup = Titanium.UI.createTabGroup();
/////////////////////////
// create base UI tab and root window
// We need a tabBar to be able to win.setToolbar items, even though we'll hide it
/////////////////////////
var win = Titanium.UI.createWindow({
title:'TweetFlow',
backgroundColor:'#fff',
tabBarHidden: true
});
var tab1 = Titanium.UI.createTab({
icon:'KS_nav_views.png',
title:'Map',
window:win
});
var mapview = Titanium.Map.createView({
mapType: Titanium.Map.STANDARD_TYPE,
region:{latitude:33.74511, longitude:-84.38993, latitudeDelta:15, longitudeDelta:15},
animate:true,
regionFit:true,
userLocation:false
});
win.add(mapview);
var connectButton = Titanium.UI.createButton({
title:'Connect',
width:75,
style:Titanium.UI.iPhone.SystemButtonStyle.BORDERED
});
var closeButton = Titanium.UI.createButton({
title:'Close',
width:75,
style:Titanium.UI.iPhone.SystemButtonStyle.BORDERED
});
var clearButton = Titanium.UI.createButton({
title:'Clear',
width:75,
style:Titanium.UI.iPhone.SystemButtonStyle.BORDERED
});
var flexSpace = Titanium.UI.createButton({
systemButton:Titanium.UI.iPhone.SystemButton.FLEXIBLE_SPACE
});
var fixedSpace = Titanium.UI.createButton({
systemButton:Titanium.UI.iPhone.SystemButton.FIXED_SPACE,
width:50
});
win.setToolbar([flexSpace,connectButton,flexSpace,closeButton,flexSpace,clearButton,flexSpace]);
/////////////////////////
// Button event listeners
/////////////////////////
mapview.addEventListener('click', function(e) {
if (e.annotation) {
e.annotation.leftView.image = "http://img.tweetimag.es/i/" + e.title + '_n.png';
}
});
connectButton.addEventListener('click', function() {
try {
socket.connect();
Titanium.API.info('Opened!');
} catch (e) {
Titanium.API.info('Exception: '+e);
}
});
closeButton.addEventListener('click', function() {
try {
socket.close();
} catch (e) {
Titanium.API.info('Exception: '+e);
}
});
clearButton.addEventListener('click', function() {
mapview.removeAllAnnotations();
});
tabGroup.addTab(tab1);
tabGroup.open();
/////////////////////////
// Socket event listener
/////////////////////////
var socket = Titanium.Network.createTCPSocket({
hostName:'localhost',
port:6969,
mode:Titanium.Network.READ_MODE
});
socket.addEventListener('read', function(e) {
//Titanium.API.info(e['from'] + ':' + e['data'].text);
try {
var tweet = JSON.parse(e.data.text);
if (tweet.geo && tweet.user.screen_name && tweet.text) {
//Titanium.API.info(tweet.geo);
var profileImageView = Titanium.UI.createImageView({
width:32,
height:32
});
var tweetAnnotation = Titanium.Map.createAnnotation({
latitude: parseFloat(tweet.geo.coordinates[0]),
longitude: parseFloat(tweet.geo.coordinates[1]),
title: tweet.user.screen_name,
subtitle: tweet.text,
pincolor: Titanium.Map.ANNOTATION_RED,
leftView: profileImageView,
animate:true
});
mapview.addAnnotation(tweetAnnotation);
}
} catch (exception) {
Titanium.API.info('Exception: '+ exception);
}
});
tabGroup.addEventListener('close', function(e) {
if (socket.isValid) {
socket.close();
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment