Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Maps API - double click spot on map to move marker there

Is there a built-in option in the google maps API or simple way to implement this functionality. I want to double click a spot on the map and have google maps move my marker to that spot.

like image 730
Kirk Ross Avatar asked Dec 14 '25 06:12

Kirk Ross


1 Answers

(Edit: look at Vinks' post. He points out Google maps has a disableDoubleClickZoom option.)

Sure

<!DOCTYPE html>
<html>
  <head>
    <title>Double click on the map</title>
    <meta name="viewport" content="initial-scale=1.0, user-scalable=no">
    <meta charset="utf-8">
    <style>
      html, body, #map-canvas {
        height: 500px;
        margin: 0px;
        padding: 0px
      }

    </style>
    <script src="https://maps.googleapis.com/maps/api/js?v=3.exp&libraries=places"></script>
    <script>
    function initialize() {
      var my_position = new google.maps.LatLng(50.5, 4.5);
      var map = new google.maps.Map(document.getElementById('map-canvas'), {
        center: my_position,
        zoom: 15
      });
      var marker = new google.maps.Marker({
        position: my_position,
        map: map
      });
      // double click event
      google.maps.event.addListener(map, 'dblclick', function(e) {
        var positionDoubleclick = e.latLng;
        marker.setPosition(positionDoubleclick);
        // if you don't do this, the map will zoom in
        e.stopPropagation();
      });
    }
    google.maps.event.addDomListener(window, 'load', initialize);
    </script>
  </head>
  <body>
    <div id="map-canvas"></div>
    <p>Double click on the map</p>
  </body>
</html>
like image 58
Emmanuel Delay Avatar answered Dec 16 '25 22:12

Emmanuel Delay



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!