Skip to content

Instantly share code, notes, and snippets.

@Mindelusions

Mindelusions/app.js

Created Oct 18, 2011
Embed
What would you like to do?
Sample Titanium App Built Using Sensis Developer API
// Include a small sensis helper library
var mind = {};
Ti.include('sensis.js');
// Custom activity indicator
var indWin = null;
var actInd = null;
function showIndicator()
{
if (Ti.Platform.osname != 'android')
{
// window container
indWin = Titanium.UI.createWindow({
height:150,
width:150
});
// black view
var indView = Titanium.UI.createView({
height:150,
width:150,
backgroundColor:'#000',
borderRadius:10,
opacity:0.8
});
indWin.add(indView);
}
// loading indicator
actInd = Titanium.UI.createActivityIndicator({
style:Titanium.UI.iPhone.ActivityIndicatorStyle.BIG,
height:30,
width:30
});
if (Ti.Platform.osname != 'android')
{
indWin.add(actInd);
// message
var message = Titanium.UI.createLabel({
text:'Loading',
color:'#fff',
width:'auto',
height:'auto',
font:{fontSize:20,fontWeight:'bold'},
bottom:20
});
indWin.add(message);
indWin.open();
} else {
actInd.message = "Loading";
}
actInd.show();
}
function hideIndicator()
{
actInd.hide();
if (Ti.Platform.osname != 'android') {
indWin.close({opacity:0,duration:500});
}
}
// create tab group
var tabGroup = Titanium.UI.createTabGroup();
//
// create search tab, window, and form.
//
var win1 = Titanium.UI.createWindow({
title:'Enter Search & Location',
backgroundColor:'#fff',
layout:'vertical'
});
var tab1 = Titanium.UI.createTab({
icon:'KS_nav_views.png',
title:'Search',
window:win1
});
var queryField = Ti.UI.createTextField({
width:250,
height:35,
hintText:'Search Query',
top:20,
borderColor:'#333',
borderRadius:10
});
win1.add(queryField);
var locField = Ti.UI.createTextField({
width:250,
height:35,
hintText: 'Location',
top:20,
borderColor:'#333',
borderRadius:10
});
win1.add(locField);
var searchBtn = Ti.UI.createButton({
width:250,
height:40,
title:'Search',
top:20
});
searchBtn.addEventListener('click', function() {
showIndicator();
mind.sensis.search({query:queryField.value,loc:locField.value});
});
win1.add(searchBtn);
// Results tab and window.
var win2 = Titanium.UI.createWindow({
title:'No Search Query',
backgroundColor:'#fff'
});
var tab2 = Titanium.UI.createTab({
icon:'KS_nav_ui.png',
title:'Results',
window:win2
});
// Create search results table.
var resTable = Ti.UI.createTableView({
data:[{title:'No Results'}]
});
resTable.addEventListener('click', function(e) {
Ti.API.info(JSON.stringify(e.rowData.info));
var place = e.rowData.info;
var placeWin = Ti.UI.createWindow({
modal:true,
title:place.name,
backgroundColor:'#fff'
});
var closeBtn = Ti.UI.createButton({
title:'Close'
});
closeBtn.addEventListener('click', function() {
placeWin.close();
});
placeWin.leftNavButton = closeBtn;
// Using Google static maps for simplicity
var mapImage = Ti.UI.createImageView({
image:'http://maps.googleapis.com/maps/api/staticmap?size=320x200&maptype=roadmap&sensor=false&markers='+place.primaryAddress.latitude+','+place.primaryAddress.longitude,
width:320,
height:200,
top:0
});
placeWin.add(mapImage);
var callBtn = Ti.UI.createButton({
title:'Call This Location',
width:200,
height:45,
top:210
});
callBtn.addEventListener('click', function() {
Ti.Platform.openURL('tel:'+place.primaryContacts[0].value);
});
placeWin.add(callBtn);
placeWin.open();
});
win2.add(resTable);
//
// add tabs
//
tabGroup.addTab(tab1);
tabGroup.addTab(tab2);
// open tab group
tabGroup.open();
// Listen for the sensis_complete event to know when the search has
// returned results
Ti.App.addEventListener('sensis_complete', function(e) {
var res = JSON.parse(e.resp),
row, item, data = [];
Ti.API.info('Search returned '+res.count.toString()+' results');
win2.title = e.query + ' in ' + e.loc;
for (var i = 0; i < res.results.length; i++) {
item = res.results[i];
row = Ti.UI.createTableViewRow({
title: item.name,
hasChild:true,
info: item
});
data.push(row);
}
resTable.setData(data);
tabGroup.setActiveTab(1);
hideIndicator();
});
// Listen for a search error so that it can be handled properly.
Ti.App.addEventListener('sensis_error', function(e) {
alert('Error: ' + e.err);
hideIndicator();
});
(function() {
mind.sensis = {
endpoint: 'http://api.sensis.com.au/ob-20110511/test/search',
apikey: 'API_KEY_HERE',
query: 'cafe',
loc: 'melbourne'
};
var client = Ti.Network.createHTTPClient();
// build query from stored parameters
mind.sensis.buildQuery = function() {
return mind.sensis.endpoint + '?key=' + mind.sensis.apikey + '&query=' + mind.sensis.query + '&location=' + mind.sensis.loc;
};
// Store and execute search with parameters: 'query' and 'loc'
mind.sensis.search = function(_args) {
_args = _args || {};
mind.sensis.query = _args.query ? _args.query : mind.sensis.query;
mind.sensis.loc = _args.loc ? _args.loc : mind.sensis.loc;
client.timeout = 10000;
client.onload = function() {
Ti.API.info(this.responseText);
Ti.App.fireEvent('sensis_complete', {
resp:this.responseText,
query: mind.sensis.query,
loc: mind.sensis.loc
});
};
client.onerror = function() {
Ti.App.fireEvent('sensis_error',{err:this.responseText});
};
Ti.API.info('query: '+mind.sensis.buildQuery());
client.open('GET', mind.sensis.buildQuery());
client.send();
};
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment