Back to Top

Solution: Map moves when we drag the marker in touchscreen

Solution: Map moves when we drag the marker in touchscreen

From the few days, I am working with the google map.Google map is a very popular feature on most websites and I want to implement draggable marker feature in Google Map. you can set marker as draggable by setting draggable parameter as true into marker variable.

The following code will allow to provide drag option to google map.

 
var marker = new google.maps.Marker({  
    draggable: true,
});

Today I found an issue is when I drag the Marker, the map also starts dragging only for touchscreen devices like mobile.it was working fine with chrome but not in other browsers.

so i got the pretty simple solution for that issue as follows:

google.maps.event.addListener(marker, 'dragstart', function(){
    mapObj.setOptions({ draggable: false }); // mapObj is the object of google map
});

google.maps.event.addListener(marker, 'dragend', function(){
    mapObj.setOptions({ draggable: true }); // mapObj is the object of google map
});

That’s it.check your map into the touchscreen devices.

Hope this post will helpful for you, waiting for your responses.Thank you for reading.Feel free to share your thoughts! Don’t Forget to Follow us on Twitter or Subscribe us to Get the Latest Updates.

Comments (1)

  1. Of course!!! How come I didnt think that!!! Thanks Bhumi you solution was simple , elegant and 100% useful… :).

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Most Popular Posts

How to use Git init command

Posted on 1 year ago

Bhumi

How to create Bootstrap Switch using CSS

Posted on 3 years ago

Bhumi

What are Traits in PHP

Posted on 3 years ago

Bhumi

To prevent caching in web browser

Posted on 7 years ago

Bhumi

How to Create a Simple Anti-Spam Captcha

Posted on 6 years ago

Bhumi

The Reader’s Poll – June 2015

Posted on 3 years ago

Bhumi

The Reader’s Poll – August 2014

Posted on 4 years ago

Bhumi

The Reader’s Poll – June 2014

Posted on 4 years ago

Bhumi

The Readers’ Poll – May 2014

Posted on 5 years ago

Bhumi

The Readers’ Poll – August 2013

Posted on 5 years ago

Bhumi