Created
September 12, 2011 14:09
-
-
Save SergXIIIth/1211336 to your computer and use it in GitHub Desktop.
shops on google map with filter them by country
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
log = function(mes){ | |
if (console){ | |
console.log(mes); | |
} | |
} | |
jQuery.fn.rank = function(min, max) { | |
var result = $.grep(this, function(shop){ | |
return min <= shop.rank && shop.rank <= max | |
}); | |
return $(result); | |
}; | |
jQuery.fn.rank_top = function(top_count) { | |
var shop_sort = this.get().sort(function(shop_a, shop_b){ | |
return shop_b.rank - shop_a.rank; | |
}); | |
var result = []; | |
$.each(shop_sort, function(i, shop){ | |
if (i + 1 <= top_count ) | |
{ | |
result.push(shop); | |
} | |
}) | |
return $(result); | |
}; | |
jQuery.fn.fitOnMap = function(){ | |
var bounds = new google.maps.LatLngBounds(); | |
this.each(function(i, shop){ | |
bounds.extend(shop.geocode()); | |
}); | |
map.fitBounds(bounds); | |
// not too close | |
if (map.getZoom() > 15) | |
{ | |
map.setZoom(15); | |
} | |
return this; | |
} | |
jQuery.fn.attrs = function(name) { | |
var attrs = this.map(function(){ return $.trim($(this).attr(name)); }); | |
attrs = attrs.get(); | |
return attrs; | |
} | |
jQuery.fn.blank = function() { return this.size() === 0; }; | |
jQuery.fn.present = function() { return this.size() > 0; }; | |
(function(){ | |
var geocoder; | |
function shop_geo_init(shop){ | |
var $shop = $(shop); | |
function g(name){ return $shop.attr("data-" + name); } | |
shop.zip = g("zip"); | |
shop.country = g("country"); | |
shop.address = g("address") + ", " + g("country") + " " + g("zip") + " " + g("town"); | |
shop._geocode = null; | |
shop.geocode = function(on_done){ | |
if (shop._geocode == null) | |
{ | |
geocoder.geocode( { 'address': shop.address}, function(results, status) { | |
if (status == google.maps.GeocoderStatus.OK) { | |
shop._geocode = results[0].geometry.location | |
} | |
else | |
{ | |
log("Geocode was not successful for the following reason: " + status); | |
} | |
if ($.isFunction(on_done)){ on_done(); } | |
}); | |
} | |
return shop._geocode; | |
} | |
shop.marker = {}; | |
shop.marker.show = function(){ | |
shop.marker = new google.maps.Marker({ | |
map: map, | |
position: shop.geocode(), | |
icon: "http://maps.gstatic.com/mapfiles/ms2/micons/blue-dot.png" | |
}); | |
shop.infowindow = new google.maps.InfoWindow({ | |
content: $(".infowindow", shop).clone().html() | |
}); | |
google.maps.event.addListener(shop.marker, 'click', function() { | |
$("[data-shop]") | |
.removeClass("active") | |
.each(function(){ this.infowindow.close(); }); | |
shop.infowindow.open(map,shop.marker); | |
$(shop).addClass("active"); | |
}); | |
} | |
} | |
function map_init(on_done){ | |
var myOptions = { | |
zoom: 3, | |
center: new google.maps.LatLng(50.076969, 14.463957), | |
mapTypeId: google.maps.MapTypeId.ROADMAP | |
}; | |
map = new google.maps.Map($("#map_canvas").get(0), myOptions); | |
geocoder = new google.maps.Geocoder(); | |
// get all shop geocode, wait till it here | |
var geo_request_count = 0; | |
$("[data-shop]").each(function(){ | |
var shop = this; | |
shop_geo_init(shop); | |
geo_request_count = geo_request_count + 1; | |
shop.geocode(function(){ | |
shop.marker.show(); | |
geo_request_count = geo_request_count - 1; | |
if (geo_request_count < 1 && $.isFunction(on_done)) | |
{ | |
on_done(); | |
} | |
}); | |
}); | |
} | |
function filter_calc_rank(shop) | |
{ | |
var country = $("#filter-shopdigest-country").val(); | |
var zip = $("#filter-shopdigest-location").val(); | |
shop.rank = 0; | |
if (country == 'world'){ | |
shop.rank = shop.rank + 50; | |
if (shop.zip == zip) { shop.rank = shop.rank + 30; } | |
} | |
if (shop.country == country) { shop.rank = shop.rank + 50; } | |
if (shop.country == country && shop.zip == zip) { shop.rank = shop.rank + 30; } | |
// last 20 rank for tags | |
var checked_tags = $("[data-filter-tag]:checked").attrs("data-filter-tag"); | |
checked_tags = $.map(checked_tags, function(tag){ return tag.toLowerCase(); }); | |
if (checked_tags.length > 0) | |
{ | |
var tag_rank = 20 / checked_tags.length; | |
//log(checked_tags); | |
$.each($(shop).attr("data-tags").split(","), function(i, tag){ | |
//log("i = " + i + " tag = '" + tag.toLowerCase() + "'"); | |
var tag_checked = $.inArray($.trim(tag.toLowerCase()), checked_tags) > -1; | |
if (tag_checked){ | |
shop.rank = shop.rank + tag_rank; | |
//log(shop.zip + "add rank " + tag_rank); | |
} | |
}); | |
} | |
// for debug | |
$(shop).attr("data-rank", shop.rank); | |
} | |
function filter_sort_by_rank(){ | |
var shops = $("[data-shop]"); | |
// filter by rank | |
if (shops.rank(80, 100).present()) | |
{ | |
shops.rank(0, 29).hide(); | |
shops.rank(30, 100).show(); | |
shops.rank(80, 100).fitOnMap(); | |
} | |
else { if (shops.rank(30, 100).present()) | |
{ | |
shops.rank(0, 29).hide(); | |
shops.rank(30, 100).show().fitOnMap(); | |
} | |
else | |
{ | |
shops.rank(0, 0).hide(); | |
shops.rank(1, 100).show().fitOnMap(); | |
}} | |
shops.sortElements(function (shop_a, shop_b) { | |
return shop_b.rank - shop_a.rank; | |
}); | |
} | |
function filter(){ | |
var shops = $("[data-shop]"); | |
// build shop rank 0..100 | |
shops.each(function(){ filter_calc_rank(this); }) | |
filter_sort_by_rank(); | |
return false; | |
} | |
window.shop = {}; | |
window.shop.list = function(){ | |
map_init(function(){ | |
$(".search_go").click(function(){ return filter(); }); | |
$(".search_location").change(function(){ return filter(); }); | |
$("[data-filter-tag]").change(filter); | |
$("[data-filter-all]").change(function(){ | |
$("[data-filter-tag]", $(this).closest("ol")) | |
.prop("checked", $(this).prop("checked")) | |
.first().change(); | |
}); | |
}); | |
} | |
window.shop.one = function(){ | |
map_init(function(){ | |
$("[data-shop]").fitOnMap(); | |
}); | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment