-
Notifications
You must be signed in to change notification settings - Fork 0
/
geolocation.js
85 lines (73 loc) · 2.69 KB
/
geolocation.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
var coordinatesString = 'geocode=';
var latestId = '';
var latestUpdate = '';
var range = '';
var measurement = '';
$('document').ready(function(){
if(navigator.geolocation){
setupCoordinatesStringAndCallTwitter();
setInterval("callTwitter()", 30000);
} else {
$('#twitterContainer').append('<p>Sorry! The geolocation API is not supported by your browser..no fun for you!</p>');
}
});
setupCoordinatesStringAndCallTwitter = function(){
navigator.geolocation.getCurrentPosition(function(position){
setCoordinatesString(position);
callTwitter();
});
}
setCoordinatesString = function(position){
coordinatesString += position.coords.latitude + '%2c' + position.coords.longitude + '%2c';
}
callTwitter = function(){
var stringVal = addRangeAndMeasurement();
var twitterUrl = 'http://search.twitter.com/search.json?'+ stringVal +'&callback=?';
$.getJSON(twitterUrl, null, function(tweets){
var results = tweets.results;
if(latestId == ''){
updateLatestVariables(results[0]);
$.each(results, function(){
$('#twitterContainer').append(writeTextBlock(this));
})
} else {
var currentTopElement = $('#twitterContainer > :first-child');
$.each(results, function(){
if(this.id != latestId && this.created_at > latestUpdate){
currentTopElement.before(writeTextBlock(this));
var element = currentTopElement.prev();
element.css("backgroundColor", "rgb(255,255,204)" );
highlight(element);
}
})
updateLatestVariables(results[0]); }
});
}
addRangeAndMeasurement = function(){
range = $('#range').val();
measurement = $("input[@name='measurement']:checked").val();
range = range || '50';
measurement = measurement || 'km';
return coordinatesString + range + measurement;
}
highlight = function(elementToUpdate){
var backgroundColorString = elementToUpdate.css("backgroundColor");
var blueValue = getBlueValue(backgroundColorString);
if(blueValue < 255){
blueValue += 1;
updateString = "rgb(255,255," + blueValue + ")";
elementToUpdate.css("backgroundColor", updateString);
setTimeout(function(){highlight(elementToUpdate)}, 75);
}
}
getBlueValue = function(backgroundColor){
var blue =jQuery.trim(backgroundColor.split(',')[2]);
return new Number(blue.substring(0,3));
}
writeTextBlock = function(tweet){
return '<div class="textBlock roundedBox whiteBackground"><img src="' + tweet.profile_image_url + '" class="image" /><div class="from"><a href="http://twitter.com/' + tweet.from_user + '">' + tweet.from_user + '</a></div><div class="created">' + tweet.created_at + '</div><div class="text">' + tweet.text + '</div></div>'
}
updateLatestVariables = function(latestTweet){
latestId = latestTweet.id;
latestUpdate = latestTweet.created_at;
}