Created
July 10, 2012 13:23
-
-
Save boydlee/3083201 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// this sets the background color of the master UIView (when there are no windows/tab groups on it) | |
Titanium.UI.setBackgroundColor('#000'); | |
// | |
// create base UI tab and root window | |
// | |
var win1 = Titanium.UI.createWindow({ | |
title:'Tab 1', | |
backgroundColor:'#000' | |
}); | |
var map_stops = Titanium.Map.createView({ | |
userLocation: true, | |
regionFit: true, | |
height: 320, | |
height: 326, | |
top: 89 | |
}); | |
function setMarkersWithCenter(latiarray,longiarray){ | |
//latitute and longitude arrays should be equal | |
if(latiarray.length != longiarray.length){ | |
return; | |
} | |
var total_locations = latiarray.length; | |
var minLong = null, minLat = null, maxLong = null, maxLat = null; | |
var totalLongi = 0.0, totalLati = 0.0; | |
for(var i = 0; i < total_locations; i++) { | |
if(minLat == null || minLat > latiarray[i]) { | |
minLat = latiarray[i]; | |
} | |
if(minLong == null || minLong > longiarray[i]) { | |
minLong = longiarray[i]; | |
} | |
if(maxLat == null || maxLat < latiarray[i]) { | |
maxLat = latiarray[i]; | |
} | |
if(maxLong == null || maxLong < longiarray[i]) { | |
maxLong = longiarray[i]; | |
} | |
} | |
Titanium.API.info(maxLat); | |
Titanium.API.info(minLat); | |
Titanium.API.info(maxLong); | |
Titanium.API.info(minLong); | |
var ltDiff = maxLat-minLat; | |
var lgDiff = maxLong-minLong; | |
Titanium.API.info(ltDiff); | |
Titanium.API.info(lgDiff); | |
var delta = ltDiff>lgDiff ? ltDiff : lgDiff; | |
if(total_locations>0 && delta>0) { | |
Titanium.API.info(((parseFloat(maxLat) + parseFloat(minLat))/2) + ', ' + ((parseFloat(maxLong) +parseFloat(minLong))/2) + " (delta: " + delta + ")"); | |
map_stops.setLocation({ | |
animate : true, | |
latitude:((parseFloat(maxLat) + parseFloat(minLat))/2) , | |
longitude: ((parseFloat(maxLong) +parseFloat(minLong))/2), | |
latitudeDelta:delta, | |
longitudeDelta:delta, | |
}); | |
} | |
} | |
function SearchStopsNearby(lat, lng) { | |
data_stops = []; | |
data_pins = []; | |
/* erase previous pins */ | |
map_stops.removeAllAnnotations(); | |
/* send payload to table and map */ | |
map_stops.setRegion({ | |
latitude: lat, | |
longitude: lng, | |
latitudeDelta: 0.002, | |
longitudeDelta: 0.002 | |
}); | |
/* load new data from bus live api */ | |
var url = 'http://api.busliveapp.com/stops/nearest?lat=' + lat + '&lng=' + lng; | |
var client = Ti.Network.createHTTPClient({ | |
onload: function(e) { | |
Ti.API.info("Received text: " + this.responseText); | |
/* convert json to data */ | |
var stop_data = JSON.parse(this.responseText); | |
var latiarray = []; | |
var longiarray = []; | |
/* loop through stops and create data */ | |
for (var c = 0; c < stop_data.length; c++) { | |
Ti.API.info(stop_data[c].name); | |
/* add pins to map */ | |
latiarray.push(stop_data[c].lat); | |
longiarray.push(stop_data[c].lng); | |
var marker = Titanium.Map.createAnnotation({ | |
latitude: stop_data[c].lat, | |
longitude: stop_data[c].lng, | |
pincolor: Titanium.Map.ANNOTATION_RED, | |
animate: true, | |
title: stop_data[c].name, | |
subtitle: 'towards ' + stop_data[c].heading, | |
stopCode: stop_data[c].code, | |
rightButton: Titanium.UI.iPhone.SystemButton.DISCLOSURE | |
}); | |
if (stop_data[c].friendly_code == '') { | |
/* create a stop code disc */ | |
var pin_stop_code = Titanium.UI.createView({ | |
backgroundImage: 'images/bg_stop_code.png', | |
left: 8, | |
top: 7, | |
width: 18, | |
height: 19 | |
}); | |
var pin_stop_code_label = Ti.UI.createLabel({ | |
text: stop_data[c].friendly_code, | |
textAlign: Ti.UI.TEXT_ALIGNMENT_CENTER, | |
color: '#FFF', | |
shadowColor: 'rgba(0,0,0,0.5)', | |
shadowOffset: {x:1,y:1}, | |
width: 18, | |
height: 18, | |
left: 0, | |
top: 0, | |
font: {fontSize : 9, fontWeight : 'bold', fontFamily : 'P22 Johnston Underground'} | |
}); | |
pin_stop_code.add(pin_stop_code_label); | |
/* add stop code to annotation */ | |
marker.LeftView = pin_stop_code; | |
}; | |
data_pins.push(marker); | |
} | |
map_stops.addAnnotations(data_pins); | |
/* set region to fit pins */ | |
setTimeout(function(e){ | |
setMarkersWithCenter(latiarray,longiarray); | |
}, 500); | |
/* add stops to table list */ | |
table_stops.setData(data_stops); | |
}, | |
onerror: function(e) { | |
alert('Network error: Cannot update stops list'); | |
}, | |
timeout: 100000 | |
}); | |
client.open("GET", url); | |
client.send(); | |
} | |
win1.add(map_stops); | |
SearchStopsNearby(51.500152, -0.126236); | |
// open tab group | |
win1.open(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment