When a draggable marker is dragged and dropped, a server side markerDragListener can be invoked passing a MarkerDragEvent that contains a reference to the dragged marker whose positon is updated already.
<script src="https://maps.google.com/maps/api/js?key=AIzaSyCvCDkYieuUBmMWon_mfLAfjuaeuosuqow&sensor=false"></script>
<div class="card">
<h:form prependId="false">
<p:growl id="growl" showDetail="true"/>
<p:gmap id="gmap" center="36.890257,30.707417" zoom="13" type="HYBRID"
model="#{draggableMarkersView.draggableModel}" style="width:100%;height:400px">
<p:ajax event="markerDrag" listener="#{draggableMarkersView.onMarkerDrag}" update="growl"/>
</p:gmap>
</h:form>
</div>
package org.primefaces.showcase.view.data.gmap;
import jakarta.annotation.PostConstruct;
import jakarta.faces.application.FacesMessage;
import jakarta.faces.context.FacesContext;
import jakarta.faces.view.ViewScoped;
import jakarta.inject.Named;
import java.io.Serializable;
import io.quarkus.runtime.annotations.RegisterForReflection;
import org.primefaces.event.map.MarkerDragEvent;
import org.primefaces.model.map.DefaultMapModel;
import org.primefaces.model.map.LatLng;
import org.primefaces.model.map.MapModel;
import org.primefaces.model.map.Marker;
@Named
@ViewScoped
@RegisterForReflection(serialization = true)
public class DraggableMarkersView implements Serializable {
private MapModel<Long> draggableModel;
@PostConstruct
public void init() {
draggableModel = new DefaultMapModel<>();
//Shared coordinates
LatLng coord1 = new LatLng(36.879466, 30.667648);
LatLng coord2 = new LatLng(36.883707, 30.689216);
LatLng coord3 = new LatLng(36.879703, 30.706707);
LatLng coord4 = new LatLng(36.885233, 30.702323);
//Draggable
draggableModel.addOverlay(new Marker<>(coord1, "Konyaalti", 1L));
draggableModel.addOverlay(new Marker<>(coord2, "Ataturk Parki", 2L));
draggableModel.addOverlay(new Marker<>(coord3, "Karaalioglu Parki", 3L));
draggableModel.addOverlay(new Marker<>(coord4, "Kaleici", 4L));
for (Marker<Long> premarker : draggableModel.getMarkers()) {
premarker.setDraggable(true);
}
}
public MapModel<Long> getDraggableModel() {
return draggableModel;
}
public void onMarkerDrag(MarkerDragEvent<Long> event) {
Marker<Long> marker = event.getMarker();
FacesContext.getCurrentInstance().addMessage(null,
new FacesMessage(FacesMessage.SEVERITY_INFO,
"Marker " + marker.getData() + " Dragged",
"Lat:" + marker.getLatlng().getLat() + ", Lng:" + marker.getLatlng().getLng()));
}
}