Click here to Skip to main content
15,896,437 members
Articles / Web Development / HTML
Tip/Trick

Google Maps Drag to Select Multiple Markers or Click Marker to Select and Unselect

Rate me:
Please Sign up or sign in to vote.
4.77/5 (12 votes)
20 Jul 2017CPOL1 min read 32.7K   1.2K   16   9
Google Maps Drag to select multiple markers or click Marker to select and unselect

Introduction

My team searched the net to find a sample solution that demonstrates the following but didn't find any and they tried to develop their own solution but were not able to do so. As a result, I dedicated some of my time and managed to develop a smart solution.

On GoogleMaps, I tended to:

  1. Switch marker image when selecting or unselecting it to show it's selected or unselected
  2. Allow user to press "shift+mouse drag" to select all markers in an area on the map and change their image to show they are selected

Background

You need to have knowledge of JavaScript and HTML.

Explanation

This zip folder contains 2 files:

  1. The first file is the HTML that renders the map
  2. The second file is a script file that helps to shift+drag to select Markers

A Brief Explanation Of the Code

  • The script file belongs to Google but I modified it to pass the bounds, after drag, to a script function on the HTML page and this function in turn gets all the markers on map that reside in those bounds and then change their images to show they are selected.
  • Final note - just download the zip, then extract it and double click the HTML to be displayed in browser and everything will go fine.

In my sample, this is how markers look before selection:

Image 1

How the map looks while selecting using Shift and Drag:

Image 2

How markers look after selection and switching to pink images:

Image 3

Then user can click on any marker to unselect and switch back to the red image:

Image 4

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Lighthouse Technology
Egypt Egypt
He is a Senior Software Developer. He provides well designed and developed software solutions using ASP.NET(MVC, Web forms) and Desktop technologies.

He is professional in using Java, C#, VB, Ado.Net, Entity Framework, Codesmith, LINQ, Asp.Net, MVC, Ajax, Sql Server, Business Intelligence, JavaScript, Jquery, Angular, TypeScript, Telerik web and windows controls, Telerik Kendo controls, Design Patterns,UML, XML, Web service, WCF, Web API.


Comments and Discussions

 
GeneralMy vote of 5 Pin
Rodolfo Redolfi 202119-Apr-22 0:30
Rodolfo Redolfi 202119-Apr-22 0:30 
QuestionZIP file not working Pin
Member 1460772529-Sep-19 21:57
Member 1460772529-Sep-19 21:57 
QuestionNot work Pin
Member 1337045020-Aug-17 23:12
Member 1337045020-Aug-17 23:12 
GeneralMy vote of 4 Pin
Member 1236439024-Jul-17 0:49
Member 1236439024-Jul-17 0:49 
QuestionThe Zip File Pin
Member 93060557-Jul-16 7:39
Member 93060557-Jul-16 7:39 
AnswerRe: The Zip File Pin
Member 93060557-Jul-16 9:01
Member 93060557-Jul-16 9:01 
QuestionZip File not available. Pin
Karthik_Mahalingam7-Jul-16 5:02
professionalKarthik_Mahalingam7-Jul-16 5:02 
AnswerMessage Closed Pin
7-Jul-16 8:20
khaled Ezzat Abdelfattah Abdelgawad7-Jul-16 8:20 
GeneralRe: Zip File not available. Pin
Karthik_Mahalingam7-Jul-16 9:13
professionalKarthik_Mahalingam7-Jul-16 9:13 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.