OSMap embeds an OpenStreetMap into your page. All you have to know is the GPS coordinate of the center of the map. Based on Leaflet by Volodymyr Agafonkin and OpenStreetMap.
A map needs the GPS coordinates of its center and a zoom level.
The stateChange event fires when the map is zoomed or moved, the pointSelect event when a point is clicked. Zoom, drag or click the map.
The map model can also hold shapes: polylines, polygons, circles and rectangles.
<style>
/* the map tiles follow the color scheme of the page (data-dark on html, set by the theme configurator) */
html[data-dark="true"] .leaflet-tile-pane {
filter: invert(1) hue-rotate(180deg) brightness(0.95) contrast(0.9);
}
</style>
<div class="card">
<h5>Basic usage</h5>
<p>A map needs the GPS coordinates of its <code>center</code> and a <code>zoom</code> level.</p>
<h:form id="formBasicUsage">
<pe:osmap style="width:100%; height:500px;"
center="42.3402,-3.6972"
zoom="17"/>
</h:form>
</div>
<div class="card">
<h5>Event usage</h5>
<p>The <i>stateChange</i> event fires when the map is zoomed or moved, the <i>pointSelect</i> event when a point is clicked. Zoom, drag or click the map.</p>
<h:form id="formEventUsage">
<p:growl id="growl" showDetail="true" showSummary="true" globalOnly="false"/>
<pe:osmap style="width:100%; height:500px;"
center="42.3402,-3.6972"
zoom="17">
<p:ajax event="stateChange" listener="#{eventView.onStateChange}" update="growl"/>
<p:ajax event="pointSelect" listener="#{eventView.onPointSelect}" update="growl"/>
</pe:osmap>
</h:form>
</div>
<div class="card">
<h5>Shape Usage</h5>
<p>The map model can also hold shapes: polylines, polygons, circles and rectangles.</p>
<h:form id="formShapeUsage">
<pe:osmap style="width:100%; height:500px;"
center="36.890257,30.707417"
zoom="13" model="#{shapesView.shapeModel}"/>
</h:form>
</div>
package org.primefaces.extensions.showcase.controller.osmap;
import java.io.Serial;
import java.io.Serializable;
import jakarta.enterprise.context.RequestScoped;
import jakarta.faces.application.FacesMessage;
import jakarta.faces.context.FacesContext;
import jakarta.inject.Named;
import org.primefaces.event.map.PointSelectEvent;
import org.primefaces.event.map.StateChangeEvent;
import org.primefaces.model.map.LatLng;
import org.primefaces.model.map.LatLngBounds;
@Named
@RequestScoped
public class EventView implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
public void onStateChange(StateChangeEvent event) {
LatLngBounds bounds = event.getBounds();
int zoomLevel = event.getZoomLevel();
addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO, "Zoom Level", String.valueOf(zoomLevel)));
addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO, "Center", event.getCenter().toString()));
addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO, "NorthEast", bounds.getNorthEast().toString()));
addMessage(new FacesMessage(FacesMessage.SEVERITY_INFO, "SouthWest", bounds.getSouthWest().toString()));
}
public void onPointSelect(PointSelectEvent event) {
LatLng latlng = event.getLatLng();
FacesContext.getCurrentInstance().addMessage(null, new FacesMessage(FacesMessage.SEVERITY_INFO, "Point Selected",
"Lat:" + latlng.getLat() + ", Lng:" + latlng.getLng()));
}
public void addMessage(FacesMessage message) {
FacesContext.getCurrentInstance().addMessage(null, message);
}
}
package org.primefaces.extensions.showcase.controller.osmap;
import java.io.Serial;
import java.io.Serializable;
import jakarta.annotation.PostConstruct;
import jakarta.enterprise.context.RequestScoped;
import jakarta.inject.Named;
import org.primefaces.model.map.Circle;
import org.primefaces.model.map.DefaultMapModel;
import org.primefaces.model.map.LatLng;
import org.primefaces.model.map.LatLngBounds;
import org.primefaces.model.map.MapModel;
import org.primefaces.model.map.Polygon;
import org.primefaces.model.map.Polyline;
import org.primefaces.model.map.Rectangle;
@Named
@RequestScoped
public class ShapesView implements Serializable {
@Serial
private static final long serialVersionUID = 1L;
private MapModel<Long> shapeModel;
@PostConstruct
public void init() {
shapeModel = 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);
LatLng coord5 = new LatLng(36.90, 30.667648);
LatLng coord6 = new LatLng(36.909466, 30.697648);
LatLng coord7 = new LatLng(36.90, 30.707648);
LatLng ne = new LatLng(36.899466, 30.667648);
LatLng sw = new LatLng(36.890233, 30.707648);
// Polyline
Polyline<Long> polyline = new Polyline<>();
polyline.setData(1L);
polyline.getPaths().add(coord1);
polyline.getPaths().add(coord2);
polyline.getPaths().add(coord3);
polyline.getPaths().add(coord4);
polyline.setStrokeWeight(10);
polyline.setStrokeColor("#FF9900");
polyline.setStrokeOpacity(0.7);
shapeModel.addOverlay(polyline);
// Circle
Circle<Long> circle1 = new Circle<>(coord1, 500);
circle1.setStrokeColor("#d93c3c");
circle1.setFillColor("#d93c3c");
circle1.setFillOpacity(0.5);
circle1.setData(1L);
Circle<Long> circle2 = new Circle<>(coord4, 300);
circle2.setStrokeColor("#00ff00");
circle2.setFillColor("#00ff00");
circle2.setStrokeOpacity(0.7);
circle2.setFillOpacity(0.7);
circle2.setData(2L);
shapeModel.addOverlay(circle1);
shapeModel.addOverlay(circle2);
// Polygon
Polygon<Long> polygon = new Polygon<>();
polygon.setData(8L);
polygon.getPaths().add(coord5);
polygon.getPaths().add(coord6);
polygon.getPaths().add(coord7);
polygon.setStrokeColor("#FF0000");
polygon.setFillColor("#FF0000");
polygon.setStrokeOpacity(0.7);
polygon.setFillOpacity(0.7);
shapeModel.addOverlay(polygon);
// Rectangle
Rectangle<Long> rect = new Rectangle(new LatLngBounds(sw, ne));
rect.setData(7L);
rect.setStrokeColor("#0000ff");
rect.setFillColor("#0000ff");
rect.setFillOpacity(0.5);
shapeModel.addOverlay(rect);
}
public MapModel<Long> getShapeModel() {
return shapeModel;
}
}