Adding, selecting, and dragging markers in maps

It is possible to add markers onto the map via a data model and then select or drag it by interacting with the map.

How to do it…

The marker should be an instance of org.primefaces.model.map.Marker. Markers can be easily constructed by providing an instance of org.primefaces.model.map.LatLng to define their position. The latitude and longitude values could be provided to the LatLng class as constructor parameters. Markers will be added to the data model via the addOverlay method. This is shown in the following code:

MapModel markerModel = new DefaultMapModel();
markerModel.addOverlay(new Marker(new LatLng(41.073399,
  29.051971), "Bosphorus"));
markerModel.addOverlay(new Marker(new LatLng(41.118418,
  29.134026), "Bosphorus"));

The attributes of org.primefaces.model.map.Marker are listed in the following table:

Property

Default

Type

Description

title

null

String

This is the text to display on rollover

latlng

null

LatLng

This is the location of the marker

icon

null

String

This is the foreground image of the marker

shadow

null

String

This is the shadow image of the marker

cursor

pointer

String

This is the cursor to display on rollover

draggable

False

Boolean

This defines whether the marker can be dragged

clickable

True

Boolean

This defines whether the marker can be clicked

flat

False

Boolean

This is the shadow image not displayed when set to true

visible

True

Boolean

This defines the visibility of the marker

There's more…

The gmap component offers the overlaySelect and markerDrag AJAX behavior events to handle the selection and dragging of the markers placed on the map.

Selecting markers

The definition of <p:ajax>, along with the listener method, is given here:

<p:gmap ...>
  <p:ajax event="overlaySelect"
    listener="#{mapBean.onMarkerSelect}" update="growl" />
</p:gmap>

public void onMarkerSelect(OverlaySelectEvent event) {
  Marker selectedMarker = (Marker) event.getOverlay();
  MessageUtil.addInfoMessageWithoutKey(selectedMarker.getTitle(),
    selectedMarker.getLatlng().toString());
}

Dragging markers

To have draggable markers, each marker's draggable attribute should be set to true first. Then, the definition of <p:ajax>, along with the listener method, can be performed, as shown here:

<p:gmap ...>
  <p:ajax event="markerDrag"
    listener="#{mapBean.onMarkerDrag}" update="growl" />
</p:gmap>

public void onMarkerDrag(MarkerDragEvent event) {
  MessageUtil.addInfoMessage("marker.dragged",
    event.getMarker().getLatlng().toString());
}

PrimeFaces Cookbook Showcase application

This recipe is available in the demo web application on GitHub (https://github.com/ova2/primefaces-cookbook/tree/second-edition). Clone the project if you have not done it yet, explore the project structure, and build and deploy the WAR file on application servers compatible with Servlet 3.x, such as JBoss WildFly and Apache TomEE.

The showcase for the recipe is available at http://localhost:8080/pf-cookbook/views/chapter9/mapMarkers.jsf.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.226.133.49