Can you please take a look at this Demo and let me know how I can make the markers Dragable when the check box is unchecked. Please be informed that all markers has their own associated checkbox it means that user can lock each marker which they want( Not all together). Initially all Markers are unchecked when they loaded to page . And finally I need to change the icon when the marker checked as lock.
var contentString = ' -- Lock <input name="your_name" value="your_value" type="checkbox">';
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
var infowindow = new google.maps.InfoWindow({ content: "" });
var data = {
"markers": [{
"latitude": 11.606503,
"longitude": 122.712637,
"title": "Copenhagen"
}, {
"latitude": 11.585988,
"longitude": 122.757084,
"title": "Barcelona"
}
]
};
locations.length = 0;
for (p = 0; p < data.markers.length; p++) {
var marker = new google.maps.Marker({
position: new google.maps.LatLng(data.markers[p].latitude, data.markers[p].longitude),
map: map,
draggable: true,
title: "marker " + p,
id: p
});
bindMarker(marker);
bindInfoWindow(marker, map, infowindow, data.markers[p].title);
}
function bindMarker(marker) {
google.maps.event.addListener(marker, 'dragend', function () {
marker.setIcon('https://www.google.com/mapfiles/marker_green.png');
});
google.maps.event.addListener(marker, 'click', function () {
});
}
function bindInfoWindow(marker, map, infowindow, strDescription) {
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(strDescription + contentString);
infowindow.open(map, marker);
});
}
});
**
Update
function bindMarkerDrag(marker){
google.maps.event.addListener(checkbox, "click", function(){
draggable: false
//marker.setClickable(checkbox.checked);
});
}
then I add the
bindMarkerDrag(marker);
inside the loop Here is a Demo but not working
As I said in my comment, you need to add
checkbox.checked = !marker.getDraggable();
in your marker click event listener. Not in the checkbox event listener.Check this code!! I have updated buildInfoWindow function to meet your requirements.
You could something like:
This will set the draggable property to false or true before you start dragging (if you put it on the
dragstart
event, you will have a slight delay.) Replace the idcheckbox
with the one you need.