2013-04-23 4 views
1

Я надеюсь получить немного прозрения о функции show/hide в gmaps4rails.gmaps4rails показать скрыть функциональность

example functionality

// == shows all markers of a particular category, and ensures the checkbox is checked == 
    function show(category) { 
    for (var i=0; i<gmarkers.length; i++) { 
     if (gmarkers[i].mycategory == category) { 
     gmarkers[i].setVisible(true); 
     } 
    } 
    // == check the checkbox == 
    document.getElementById(category+"box").checked = true; 
    } 

    // == hides all markers of a particular category, and ensures the checkbox is cleared == 
    function hide(category) { 
    for (var i=0; i<gmarkers.length; i++) { 
     if (gmarkers[i].mycategory == category) { 
     gmarkers[i].setVisible(false); 
     } 
    } 
    // == clear the checkbox == 
    document.getElementById(category+"box").checked = false; 
    // == close the info window, in case its open on a marker that we just hid 
    infowindow.close(); 
    } 

    // == a checkbox has been clicked == 
    function boxclick(box,category) { 
    if (box.checked) { 
     show(category); 
    } else { 
     hide(category); 
    } 
    // == rebuild the side bar 
    makeSidebar(); 
    } 

    function myclick(i) { 
    google.maps.event.trigger(gmarkers[i],"click"); 
    } 


    // == rebuilds the sidebar to match the markers currently displayed == 
    function makeSidebar() { 
    var html = ""; 
    for (var i=0; i<gmarkers.length; i++) { 
     if (gmarkers[i].getVisible()) { 
     html += '<a href="javascript:myclick(' + i + ')">' + gmarkers[i].myname + '<\/a><br>'; 
     } 
    } 
    document.getElementById("side_bar").innerHTML = html; 
    } 

Итак, в мой контроллер, я строю список маркеров, и в том числе их категории, как JSON.

@markersLoc = @locSearch.to_gmaps4rails do |loc, marker| 
    letter.next! 
    marker_image = "http://chart.apis.google.com/chart?chst=d_map_pin_letter&chld=#{letter}|82ABDD|000000" 
    marker.infowindow render_to_string(partial: '/events/info', 
            locals: {object: loc}) 
    marker.picture({picture: marker_image, 
        width: 32, 
        height: 32, 
        marker_anchor: [11,30], 
        shadow_picture: "http://chart.apis.google.com/chart?chst=d_map_pin_shadow", 
        shadow_width: 110, 
        shadow_height: 110, 
        shadow_anchor: [12,34]}) 
    marker.title loc.what 
    marker.sidebar render_to_string(partial: '/events/sidebar', 
            locals: {object: loc, letter: marker_image}) 
    marker.json({cat: loc.category}) 
end 

Я как бы застрял здесь. Я знаю, что строка :cat доступна (например: 1,3,4), но я не уверен, как ее использовать для достижения того, что мне нужно.

ответ

1

Был в состоянии в значительной степени использовать то, что было там, с некоторыми изменениями. Это дало мне функциональность, чтобы иметь 9 категорий (может быть больше или меньше), и только просматривать то, что я хочу. Потрясающие.

// == shows all markers of a particular category, and ensures the checkbox is checked == 
function show(category) { 
    var regEx = new RegExp("[" + category + "]") 
    for (var i=0; i<Gmaps.map.markers.length; i++) { 
    if (Gmaps.map.markers[i].cat) { 
     if (Gmaps.map.markers[i].cat.match(regEx)) { 
     Gmaps.map.showMarker(Gmaps.map.markers[i]); 
     } 
    } 
    } 
    // == check the checkbox == 
    document.getElementById(category+"box").checked = true; 
} 

// == hides all markers of a particular category, and ensures the checkbox is cleared == 
function hide(category) { 
    var regEx = new RegExp("[" + category + "]") 
    for (var i=0; i<Gmaps.map.markers.length; i++) { 
    if (Gmaps.map.markers[i].cat) { 
     if (Gmaps.map.markers[i].cat.match(regEx)) { 
     Gmaps.map.hideMarker(Gmaps.map.markers[i]); 
     } 
    } 
    } 
    // == clear the checkbox == 
    document.getElementById(category+"box").checked = false; 
    // == close the info window, in case its open on a marker that we just hid 
    // Gmaps.map.infowindow.close(); 
} 

// == a checkbox has been clicked == 
function boxclick(box,category) { 
    if (box.checked) { 
    show(category); 
    } else { 
    hide(category); 
    } 
} 
Смежные вопросы