Move two markers on Google Maps simultaneously

After researching various articles and posts on Stack Overflow about selecting multiple markers in Google Maps, I have been unable to find a solution for dragging two markers simultaneously. While I've managed to make it work so that one marker can trigger an event on its sibling and drag the related marker, it interrupts the dragging event on the original clicked marker.

Unfortunately, using complex polygons is not feasible as changing the color of just one path on a complex marker SVG poses challenges. Instead, I have to create two separate SVGs - one for the center fill and another for the outer fill - and then adjust the color of the outer fill accordingly.

This is the approach I've taken:

Firstly, I create the markers and position them at the same location.

Then, I pass these markers into the following function:

addMultiMarkerListener:function(id, outer, inner){
    var arr = ['mousedown', 'mouseup', 'click','mouseover','mouseout', 'dragend','drag','dragstart'];
    var evtArr = {};
        _.each(arr, function(evt){
            evtArr[evt] = {};
            evtArr[evt][id] = false;

            outer.addListener(evt, function(){
                if(!evtArr[evt][id]){
                    evtArr[evt][id] = true;
                    google.maps.event.trigger(inner, evt);
                    evtArr[evt][id] = false;

                }

            })

            inner.addListener(evt, function(){
                 if(!evtArr[evt][id]){
                    evtArr[evt][id] = true;
                    google.maps.event.trigger(outer, evt);
                    evtArr[evt][id] = false;
                }
            })
        })

    },

My goal is to trigger the mousedown event for both markers so that I can click and drag them at the same time. Currently, the method I'm using halts the first mousedown event when triggering the mousedown event on the sibling marker.

I have also considered implementing a separate event that would not interfere with the initial one. It may require some brainstorming, but I believe I can achieve a solution. Any insights or suggestions from others would be greatly appreciated.

Answer №1

I successfully resolved the issue by making a change in my approach. Instead of triggering two mousedown events, I opted to capture the position of the first marker during the drag event, and then assign the same position to the second marker. Here is how I implemented it.

  addDragListener: function(marker, id, centerMarker) {
        var self = this;
        var fill = centerMarker;

        marker.addListener('drag', function(){
            var position = marker.getPosition();
            fill.setPosition(position);
        })
  }

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Instructions for activating a button in the absence of any inputs

I need help enabling a button in angularjs based on whether any of the inputs are filled out. I was successful in disabling a button when only one input is checked, but how can I do this for all inputs affecting one button? This is what I've attempted ...

Retrieving data from a <span> element within a webpage created using ReactJS

I am completely new to the world of web design and development, so there may be some mistakes in my request. Essentially, I have a webpage that contains numerous span tags like the following example: These span tags are part of a significantly large DOM t ...

Tips on sending a function's return value to an object in Node.js

I'm currently exploring Node.js and working on a REST API for a project. One of the functionalities I am implementing is a post request to store form data in a database. Some values will be retrieved from the form data while others will be generated ...

Generate PDF Files Using Ajax

Currently, I am utilizing FPDF to generate my report. $pdf = new Report('P','mm','A4', $_POST); $pdf->AddPage(); $pdf->Output('file.pdf','I'); Furthermore, I am employing ajax to send a request to t ...

What is the best way to run a promise once the endpoint has responded?

Is it possible to fulfill a promise after the response has been sent to the browser without using await? I must convert a video and save the output file in the operating system. For performance reasons, I want to avoid using await. Here's an example: ...

When trying to insert JavaScript into the console, a section of the code fails to execute

In this code snippet, I am fetching the most common English words from Wikipedia, extracting all the words from the page, and then filtering out the commonly used words: // get table data from most common words var arr = []; $.ajax({ url: 'https:/ ...

Ways to extract the value from a jQuery object

How can I retrieve the selected time value using a jQuery plugin and save it on a specific input element within the page? Refer to the plugin and code provided below: http://jsfiddle.net/weareoutman/YkvK9/ var input = $('#input-a'); input.cloc ...

Managing the automatic download of a zip file through JQuery - A guide

When working with a REST API and creating a zip file for forced download, the task is to call the API by sending necessary POST data through JQuery to initiate the download. How can this goal be accomplished effectively? ...

Problem with Ext.net TabPanel

I am encountering a problem with the Ext.net TabPanel. Every time the page containing the tab panel is opened for the first time after the application has been rebuilt, it throws an error Uncaught TypeError: Object [object Object] has no method 'getCo ...

Refining the options in security question dropdown menus

Firstly: The title should mention filtering security options in dropdown lists, but it seems I'm restricted from using the term questions or question in the title. I came across this code example, but it appears to be outdated. Does anyone know why a ...

JMeter recording displays a script alert error notification

After recording the JMeter script on blazemeter, it initially worked fine on the website. However, upon running the jmx file, I encountered an error message: "alert("Something went wrong. Please try again");window.history.back();" I&a ...

Issues arise with the functionality of CSS and JavaScript after successfully deploying a Next.js and React project to the server side

I need to deploy my React+Next and Node.js projects on the same port. To achieve this, I converted my TypeScript files to JavaScript and moved the .next folder to the backend side. Now, my API and frontend code are functioning on the same port thanks to Ex ...

The waypoints.js snippet seems to be malfunctioning in this specific context

After experimenting with waypoints.js (jQuery.waypoints) for some time, I've been attempting to utilize this library to incorporate animations on children elements within a specific container. Here is the approach I have taken: var waypoints = $( ...

What is the process for refreshing information in Laravel?

I am currently facing a challenge with updating an existing user in my system and I would appreciate some guidance on how to go about it. Below is the API route I am utilizing to update the user: Route::put('/user/{user}', [UserController::class ...

Managing errors in jQuery's .ajax function

Having some issues with jQuery.ajax() while trying to fetch an html code snippet from table_snippet.html and replacing the element in my html code. The error handler in jQuery.ajax() gets triggered instead of the expected success handler. <!DOCTYPE H ...

Certain public files in Express cannot be accessed locally

While running my Node.js application on localhost, I am able to access http://localhost:3000/css/dashboard.css without any issues. However, when attempting to access http://localhost:3000/css/logo.png for a logo image in the same directory, all I receive i ...

The CustomValidator ClientValidationFunction will only activate on the second or subsequent submission if CheckBoxList is checked

I am encountering an issue with a user control that is dynamically added to pages on DNN. The user control is built on a customized version of CheckBoxList and includes a CustomValidator with ClientValidationFunction set to a javascript function. It works ...

Changing global properties in VueCli

Recently, I integrated a component library into my Vue 3 project. All instances of the component require the same styles. Instead of manually adjusting each instance's props, I opted to utilize a global property: app.config.globalProperties.$tooltipS ...

What is the process for exporting an NPM module for use without specifying the package name?

I am looking for a way to export an NPM module so that it can be used without specifying the package name. Traditionally, modules are exported like this: function hello(){ console.log("hello"); } module.exports.hello = hello; And then imported and ...

Creating a welcome screen that is displayed only the first time the app is opened

Within my application, I envisioned a startup/welcome page that users would encounter when the app is launched. They could then proceed by clicking a button to navigate to the login screen and continue using the app. Subsequently, each time the user revisi ...