{section}
{column:width=70%}
h2. Java API for KML - Advanced Example1
{toc:minLevel=3|maxLevel=5|type=flat}
h3. The concept
This series of more sophisticated JAK examples is trying to show the capabilities what is possible with KML in Java. They show the usage of JAK for generating some nice-looking KML files, which can be viewed with Google Earth.
{info:title=Important!}
JAK only works if all dependencies are resolved! That are mainly [JAK|http://code.google.com/p/javaapiforkml/] and [JAXB|https://jaxb.dev.java.net/]! For further instructions check out the [jak:FAQ].
{info}
h3. Google's Chart API used by JAK
This example generates a KML file with placemarks and charts for each continent. The chart shows the perentual coverage of the continent's landmass and it is generated with [Google's Chart API|http://chart.apis.google.com].
!jakdemo1.png!
h6. Figure 1: Placemarks with a chart of the surface of the earth for each continent.
h3. Get statistic data and generate charts
The statistical data is gathered from [Wikipedia|http://en.wikipedia.org/wiki/Continent] and additionally shown in Table 1.
||Continent||LatLon||Percent of total landmass||
|[Asia|http://en.wikipedia.org/wiki/Asia]| 93.24607775062842, 47.49808862281773|29.5%|
|[Africa|http://en.wikipedia.org/wiki/Africa]| 19.44601806124206, 10.13133611111111 |20.4%|
|[North America|http://en.wikipedia.org/wiki/North_America]| -103.5286299241638, 41.26035225962401|16.5%|
|[South America|http://en.wikipedia.org/wiki/South_America]| 59.96161780270248, -13.27347674076888|12.0%|
|[Europe|http://en.wikipedia.org/wiki/Europe] |14.45531426360271, 47.26208181151567|6.8%|
|[Australia|http://en.wikipedia.org/wiki/Australia_(continent)]| 135.0555272486322, -26.23824399654937|5.9%|
h6. Table 1: The land mass coverage in percent of each continent.
Listing 1 shows the structure of the "Create-A-Pie-Chart-Of-The-Land_Coverage_of_Each_Continent"TM. It uses a solely for this example created helper function called {{createPlacemarkWithChart ()}}. It is a convenience method to save a lot of boilerplate code and it is used to create the different placemarks filled with the statistical data gathered in Table 1. The structure of {{createPlacemarkWithChart ()}} is shown below in Listing 2.
{anchor:generate_chart}
{code:title=Listing 1: Generates Placemarks with a chart of the surface of the earth for each continent. }
final Kml kml = new Kml();
Document doc = kml.createAndSetDocument().withName("JAK Example1").withOpen(true);
// create a Folder
Folder folder = doc.createAndAddFolder();
folder.withName("Continents with Earth's surface").withOpen(true);
// create Placemark elements
createPlacemarkWithChart(doc, folder, 93.24607775062842, 47.49808862281773, "Asia", 30);
createPlacemarkWithChart(doc, folder, 19.44601806124206, 10.13133611111111, "Africa", 20);
createPlacemarkWithChart(doc, folder, -103.5286299241638, 41.26035225962401, "North America", 17);
createPlacemarkWithChart(doc, folder, -59.96161780270248, -13.27347674076888, "South America", 12);
createPlacemarkWithChart(doc, folder, 14.45531426360271, 47.26208181151567, "Europe", 7);
createPlacemarkWithChart(doc, folder, 135.0555272486322, -26.23824399654937, "Australia", 6);
// print and save
kml.marshal(new File("advancedexample1.kml"));
{code}
The {{createPlacemarkWithChart ()}}-method generates and set a placemark object, with the given statistical data . The Icon and Style objects (color and size of the text and icon) are saved to the root element. The placemark is created and set to the given folder.
{code:title=Listing 2: The createPlacemarkWithChart()-method}
/**
* @param document structure of the KML file
* @param folder to add continent
* @param longitude of the continent
* @param latitude of the continent
* @param continentName or name of the placemark
* @param coveredLandmass in percent
*/
private static void createPlacemarkWithChart(Document document, Folder folder, double longitude, double latitude,
String continentName, int coveredLandmass) {
int remainingLand = 100 - coveredLandmass;
Icon icon = new Icon()
.withHref("http://chart.apis.google.com/chart?chs=380x200&chd=t:" + coveredLandmass + "," + remainingLand + "&cht=p&chf=bg,s,ffffff00");
Style style = document.createAndAddStyle();
style.withId("style_" + continentName) // set the stylename to use this style from the placemark
.createAndSetIconStyle().withScale(5.0).withIcon(icon); // set size and icon
style.createAndSetLabelStyle().withColor("ff43b3ff").withScale(5.0); // set color and size of the continent name
Placemark placemark = folder.createAndAddPlacemark();
// use the style for each continent
placemark.withName(continentName)
.withStyleUrl("#style_" + continentName)
// 3D chart imgae
.withDescription(
"<![CDATA[<img src=\"http://chart.apis.google.com/chart?chs=430x200&chd=t:" + coveredLandmass + "," + remainingLand + "&cht=p3&chl=" + continentName + "|remaining&chtt=Earth's surface\" />")
// coordinates and distance (zoom level) of the viewer
.createAndSetLookAt().withLongitude(longitude).withLatitude(latitude).withAltitude(0).withRange(12000000);
placemark.createAndSetPoint().addToCoordinates(longitude, latitude); // set coordinates
}
{code}
The full example is contained in JAK's sources ({{/JavaAPIforKML/src/test/java/de/micromata/jak/examples/Example1.java}}).
The resulting KML-file can be downloaded here: [advancedexample1.kml|^advancedexample1.kml|Shows the usage of KML and Google's Chart API].
{column}
{column:width=1%}
{column}
{column:width=29%}
{livesearch:id=1|spaceKey=jak}
[Java API for KML|Home] | [@Google|http://code.google.com/p/javaapiforkml/]
{pagetree}
{column}
{section}
{column:width=70%}
h2. Java API for KML - Advanced Example1
{toc:minLevel=3|maxLevel=5|type=flat}
h3. The concept
This series of more sophisticated JAK examples is trying to show the capabilities what is possible with KML in Java. They show the usage of JAK for generating some nice-looking KML files, which can be viewed with Google Earth.
{info:title=Important!}
JAK only works if all dependencies are resolved! That are mainly [JAK|http://code.google.com/p/javaapiforkml/] and [JAXB|https://jaxb.dev.java.net/]! For further instructions check out the [jak:FAQ].
{info}
h3. Google's Chart API used by JAK
This example generates a KML file with placemarks and charts for each continent. The chart shows the perentual coverage of the continent's landmass and it is generated with [Google's Chart API|http://chart.apis.google.com].
!jakdemo1.png!
h6. Figure 1: Placemarks with a chart of the surface of the earth for each continent.
h3. Get statistic data and generate charts
The statistical data is gathered from [Wikipedia|http://en.wikipedia.org/wiki/Continent] and additionally shown in Table 1.
||Continent||LatLon||Percent of total landmass||
|[Asia|http://en.wikipedia.org/wiki/Asia]| 93.24607775062842, 47.49808862281773|29.5%|
|[Africa|http://en.wikipedia.org/wiki/Africa]| 19.44601806124206, 10.13133611111111 |20.4%|
|[North America|http://en.wikipedia.org/wiki/North_America]| -103.5286299241638, 41.26035225962401|16.5%|
|[South America|http://en.wikipedia.org/wiki/South_America]| 59.96161780270248, -13.27347674076888|12.0%|
|[Europe|http://en.wikipedia.org/wiki/Europe] |14.45531426360271, 47.26208181151567|6.8%|
|[Australia|http://en.wikipedia.org/wiki/Australia_(continent)]| 135.0555272486322, -26.23824399654937|5.9%|
h6. Table 1: The land mass coverage in percent of each continent.
Listing 1 shows the structure of the "Create-A-Pie-Chart-Of-The-Land_Coverage_of_Each_Continent"TM. It uses a solely for this example created helper function called {{createPlacemarkWithChart ()}}. It is a convenience method to save a lot of boilerplate code and it is used to create the different placemarks filled with the statistical data gathered in Table 1. The structure of {{createPlacemarkWithChart ()}} is shown below in Listing 2.
{anchor:generate_chart}
{code:title=Listing 1: Generates Placemarks with a chart of the surface of the earth for each continent. }
final Kml kml = new Kml();
Document doc = kml.createAndSetDocument().withName("JAK Example1").withOpen(true);
// create a Folder
Folder folder = doc.createAndAddFolder();
folder.withName("Continents with Earth's surface").withOpen(true);
// create Placemark elements
createPlacemarkWithChart(doc, folder, 93.24607775062842, 47.49808862281773, "Asia", 30);
createPlacemarkWithChart(doc, folder, 19.44601806124206, 10.13133611111111, "Africa", 20);
createPlacemarkWithChart(doc, folder, -103.5286299241638, 41.26035225962401, "North America", 17);
createPlacemarkWithChart(doc, folder, -59.96161780270248, -13.27347674076888, "South America", 12);
createPlacemarkWithChart(doc, folder, 14.45531426360271, 47.26208181151567, "Europe", 7);
createPlacemarkWithChart(doc, folder, 135.0555272486322, -26.23824399654937, "Australia", 6);
// print and save
kml.marshal(new File("advancedexample1.kml"));
{code}
The {{createPlacemarkWithChart ()}}-method generates and set a placemark object, with the given statistical data . The Icon and Style objects (color and size of the text and icon) are saved to the root element. The placemark is created and set to the given folder.
{code:title=Listing 2: The createPlacemarkWithChart()-method}
/**
* @param document structure of the KML file
* @param folder to add continent
* @param longitude of the continent
* @param latitude of the continent
* @param continentName or name of the placemark
* @param coveredLandmass in percent
*/
private static void createPlacemarkWithChart(Document document, Folder folder, double longitude, double latitude,
String continentName, int coveredLandmass) {
int remainingLand = 100 - coveredLandmass;
Icon icon = new Icon()
.withHref("http://chart.apis.google.com/chart?chs=380x200&chd=t:" + coveredLandmass + "," + remainingLand + "&cht=p&chf=bg,s,ffffff00");
Style style = document.createAndAddStyle();
style.withId("style_" + continentName) // set the stylename to use this style from the placemark
.createAndSetIconStyle().withScale(5.0).withIcon(icon); // set size and icon
style.createAndSetLabelStyle().withColor("ff43b3ff").withScale(5.0); // set color and size of the continent name
Placemark placemark = folder.createAndAddPlacemark();
// use the style for each continent
placemark.withName(continentName)
.withStyleUrl("#style_" + continentName)
// 3D chart imgae
.withDescription(
"<![CDATA[<img src=\"http://chart.apis.google.com/chart?chs=430x200&chd=t:" + coveredLandmass + "," + remainingLand + "&cht=p3&chl=" + continentName + "|remaining&chtt=Earth's surface\" />")
// coordinates and distance (zoom level) of the viewer
.createAndSetLookAt().withLongitude(longitude).withLatitude(latitude).withAltitude(0).withRange(12000000);
placemark.createAndSetPoint().addToCoordinates(longitude, latitude); // set coordinates
}
{code}
The full example is contained in JAK's sources ({{/JavaAPIforKML/src/test/java/de/micromata/jak/examples/Example1.java}}).
The resulting KML-file can be downloaded here: [advancedexample1.kml|^advancedexample1.kml|Shows the usage of KML and Google's Chart API].
{column}
{column:width=1%}
{column}
{column:width=29%}
{livesearch:id=1|spaceKey=jak}
[Java API for KML|Home] | [@Google|http://code.google.com/p/javaapiforkml/]
{pagetree}
{column}
{section}