JasperReports logo version 7.0.0 Home - Samples - Configuration - Functions - FAQ - API (Javadoc)

JasperReports - Map Component Sample

Illustrates the usage of the map component element.

Main Features in This Sample

Using the Built-in Map Component

Using the Built-in Map Component

Documented by Sanda Zaharia

Description / Goal
How to render maps using the built-in map component.

Since: 4.1.1

Google Map Component Overview

The map component in JasperReports exposes some of the main characteristics necessary to generate the most common Google maps, but feature upgrades for this component are planned for future development.

Important notes: 1. The JR built-in map component is based on the public Google Maps APIs licensed under the Creative Commons Attribution 3.0 License. Prior to start using this component, please consult both Google terms of service and Google site policies pages.
Ensure the map component in your application is used in accordance with these terms and conditions.

  1. In order to get accurate results with Google maps, you need to set the net.sf.jasperreports.components.map.key property with an appropriate Google API key value.
    The Google API key must be enabled for Google Javascript API, Google Maps Static API and Google Geocoding API. More information about API keys can be found at Google Maps Platform.
    The net.sf.jasperreports.components.map.key property can be set either in the report itself or per report context.

The current map component is characterized by the kind="googlemap" attribute and contains the following structure:

Map Attributes

One can customize the map component using the following attributes:

The Latitude Expression

The <latitudeExpression/> represents the latitude coordinate of the map center, necessary to locate it on the Earth surface. Allowed values are floating point numbers representing degrees from -90° to 90°.

The Longitude Expression

The <longitudeExpression/> represents the longitude coordinate of the map center, necessary to locate it on the Earth surface. Allowed values are floating point numbers representing degrees from -180° to 180°.

The Address Expression

The <addressExpression/> represents the address of the map center and is optional. It will be considered only when the latitude and/or longitude expressions are missing or empty. Computing address expressions relies on expensive time and resources consumption, therefore the report filling performance may visibly decline. It is highly recommended to use latitude and longitude expressions whenever possible.

The Zoom Expression

The <zoomExpression/> represents the initial map zoom level. The expression allows integer values and defaults to 0.

The Language Expression

The <languageExpression/> represents the language code used to localize the textual information displayed on the map, such as control names, copyright notices and other various labels. Supported languages can be found here.

The Marker Data Element

The <markerData/> element is used to generate the list of marker elements to be displayed on the map. It can collect its data either from a subdataset, or from a hardcoded list of marker item elements.

The marker item element contains a list of specific item properties that can be set either by value or by valueExpression. If both are set, the valueExpression takes precedence over the value attribute. With few exceptions, property names must match the names of supported marker options available here.

The main exception is the position option, currently undefined in JR. It is actually replaced by the latitude and longitude property names, both necessary to compute the marker position on the map. If at least one of these two properties is missing or empty, try to set the equivalent address property. If no address is available, an exception will be thrown.

Warnings:

To avoid such exceptions when using a subdataset, use a <filterExpression> to skip markers with empty latitude/longitude and address properties. A data filtering example can be found in the map component sample.

The intensive use of the address property is not recommended, because it will definitely slow the report generation. Try to use the latitude and longitude properties when they are available.

Below is a list of valid itemProperty names:

Custom Icons

Both icon and shadow properties indicate an image location, without any additional information. In this case the image is loaded as it is and is placed in built-in positions on the map, using Google Maps defaults. But sometimes, marker icons need more detailed configurations, in order to get a better map appearance. In this case one can build custom icons, with the following properties:

Info Windows

When a marker icon is clicked on the map, additional information can be displayed either in a separate window/frame or in an info window floating above the map. The text content in an info window is processed as HTML, therefore the HTML notation should be used for special characters (see reserved characters here.)

If both url and infoWindow settings are present in a marker definition, infoWindow takes precedence over the marker URL. Clicking on the marker icon will open the info window. To configure an info window, the following item properties are available:

Paths on Google Maps

Various locations on a given map can be connected to form a path. To configure a path on the map, the following information is needed:

The Path Data Element

Points that have to be connected in a path are grouped into the <pathData/> element. A path data element is very similar to the marker data element described above, but comes with a different set of item properties. Each item in the path data element represents a place on the map and must provide either the latitude and longitude, or the equivalent address property from the following list of allowed properties:

Notes:

The Path Style Element

In order to avoid redundant data, common style properties for a given path can be grouped in a style element, so that individual items (points) in the path have to remember only the name of the path style, instead of carrying several style properties by themselves. Path styles are grouped together into a <pathStyle/> element, very similar to <pathData/>, excepting the latitude and longitude properties that are not considered in a path style.

Below is the list of allowed properties in a path style, with the same meaning and defaults (excepting the name property) as in the path data element:

Notes:

Google Static Maps

Exporters that don't allow JavaScript or dynamic page loading may use the Google Static Map service that creates and returns a map image depending on a given set of HTTP request parameters. The map image does not support user interactivity, but can be used to substitute with some inherent limitations a dynamic map, for read-only purposes. In these conditions, not all item properties defined for markers are considered in static maps. The available properties in this case are:

If no size is set, the default icon is loaded with its normal size.
For similar reasons, path style properties considered in static maps are the following:

Map Component Examples

The JRXML sample contains five map components that illustrate how various settings can be applied in order to get the desired map rendering. Each map is loaded from a subreport and collect its data from the CSV datasource located in the data subfolder.

Map #1: Simple Road Map

The first map is loaded from the reports/MapReport1.jrxml template, and illustrates a road map with minimal settings. There are no markers and no additional information on the map:

<element kind="component" positionType="Float" y="255" width="515" height="326">
  <component kind="googlemap" evaluationTime="Report" onErrorType="Icon">
    <latitudeExpression><![CDATA[$P{latitude}]] ></latitudeExpression>
  <longitudeExpression><![CDATA[$P{longitude}]] ></longitudeExpression>
  <zoomExpression><![CDATA[$P{zoom}]] ></zoomExpression>
  <languageExpression><![CDATA[new java.util.Locale("ro").getLanguage()]] ></languageExpression>
  </component>
</element>

Map #2: Satellite Map with Default Marker Icons

The second map is loaded from the reports/MapReport2.jrxml template. One can notice the mapType="satellite" mapScale="1" imageType="jpg" attribute settings. The satellite map also contains a set of 5 markers with default icons and tooltips enabled. All marker settings are grouped in the <markerData/> section which collects its data from the MarkersDataset subdataset. Data with invalid latitude/longitude values are skipped due to the <filterExpression/> in the subdataset:

<dataset name="MarkersDataset">
  <property name="net.sf.jasperreports.data.adapter" value="/data/CsvDataAdapter.jrdax"/>
  <query language="csv"/>
  <field name="address" class="java.lang.String"/>
  <field name="latitude" class="java.lang.String"/>
  <field name="longitude" class="java.lang.String"/>
  <filterExpression>
    <![CDATA[!($F{latitude}==null || $F{latitude}.length() == 0 || $F{longitude}==null || $F{longitude}.length() == 0)]] >
  </filterExpression>
</dataset>

The map component configuration is the following:

<element kind="component" positionType="Float" y="150" width="515" height="326">
  <component kind="googlemap" mapType="satellite" imageType="jpg" onErrorType="Icon">
    <latitudeExpression><![CDATA[37.800056f]] ></latitudeExpression>
    <longitudeExpression><![CDATA[-122.4075f]] ></longitudeExpression>
    <zoomExpression><![CDATA[$P{zoom} + 1]] ></zoomExpression>
    <markerData>
    <dataset>
      <datasetRun subDataset="MarkersDataset"/>
    </dataset>
    <item>
      <property name="latitude">
        <expression><![CDATA[$F{latitude}]] ></expression>
      </property>
      <property name="longitude">
        <expression><![CDATA[$F{longitude}]] ></expression>
      </property>
      <property name="title">
        <expression><![CDATA[$F{address}]] ></expression>
      </property>
      <property name="label">
        <expression>
          <![CDATA[$V{REPORT_COUNT} % 5 < 2 ? ""+$V{REPORT_COUNT}*$V{REPORT_COUNT} :($V{REPORT_COUNT} == 2 ? "abc" : "M")]] >
        </expression>
      </property>
    </item>
  </markerData>
</component>
</element>

Map #3: Terrain Map with Custom Marker Icons

The 3rd map is a terrain sample loaded from the reports/MapReport3.jrxml template. It comes with the same set of markers as Map #2, but this time marker icons are loaded from custom URLs, with the specified width and height. Below is the map component configuration (and the subdataset):

<dataset name="MarkersDataset">
  <property name="net.sf.jasperreports.data.adapter" value="/data/CsvDataAdapter.jrdax"/>
  <query language="csv"/>
  <field name="address" class="java.lang.String"/>
  <field name="latitude" class="java.lang.String"/>
  <field name="longitude" class="java.lang.String"/>
  <field name="iconurl" class="java.lang.String"/>
  <field name="iconwidth" class="java.lang.Integer"/>
  <field name="iconheight" class="java.lang.Integer"/>
  <filterExpression>
    <![CDATA[!($F{latitude}==null || $F{latitude}.length() == 0 || $F{longitude}==null || $F{longitude}.length() == 0)]] >
  </filterExpression>
</dataset>
...
<element kind="component" positionType="Float" y="150" width="515" height="326">
  <component kind="googlemap" mapType="terrain" imageType="jpg" onErrorType="Icon">
    <latitudeExpression><![CDATA[37.800056f]] ></latitudeExpression>
    <longitudeExpression><![CDATA[-122.4075f]] ></longitudeExpression>
    <zoomExpression><![CDATA[$P{zoom} + 1]] ></zoomExpression>
    <markerData>
      <dataset>
        <datasetRun subDataset="MarkersDataset"/>
      </dataset>
      <item>
        <property name="latitude">
          <expression><![CDATA[$F{latitude}]] ></expression>
        </property>
        <property name="longitude">
          <expression><![CDATA[$F{longitude}]] ></expression>
        </property>
        <property name="title">
          <expression><![CDATA[$F{address}]] ></expression>
        </property>
        <property name="icon.url">
          <expression><![CDATA[$F{iconurl}]] ></expression>
        </property>
        <property name="icon.width">
          <expression><![CDATA[$F{iconwidth}]] ></expression>
        </property>
        <property name="icon.height">
          <expression><![CDATA[$F{iconheight}]] ></expression>
        </property>
      </item>
    </markerData>
  </component>
</element>

Map #4: Satellite Map with Marker Hyperlinks and Paths

In the 4th sample loaded from the reports/MapReport4.jrxml template the same set of markers with default icons are placed on a satellite map. This time marker icons display custom colors, and in static maps only, custom sizes. Marker tooltips are disabled, but markers provide hyperlinks. Clicking on a marker icon will open a new browser window/tab with related information. In addition, all markers are connected to form a green polygonal contour and two of them are also connected with a blue line.

Two additional subdatasets were added in order to generate path styles and path data.See the configuration below:

<dataset name="MarkersDataset">
  <property name="net.sf.jasperreports.data.adapter" value="/data/CsvDataAdapter.jrdax"/>
  <query language="csv"/>
  <field name="latitude" class="java.lang.String"/>
  <field name="longitude" class="java.lang.String"/>
  <field name="size" class="java.lang.String"/>
  <field name="color" class="java.lang.String"/>
  <field name="url" class="java.lang.String"/>
  <filterExpression>
    <![CDATA[!($F{latitude}==null || $F{latitude}.length() == 0 || $F{longitude}==null || $F{longitude}.length() == 0)]] >
  </filterExpression>
</dataset>
<dataset name="PathStyleDataset">
  <property name="net.sf.jasperreports.data.adapter" value="/data/PathStyleDataAdapter.jrdax"/>
  <query language="csv"/>
  <field name="name" class="java.lang.String"/>
  <field name="strokecolor" class="java.lang.String"/>
  <field name="strokeopacity" class="java.lang.Double"/>
  <field name="strokeweight" class="java.lang.Integer"/>
  <field name="fillcolor" class="java.lang.String"/>
  <field name="fillopacity" class="java.lang.Double"/>
  <field name="draggable" class="java.lang.Boolean"/>
  <filterExpression><![CDATA[$F{name}!=null && $F{name}.length() != 0]] ></filterExpression>
</dataset>
<dataset name="PathLocationDataset">
  <property name="net.sf.jasperreports.data.adapter" value="/data/PathLocationDataAdapter.jrdax"/>
  <query language="csv"/>
  <field name="name" class="java.lang.String"/>
  <field name="style" class="java.lang.String"/>
  <field name="latitude" class="java.lang.String"/>
  <field name="longitude" class="java.lang.String"/>
  <field name="ispolygon" class="java.lang.String"/>
  <filterExpression>
    <![CDATA[!($F{latitude}==null || $F{latitude}.length() == 0 || $F{longitude}==null || $F{longitude}.length() == 0)]] >
  </filterExpression>
</dataset>
...
<element kind="component" positionType="Float" y="150" width="515" height="326">
  <component kind="googlemap" mapType="satellite" imageType="jpg" onErrorType="Icon">
    <latitudeExpression><![CDATA[37.800056f]] ></latitudeExpression>
    <longitudeExpression><![CDATA[-122.4075f]] ></longitudeExpression>
    <zoomExpression><![CDATA[$P{zoom} + 1]] ></zoomExpression>
    <pathStyleList>
      <pathStyleList>
        <dataset>
          <datasetRun subDataset="PathStyleDataset"/>
        </dataset>
        <item>
          <property name="name">
            <expression><![CDATA[$F{name}]] ></expression>
          </property>
          <property name="strokeColor">
            <expression><![CDATA[$F{strokecolor}]] ></expression>
          </property>
          <property name="strokeOpacity">
            <expression><![CDATA[$F{strokeopacity}]] ></expression>
          </property>
          <property name="strokeWeight">
            <expression><![CDATA[$F{strokeweight}]] ></expression>
          </property>
          <property name="fillColor">
            <expression><![CDATA[$F{fillcolor}]] ></expression>
          </property>
          <property name="fillOpacity">
            <expression><![CDATA[$F{fillopacity}]] ></expression>
          </property>
          <property name="draggable">
            <expression><![CDATA[$F{draggable}]] ></expression>
          </property>
        </item>
      </pathStyleList>
    </pathStyleList>
    <pathDataList>
      <pathDataList>
        <dataset>
          <datasetRun subDataset="PathLocationDataset"/>
        </dataset>
        <item>
          <property name="name">
            <expression><![CDATA[$F{name}]] ></expression>
          </property>
          <property name="style">
            <expression><![CDATA[$F{style}]] ></expression>
          </property>
          <property name="latitude">
            <expression><![CDATA[$F{latitude}]] ></expression>
          </property>
          <property name="longitude">
            <expression><![CDATA[$F{longitude}]] ></expression>
          </property>
          <property name="isPolygon">
            <expression><![CDATA[$F{ispolygon}]] ></expression>
          </property>
          <property name="path.hyperlink">
            <expression><![CDATA["http://www.google.com"]] ></expression>
          </property>
        </item>
      </pathDataList>
    </pathDataList>
    <markerData>
      <dataset>
        <datasetRun subDataset="MarkersDataset"/>
      </dataset>
      <item>
        <property name="latitude">
          <expression><![CDATA[$F{latitude}]] ></expression>
        </property>
        <property name="longitude">
          <expression><![CDATA[$F{longitude}]] ></expression>
        </property>
        <property name="color">
          <expression><![CDATA[$F{color}]] ></expression>
        </property>
        <property name="size">
          <expression><![CDATA[$F{size}]] ></expression>
        </property>
        <property name="url">
          <expression><![CDATA[$F{url}]] ></expression>
        </property>
        <property name="target">
          <expression><![CDATA["_blank"]] ></expression>
        </property>
      </item>
    </markerData>
  </component>
</element>

Map #5: Hybrid Map with Custom Icons and Info Windows

The last sample is loaded from reports/MapReport5.jrxml template. Markers provide custom icons and tooltips. Clicking on a marker icon will open an info window with related information about the marker position. The value of the infowindow.content property is a HTML fragment with escaped open and end tags.

<dataset name="MarkersDataset">
  <property name="net.sf.jasperreports.data.adapter" value="/data/CsvDataAdapter.jrdax"/>
  <query language="csv"/>
  <field name="address" class="java.lang.String"/>
  <field name="latitude" class="java.lang.String"/>
  <field name="longitude" class="java.lang.String"/>
  <field name="iconurl" class="java.lang.String"/>
  <field name="iconwidth" class="java.lang.Integer"/>
  <field name="iconheight" class="java.lang.Integer"/>
  <filterExpression>
    <![CDATA[!($F{latitude}==null || $F{latitude}.length() == 0 || $F{longitude}==null || $F{longitude}.length() == 0)]] >
  </filterExpression>
</dataset>
...
<element kind="component" positionType="Float" y="150" width="515" height="326">
  <component kind="googlemap" mapType="hybrid" imageType="jpg" onErrorType="Icon">
    <latitudeExpression><![CDATA[37.800056f]] ></latitudeExpression>
    <longitudeExpression><![CDATA[-122.4075f]] ></longitudeExpression>
    <zoomExpression><![CDATA[$P{zoom} + 1]] ></zoomExpression>
    <markerData>
      <dataset>
        <datasetRun subDataset="MarkersDataset"/>
      </dataset>
      <item>
        <property name="latitude">
          <expression><![CDATA[$F{latitude}]] ></expression>
        </property>
        <property name="longitude">
          <expression><![CDATA[$F{longitude}]] ></expression>
        </property>
        <property name="title">
          <expression><![CDATA[$F{address}]] ></expression>
        </property>
        <property name="icon.url">
          <expression><![CDATA[$F{iconurl}]] ></expression>
        </property>
        <property name="icon.width">
          <expression><![CDATA[$F{iconwidth}]] ></expression>
        </property>
        <property name="icon.height">
          <expression><![CDATA[$F{iconheight}]] ></expression>
        </property>
        <property name="infowindow.content">
          <expression>
            <![CDATA["<p style='text-align:right;'><img src='https://jasperreports.sourceforge.net/jasperreports.png'/></p>
            <p style='text-align:left;'><b>"+$F{address}+"</b></p>"]] >
          </expression>
        </property>
      </item>
    </markerData>
  </component>
</element>

Running the Sample

Running the sample requires the Apache Maven library. Make sure that maven is already installed on your system (version 3.6 or later). In a command prompt/terminal window set the current folder to demo/samples/map within the JasperReports source project and run the following command:

> mvn clean compile exec:exec@all

It will generate all supported document types containing the sample report in the demo/samples/map/target/reports directory.




© 2001- Cloud Software Group, Inc. www.jaspersoft.com