public
Last active

Github jobs api example. Showing it on maps.

  • Download Gist
core.js
JavaScript
1 2 3 4 5 6 7
Object.size = function(obj) {
var size = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) size++;
}
return size;
};
index.html
HTML
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
<!DOCTYPE html>
<html>
<head>
<title>Simple Map</title>
<meta charset="utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<style>
html, body, #map-canvas {
margin: 0;
padding: 0;
height: 100%;
}
</style>
<script src="https://maps.googleapis.com/maps/api/js?v=3.exp&sensor=false"></script>
</head>
<body>
<div id="map-canvas">Initializing the map</div>
 
<script type="text/javascript" src="http://code.jquery.com/jquery-1.10.2.min.js"></script>
<script type="text/javascript" src="http://underscorejs.org/underscore-min.js"></script>
<script type="text/javascript" src="core.js"></script>
<script type="text/javascript" src="map.js"></script>
<script type="text/javascript" src="jobs.js"></script>
</body>
</html>
jobs.js
JavaScript
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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122
$(function(){
 
var github_jobs = 'http://jobs.github.com/positions.json',
locations = [],
i,
states = {},
url,
each_callback;
 
var getCollection = function (url){
var collection = [],
after_finish = arguments[1] ? arguments[1] : function(){ };
 
$.ajax({
url: url,
dataType: 'jsonp',
success: function(res){
var i;
for (i = res.length - 1; i >= 0; i--) {
locations.push(res[i]);
}
after_finish(url);
}
});
 
return collection;
};
 
var after_all_callback = function () {
// All requests are finished.
var i, n = locations.length, qAddresses = {};
 
var callRate = 10; // in miliseconds
 
for (i = 0; i < n; i++) {
var address = locations[i].location,
keys = _.keys(qAddresses);
 
var condition = _.some(keys, function(key) {
var reg_for_address = new RegExp(address, 'ig'),
reg_for_key = new RegExp(key, 'ig');
 
return key.search(reg_for_address) != -1 || address.search(reg_for_key) != -1;
})
 
if(qAddresses[address] || condition){
if(qAddresses[address]){
qAddresses[address] += 1;
}
else {
var _address = address;
_.each(keys, function(key) {
var reg_for_address = new RegExp(address, 'ig'),
reg_for_key = new RegExp(key, 'ig');
 
if(key.search(reg_for_address) != -1 || address.search(reg_for_key) != -1){
_address = key;
}
})
qAddresses[_address] += 1;
}
}
else {
qAddresses[address] = 1;
}
 
 
 
};
 
//console.log(Object.size(qAddresses));
//console.log(qAddresses);
 
var qAddress, delay = 1000;
for(qAddress in qAddresses){
if(qAddress.toLowerCase() == 'anywhere' || qAddress.toLowerCase() == 'remote'){
var marker = new google.maps.Marker({
map: map,
position: new google.maps.LatLng(-80,0),
draggable: false,
animation: google.maps.Animation.DROP
});
delete qAddresses['anywhere'];
delete qAddresses['remote'];
}
else {
setTimeout(
(function(s){
return function() {
geocoding(s);
}
})(qAddress), delay);
delay += 1000;
}
}
 
 
};
 
 
for (i = 4; i > 0; i--) {
url = [github_jobs, (i == 1) ? '' : '?page='+i].join('');
states[url] = {done: false};
 
each_callback = function(url){
states[url].done = true;
 
for (var state in states) {
if( !states[state].done ){
return;
}
}
 
setTimeout(function(){
after_all_callback(); // run ones
}, 100);
};
 
locations.concat( getCollection(url, each_callback) );
}
 
});
map.js
JavaScript
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
var geocoder, map;
 
function initialize() {
geocoder = new google.maps.Geocoder();
 
var mapOptions = {
zoom: 2,
center: new google.maps.LatLng(0,0),
mapTypeId: google.maps.MapTypeId.ROADMAP
};
map = new google.maps.Map(document.getElementById('map-canvas'),
mapOptions);
}
 
var geocoding = function(address){
geocoder.geocode( { 'address': address}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
var marker = new google.maps.Marker({
map: map,
position: results[0].geometry.location,
draggable: false,
animation: google.maps.Animation.DROP
});
}
else {
console.log('Not found with status ', status);
}
});
}
 
google.maps.event.addDomListener(window, 'load', initialize);

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.