If I have 100 markers on map and I want to do something with only 15 of them is there any way to implement this by using CTRL + click on marker or standard cursor drag selection?
2 Answers
Ctrl+click is possible. Add a click event to the marker, and then in the event handler test if the CTRL key was pressed. If so, add the marker to an array that you can then do something with later.
var markers = [];
google.maps.event.addListener(marker, 'click', function (event) {
if (event.ctrlKey) {
markers.push(marker);
// some more code to change icon, add marker name to list, etc
// so user knows marker has been selected
}
});
There are some issues documented using this approach, but it has worked for me every time. Just make sure to test in multiple browsers.
See this Stack Overflow question for a drag-box-to-select-markers implementation.
-
This works best when combined with [Is it possible to detect ctrl key state on dblclick event](http://stackoverflow.com/q/3990893/1353267). – Samveen Dec 24 '14 at 11:14
-
I'm having the issue your referenced, but the link helped clear it up. – alaybourn Nov 10 '17 at 20:11
The way I have done this in the past is to attach a click event handler that changes the picture of the marker to something selected and than also adds that marker to an array. Then once you have your "15" markers selected you click a button (or start an event) that handles your selected markers. Where it gets interesting is when you can deselect a marker etc. HTH I know it doesn't fulfill the ctrl + click.

- 3,316
- 6
- 43
- 73