I want to create a form where you can add the location of certain events by means of a map of google maps in a comfortable way but I can not move the location of the marker by clicking, I only get it by dragging it. My code:
HTML:
<input id="coordenadasEquipo" name="coordenadasEquipo" data-toggle="modal" data-target="#ModalEquipos" type="text" class="form-control display-inline-block" placeholder="Localización" required>
<!-- Modal Coordenadas-->
<div class="modal fade" id="ModalEquipos" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg" role="document">
<div class="modal-content modal-lg">
<div class="modal-header modal-lg">
<h5 class="modal-title" id="exampleModalLabel">Localización</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
</div>
<div class="modal-body modal-lg">
<div id="map"></div>
</div>
<div class="modal-footer modal-lg">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Guardar</button>
</div>
</div>
</div>
</div>
JS:
var marker;
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 6,
center: {lat: 40.4165000, lng: -3.7025600}
});
marker = new google.maps.Marker({
map: map,
draggable: true,
animation: google.maps.Animation.DROP,
position: {lat: 40.4165000, lng: -3.7025600}
});
marker.addListener('click', toggleBounce);
marker.addListener( 'dragend', function (event)
{
//escribimos las coordenadas de la posicion actual del marcador dentro del input #coords
document.getElementById("coordenadasEquipo").value = this.getPosition().lat()+","+ this.getPosition().lng();
});
}
// permite arrastar el marcador
function toggleBounce() {
if (marker.getAnimation() !== null) {
marker.setAnimation(null);
} else {
marker.setAnimation(google.maps.Animation.BOUNCE);
}
}
// captura el evento click sobre le marcador
function funcionClick() {
if (marker.getAnimation() != null) {
marker.setAnimation(null);
} else {
marker.setAnimation(google.maps.Animation.BOUNCE);
}
}
// cargamos el mapa en la ventana modal
$('#ModalEquipos').on('shown.bs.modal', function(){
initMap();
});
The operation consists of when you click on the field of Coordinates form a modal window appears that contains the map with a marker that you can move or drag or click on the position and when leaving the modal window it saves the coordinates in the coordinate input. How can I do the click event?