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.
(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>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With