javascript - Check for checkbox value after Google Map has loaded -
i think i'm missing basic javascript. use help!
scenario: after clicking "search" google map loads. loads, checkbox appears says, "redo results when map moved" (similar yelp). if map dragged or zoomed, results update. part works fine.
problem: value of checkbox
(true or false) recognized redo()
function when map loads, , doesn't check again after (because though redo()
updating results it's not reloading map). if toggle checkbox, "redo" function doesn't notice!
simplified code:
//haml %input{:type => "checkbox", :id => "followcheck", :name => "followcheck", :onclick => 'handleclick();'} //load map function showlocations() { map = new google.maps.map(document.getelementbyid("map_canvas"), { bunch of map stuff; map.fitbounds(bounds); redo(); //this redo function, see below } } function handleclick(checkbox) { var chk = document.getelementbyid("followcheck").checked; console.log("checkbox value " + chk); return chk; } function redo() { var chk = handleclick(); console.log("inside redo, value " + chk); if (chk == true) { google.maps.event.addlistener(map, 'mouseup', function() { mapadjusted = 1; updatemapafteruserinteraction(); }); google.maps.event.addlistener(map, 'zoom_changed', function() { mapadjusted = 1; updatemapafteruserinteraction(); }); } }
(note, redo() function written collaborator, smarter me , out of town.)
so, redo()
loads when map loads. if checkbox true when map loads, redo()
function processes zoom/drag
. still after changing value false (and if value false when page loads, opposite happens).
what needed redo()
see value has changed? push()
? bind()
? get/set? change()
? addeventlistener()
? should reload map when checkbox toggled (i'd rather not)? whole new approach? i'm stuck! thanks. i'm open jquery ideas, too.
i solved this. first off, small adjustment function able turn on after checking box (even if results loaded while box unchecked via previous page session). here code (i found 3 variations had same result):
function redo() { if ($("#followcheck").is(':checked')) { google.maps.event.addlistener(map, 'mouseup', function() { mapadjusted = 1; updatemapafteruserinteraction(); }); google.maps.event.addlistener(map, 'zoom_changed', function() { mapadjusted = 1; updatemapafteruserinteraction(); }); } }
the problem didn't turn off. , that's because i've added 2 listeners! this answer helped me remove listeners. note (for me) worked when removelistener()
function within addlistener()
function. kind of strange.
function redo() { var listener1 = google.maps.event.addlistener(map, 'mouseup', function() { if (!$("#followcheck").is(':checked')) { google.maps.event.removelistener(listener1); } else { mapadjusted = 1; updatemapafteruserinteraction(); } }); var listener2 = google.maps.event.addlistener(map, 'zoom_changed', function() { if (!$("#followcheck").is(':checked')) { google.maps.event.removelistener(listener2); } else { mapadjusted = 1; updatemapafteruserinteraction(); } }); }
oh , changed onclick()
onchange()
Comments
Post a Comment