Question

I want to store the previous location before dragging a marker using PrimeFaces GMap - Draggable Markers

How to do it ?

showcase : GMap - Draggable Markers :

http://www.primefaces.org/showcase/ui/gmapDraggableMarkers.jsf

Was it helpful?

Solution

You could store copies of the original Marker objects and then, in the onMarkerDrag handler, you could find the original Marker that corresponds to the one passed by the MarkerDragEvent.

I say "copies" because it is possible that Primefaces modifies the LatLng of original Marker instead of creating a new one (most likely). You can use the "data" or "title" attributes to give them IDs and correctly match the copied Markers.

I can probably explain this with code, let me know if you need help.

Edit:

To meet your requirement, I would adapt the Primefaces showcase example as follows (notice that it requires the Marker's title to be unique. If this is not possible, implement this using the data attribute of the Marker class.):

package org.primefaces.examples.view;

//...

public class MapBean implements Serializable {

    private MapModel draggableModel;
    private Map<String, LatLng> positions = new HashMap<String, LatLng>(); // new

    public MapBean() {
        draggableModel = new DefaultMapModel();

        positions.put("Konyaalti", new LatLng(36.879466, 30.667648));
        positions.put("Ataturk Parki", new LatLng(36.883707, 30.689216));
        positions.put("Karaalioglu Parki", new LatLng(36.879703, 30.706707));
        positions.put("Kaleici", new LatLng(36.885233, 30.702323));

        for (Map.Entry<String, LatLng> e : positions.entrySet()) {
            Marker m = new Marker(e.getValue(), e.getKey());
            m.setDraggable(true);
            draggableModel.addOverlay(m);
        }
    }

    public void onMarkerDrag(MarkerDragEvent event) {
        marker = event.getMarker();

        addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO, "Marker Dragged", "Lat:" + marker.getLatlng().getLat() + ", Lng:" + marker.getLatlng().getLng()));

        // update position and get the old one
        LatLng oldPosition = positions.put(marker.getTitle(), marker.getLatlng());

        // ...
    }

    // ...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top