Created
June 16, 2014 18:05
-
-
Save korymath/1b94fe86e2df78746985 to your computer and use it in GitHub Desktop.
beerFinder javascript
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
beerfinder = { | |
map: null, | |
markers: [], | |
windows: [], | |
masterBeerList:[], | |
defaults: { | |
lat: 51.045925999999990000, | |
long: -114.057959999999980000, | |
style: | |
[ | |
{ | |
"featureType": "landscape", | |
"stylers": [ | |
{ "color": "#e3be39" } | |
] | |
},{ | |
"featureType": "poi", | |
"stylers": [ | |
{ "visibility": "off" } | |
] | |
},{ | |
"featureType": "water", | |
"stylers": [ | |
{ "color": "#9fb6d1" }, | |
{ "saturation": -100 }, | |
{ "lightness": 6 } | |
] | |
},{ | |
} | |
] | |
// [{"featureType":"water","stylers":[{"color":"#46bcec"},{"visibility":"on"}]},{"featureType":"landscape","stylers":[{"color":"#f2f2f2"}]},{"featureType":"road","stylers":[{"saturation":-100},{"lightness":45}]},{"featureType":"road.highway","stylers":[{"visibility":"simplified"}]},{"featureType":"road.arterial","elementType":"labels.icon","stylers":[{"visibility":"off"}]},{"featureType":"administrative","elementType":"labels.text.fill","stylers":[{"color":"#444444"}]},{"featureType":"transit","stylers":[{"visibility":"off"}]},{"featureType":"poi","stylers":[{"visibility":"off"}]}] | |
}, | |
venueFilterList : $('#venue-types'), | |
beerFilterList : $('#beer-list'), | |
showCitySelector:false, | |
init: function( data, beer_list ) { | |
masterBeerList = beer_list; | |
var mapOptions = { | |
zoom: 14, | |
center: new google.maps.LatLng(beerfinder.defaults.lat, beerfinder.defaults.long), | |
panControl: false, | |
zoomControl: true, | |
zoomControlOptions: { | |
style: google.maps.ZoomControlStyle.LARGE, | |
position: google.maps.ControlPosition.LEFT_CENTER | |
}, | |
mapTypeControl: false, | |
scaleControl: false, | |
streetViewControl: false, | |
overviewMapControl: false, | |
mapTypeId: google.maps.MapTypeId.ROADMAP, | |
styles: beerfinder.defaults.style | |
}; | |
beerfinder.map = new google.maps.Map(document.getElementById("map-draw"), mapOptions); | |
// Try and stay within the bounds of Alberta | |
beerfinder.allowedBounds = new google.maps.LatLngBounds( | |
new google.maps.LatLng(49.0, -120.0409), | |
new google.maps.LatLng(60, -110.00) | |
); | |
// HTML5 geolocation | |
if(navigator.geolocation) { | |
navigator.geolocation.getCurrentPosition(function(position) { | |
var pos = new google.maps.LatLng(position.coords.latitude, | |
position.coords.longitude); | |
if(beerfinder.allowedBounds.contains(pos)) | |
{ | |
beerfinder.map.setCenter(pos); | |
beerfinder.showCitySelector = false; | |
} | |
else{ | |
beerfinder.showCitySelector = true; | |
} | |
}, function() { | |
handleNoGeolocation(true); | |
}); | |
} else { | |
// Browser doesn't support Geolocation | |
handleNoGeolocation(false); | |
beerfinder.showCitySelector = true; | |
} | |
function handleNoGeolocation(errorFlag) {} | |
beerfinder.initMapData( data ); | |
$('#where').keyup(function(e){ | |
var code = e.keyCode ? e.keyCode : e.which; | |
if(13==code){$('#go-where').trigger('click');} | |
}); | |
$('#go-where').click(function(e){ | |
var where = $('#where').val(); | |
if(_gas){ | |
_gas.push(['_trackEvent', 'BeerFinder', 'Search', where]); | |
} | |
codeAddress(where); | |
function codeAddress(address) { | |
var geocoder = new google.maps.Geocoder(); | |
geocoder.geocode({ | |
'address': address, | |
'region': 'ca', | |
'bounds' : beerfinder.allowedBounds | |
}, function(results, status) { | |
if (status == google.maps.GeocoderStatus.OK && beerfinder.allowedBounds.contains(results[0].geometry.location)) { | |
beerfinder.map.setCenter(results[0].geometry.location); | |
} | |
else | |
{ | |
alert("Could not understand that location, or it isn't within Alberta"); | |
} | |
}); | |
} | |
}); | |
beerfinder.beerFilterList.find('a').not('.all').click( function(e){ | |
e.preventDefault(); | |
var link = $(this); | |
link.toggleClass("active"); | |
if(_gas){ | |
var label = link.hasClass('active') ? "BeerFilterOn" : 'BeerFilterOff' | |
_gas.push(['_trackEvent', 'BeerFinder', label, link.text()]); | |
} | |
if(beerfinder.beerFilterList.find('a.active').not('.all').length == 0) | |
{ | |
beerfinder.beerFilterList.find('a.all').addClass('active'); | |
} | |
else | |
{ | |
beerfinder.beerFilterList.find('a.all').removeClass('active'); | |
} | |
beerfinder.filterMarkers(); | |
}); | |
beerfinder.beerFilterList.find('a.all').click( function(e){ | |
e.preventDefault(); | |
beerfinder.beerFilterList.find('a').removeClass('active'); | |
var link = $(this); | |
link.addClass("active"); | |
beerfinder.filterMarkers(); | |
}); | |
beerfinder.venueFilterList.find('a').click( function(e){ | |
e.preventDefault(); | |
var link = $(this); | |
link.toggleClass("active"); | |
if(_gas){ | |
var label = link.hasClass('active') ? "VenueFilterOn" : 'VenueFilterOff' | |
_gas.push(['_trackEvent', 'BeerFinder', label, link.text()]); | |
} | |
beerfinder.filterMarkers(); | |
}); | |
// hash default check | |
if( window.location.hash ){ | |
var hash = window.location.hash.replace('#',''); | |
beerfinder.beerFilterList.find('a.beer-'+ hash).trigger('click'); | |
} else { | |
beerfinder.filterMarkers(); | |
} | |
}, | |
initMapData: function( data ){ | |
$.each(data.nodes, function(index, n) { | |
beerfinder.addMarker(n.node); | |
}); | |
}, | |
addMarker: function(node) { | |
var latlng = new google.maps.LatLng(parseFloat(node.lat), parseFloat(node.lng)); | |
var markerImage = '/themes/wildrose/img/map-marker-tap.png'; | |
if(node.loctype == "Store"){ | |
markerImage = '/themes/wildrose/img/map-marker-retail.png'; | |
} else if(node.loctype == "WildRose"){ | |
markerImage = '/themes/wildrose/img/map-marker-wildrose.png'; | |
} | |
var marker = new google.maps.Marker({ | |
position: latlng, | |
title: node.placename, | |
icon: markerImage, | |
map: beerfinder.map | |
}); | |
marker.loctype = node.loctype; | |
marker.beers = []; | |
var beers = node.beers.split(','); | |
beerfinder.markers.push(marker); | |
var mapAddress = encodeURI(node.address); | |
var mapLink = "http://maps.google.ca/?q=" + mapAddress; | |
var streetviewLink = 'http://maps.googleapis.com/maps/api/streetview?sensor=false&size=600x400&location=' + encodeURIComponent(node.address); | |
var c = '<div class="beer-window"><h3>' + node.placename + '</h3><p>' + node.address + '</br>'; | |
c += '<a class="directions" href="' + mapLink + '" target="_blank">Get Directions</a> '; | |
c += '<a class="streetview" href="' + streetviewLink + '" target="_blank">View On Streetview</a>'; | |
c += '</p>'; | |
if(node.weburl){ | |
var url = node.weburl; | |
if(url.indexOf('http') != 0) | |
{ | |
url = "http://" + url; | |
} | |
c += '<p class="weburl">' + '<a href="' + url + '" target="_blank">' + node.weburl + '</a></p>'; | |
} | |
if(node.phone){ | |
c += '<p class="phone">' + '<a href="tel:' + node.phone + '" >' + node.phone + '</a></p>'; | |
} | |
c += '<div class="beers-here"><dl><dt>Beers available here:</dt>'; | |
$.each(beers, function(i,beer){ | |
marker.beers.push(beer); | |
beerName = ''; | |
var beerInfo = null; | |
for(var i = 0; i < masterBeerList.beers.length; i++) | |
{ | |
if(masterBeerList.beers[i].beer.ID == beer) | |
{ | |
beerInfo = masterBeerList.beers[i].beer; | |
break; | |
} | |
} | |
c += '<dd>'; | |
if(beerInfo.icon) | |
{ | |
c+= '<img src="' + beerInfo.icon + '" alt="' + beerInfo.name + '" />'; | |
} | |
c += beerInfo.name + '</dd>'; | |
}); | |
c += '</dl></div></div>'; | |
var win = new google.maps.InfoWindow({ | |
content: c | |
}); | |
// map events | |
google.maps.event.addListener(marker, 'click', function() { | |
$.each(beerfinder.windows, function(index, win) { | |
win.close(); | |
}); | |
win.open(beerfinder.map,marker); | |
}); | |
beerfinder.windows.push(win); | |
}, | |
filterMarkers: function() { | |
//arrays used to hold valid filter values. If the array is empty, we will skip filtering on it | |
var validVenues = []; | |
var validBeers = []; | |
var types = beerfinder.venueFilterList.find("a.active"); | |
if(types.length == 0) { | |
validVenues.push('') | |
} else { | |
if(types.length != beerfinder.venueFilterList.find('a').length) | |
{ | |
$.each(types, function(index, type) { | |
var href=$(this).attr('href'); | |
var type = href.substr(href.lastIndexOf('-')+1); | |
validVenues.push(type); | |
}); | |
} | |
} | |
var beers = beerfinder.beerFilterList.find("a.active"); | |
if(beers.length != beerfinder.beerFilterList.find('a').length-1 && !beerfinder.beerFilterList.find('a.all').hasClass('active')) | |
{ | |
$.each(beers, function(index, type) { | |
var href=$(this).attr('href'); | |
var beer=href.substr(href.lastIndexOf('-')+1); | |
validBeers.push(beer); | |
}); | |
} | |
//Always make WR locations valid | |
if(validVenues.length > 0) | |
validVenues.push('WildRose'); | |
// console.info(validVenues, validBeers); | |
$.each(beerfinder.markers, function(index, marker) { | |
var failed = false; | |
if(validVenues.length > 0 && validVenues.indexOf(marker.loctype) < 0) | |
{ | |
failed = true; | |
} | |
if(!failed && validBeers.length > 0) | |
{ | |
var hasBeer = false; | |
for(var i = 0; i < marker.beers.length; i++) | |
{ | |
if(validBeers.indexOf(marker.beers[i]) >=0) | |
{ | |
hasBeer = true; | |
break; | |
} | |
} | |
failed = !hasBeer; | |
} | |
if(failed) | |
marker.setMap(null); | |
else | |
marker.setMap(beerfinder.map); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment