Skip to content

Instantly share code, notes, and snippets.

@objectivehtml
Created February 9, 2015 17:58
Show Gist options
  • Save objectivehtml/91552e5ca74e818707f0 to your computer and use it in GitHub Desktop.
Save objectivehtml/91552e5ca74e818707f0 to your computer and use it in GitHub Desktop.
$.fn.markerList = function(options) {
var titles = {};
var geocoder = new google.maps.Geocoder();
var _default = {
map: {},
bounds: new google.maps.LatLngBounds(),
lat: false,
lng: false,
location: false,
center: false
};
if(!options) {
var options = _default;
}
else {
options = $.extend(true, _default, options);
}
var t = {
map: false,
markers: {},
windows: {},
bounds: new google.maps.LatLngBounds()
};
t.map = options.map;
t.bounds = options.bounds;
t.addMarker = function(lat, lng, options) {
if(!lat) {
var lat = 0;
}
if(!lng) {
var lng = 0;
}
var _default = {
position: t.latLng(lat, lng),
icon: false,
content: ''
};
options = $.extend(true, _default, options);
var index = lat+','+lng;
var marker = new google.maps.Marker({
position: options.position,
map: t.map,
content: options.content,
icon: options.icon
});
var window = new google.maps.InfoWindow({
content: options.content
});
google.maps.event.addListener(marker, 'click', function() {
$.each(t.windows, function(i, window) {
window.close();
});
window.open(t.map, marker);
});
t.markers[index] = marker;
t.windows[index] = window;
t.bounds.extend(options.position);
t.map.fitBounds(t.bounds);
}
t.center = function(location) {
if(typeof location == "array") {
location = t.latLng(location[0], location[1]);
}
if(t.map) {
t.map.setCenter(location);
}
}
t.buildRequest = function(location) {
var request = {};
if(typeof location == "string") {
request.address = location;
}
if(typeof location == "array") {
request.location = t.latLng(location[0], location[1]);
}
if(typeof location == "object") {
request.location = location;
}
return request;
}
t.geocode = function(location, callback) {
var request = t.buildRequest(location);
geocoder.geocode(request, function(status, data) {
if(typeof callback == "function") {
callback(status, data);
}
});
}
t.latLng = function(lat, lng) {
return new google.maps.LatLng(lat, lng);
}
t.getOptions = function() {
return options;
}
t.getOption = function(option) {
if(options[option]) {
return options[option];
}
}
t.setOption = function(option, value) {
if(options[option]) {
options[option] = value;
}
}
t.zoom = function(zoom) {
if(typeof zoom == "number" && zoom >= 0 && zoom <= 20) {
t.map.setZoom(zoom);
}
}
if(options.center) {
if(options.lat !== false && options.lng !== false) {
options.center = [options.lat, options.lng];
}
if(typeof options.center == "array") {
t.map.setCenter(t.latLng(options.center[0], options.center[1]));
t.zoom(options.zoom);
t.init();
}
else {
t.geocode(options.center, function(data, status) {
t.map.setCenter(data[0].geometry.location);
t.zoom(options.zoom);
t.init();
});
}
}
var self = this;
t.init = function() {
self.each(function() {
var $t = $(this);
var lat = $t.attr('data-lat');
var lng = $t.attr('data-lng');
if(isNaN(lat) || isNaN(lng)) {
$t.parent().hide();
}
var data = {
venue_name : $t.attr('data-venue-name'),
event_name : $t.attr('data-event-name'),
email : $t.attr('data-email'),
phone : $t.attr('data-phone'),
start : $t.attr('data-next-start'),
end : $t.attr('data-next-end'),
url_title : $t.attr('data-url-title')
};
$t.click(function(e) {
var lat = $(this).attr('data-lat');
var lng = $(this).attr('data-lng');
var index = lat+','+lng;
var window = t.windows[index];
$.each(t.windows, function(i, w) {
w.close();
});
t.windows[index].open(t.map, t.markers[index]);
t.center(t.windows[index].anchor.getPosition());
e.preventDefault();
})
if(lat && lng) {
var index = lat+','+lng;
if(!titles[index]) {
titles[index] = [];
}
titles[index].push('<li><a href="'+data.url_title+'">'+data.event_name+'</a></li>');
if(!t.markers[index]) {
var html = [
'<h5>'+data.event_name+'</h5>',
'<p class="venue-name">'+data.venue_name+'</p>',
'<ul class="nostyle map-attribute">',
(data.email != '' ? '<li><span class="icon-envelope-alt"></span> '+data.email+'</li>' : ''),
'<li><span class="icon-time"></span> '+data.start+'',
'</ul>',
'<p><a class="map-to-detail" href="'+data.url_title+'">View Event Details</a></p>'
].join('');
t.addMarker(lat, lng, {
content: html,
icon: '/assets/img/marker.png'
});
var marker = t.markers[index];
var window = new google.maps.InfoWindow({
content: options.content
});
google.maps.event.addListener(marker, 'click', function() {
window.open(t.map, marker);
});
t.bounds.extend(marker.getPosition());
t.map.fitBounds(t.bounds);
}
else {
var window = t.windows[index];
var html = [
'<h5>'+data.venue_name+'</h5>',
'<p>This venue has more than one event scheduled:</p>',
'<ul class="nostyle multi-map disc">'+(titles[index].join(''))+'</ul>'
];
if(titles[index].length > 1) {
t.windows[index].setContent(html.join(''));
}
}
}
});
};
}
$('.icon-map-marker a').markerList({
map: map_map,
center: 'st. catharines, ca',
zoom: 11
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment