Javascript select Google markers for all possibilities of checkbox conditions

前端 未结 1 1291
挽巷
挽巷 2021-01-07 14:44

I am placing markers on a Google map. I have a number of checkboxes (19 currently) in a form that I am trying to create a dynamic \"and\" condition to display or not display

相关标签:
1条回答
  • 2021-01-07 15:08

    An approach where you did not have to iterate over each q-property of the marker:

    Use bitwise operations.

    Lets assume you have a marker with the properties q1, q3 and q5 set to true .

    Instead of storing each property of the marker, use a decimal and set the bytes 1,3 and 5.

    This would result in the binary value 10101 (the decimal is 21, store it as the marker-property)

    To compare this decimal with the checked checkboxes use also a decimal and set the bytes where the checkboxes are checked.

    Assuming the checkboxes for q1 and q5 are checked, the binary value would be 10001(decimal 17)

    All you have to do now is to check if the result of a bitwise AND of the markers property and the checkboxes-property is equal to the checkboxes-property

    ((21 & 17)>>>0===17)//returns true
    

    This approach would work for up to 32 properties.

    Demo: http://jsfiddle.net/doktormolle/hsPVS/

    0 讨论(0)
提交回复
热议问题