Creating rectangles, circles, polylines, and polygons in maps

The gmap component supports the drawing of rectangles, circles, polylines, and polygons on the map canvas.

How to do it…

All drawings can be implemented as an instance of DefaultMapModel, as stated here:

private MapModel rectangleModel = new DefaultMapModel();
private MapModel circleModel = new DefaultMapModel();
private MapModel polylineModel = new DefaultMapModel();
private MapModel polygonModel = new DefaultMapModel();

All models contain instances of LatLng, where they define the points for the drawings. The rectangle model can be defined with two points, upper-left and lower-right, which are wrapped in an instance of LatLngBounds. This is shown in the following code:

rectangleModel.addOverlay(new Rectangle(new LatLngBounds(
  new LatLng(41.073399, 29.051971),
  new LatLng(41.118418, 29.134026))));

The circle model accepts a point and the radius value to be defined:

Circle circle =new Circle(new LatLng(41.073399, 29.051971), 5000);
circleModel.addOverlay(circle);

The polyline drawings can be defined with a list of points, as shown here:

Polyline polyline = new Polyline();
polyline.getPaths().add(new LatLng(41.073399, 29.051971));
polyline.getPaths().add(new LatLng(41.118418, 29.134026));
polyline.getPaths().add(new LatLng(41.027807, 29.049973));
polylineModel.addOverlay(polyline);

The polygon's definition is similar to the polyline's, but the output will be a closed drawing filled inside. The opacity of the filling can be configured with the setFillOpacity() method of the polygon model. This is shown in the following code:

Polygon polygon = new Polygon();
polygon.getPaths().add(new LatLng(41.073399, 29.051971));
polygon.getPaths().add(new LatLng(41.118418, 29.134026));
polygon.getPaths().add(new LatLng(41.027807, 29.049973));
polygonModel.addOverlay(polygon);

Binding these models to the <p:gmap> component separately will do the trick, as shown in the following screenshot:

How to do it…

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/mapDrawings.jsf.

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

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