The KML reader and writer

There are two ways of reading KML/ KMZ files:
  • Using an IlvKMLReader instance directly. In this case, you must write all the code required to render the KML features into graphic objects, and then add them to the manager.
    This class reads KML features from a specified KML file or catalog. It implements the IlvMapFeatureIterator interface to iterate over the read features.
  • Using an IlvKMLDataSource. This is a convenient way of performing all the above operations at once and is more integrated with the data model of the map.
    The IlvKMLDataSource class provides a convenient way of creating a set of layers containing KML data in a manager.
The source code for the Map Builder demonstration, which contains all the code described in this section, can be found at <installdir> /jviews-maps810/samples/mapbuilder/index.html.
To read KML features and create vector data using the IlvKMLReader object:
  1. Create an IlvKMLReader instance from the path of the KML catalog:
    String KMLpath = " C:/maps/KML/places.kmz";
    IlvKMLReader reader = new IlvKMLReader(KMLpath);
    
  2. Create a default renderer:
    IlvFeatureRenderer renderer = new IlvDefaultFeatureRenderer();
    
  3. Iterate over the features, render them with an appropriate IlvFeatureRenderer, and assign them to a manager:
    IlvMapFeature feature = reader.getNextFeature();
    while(feature != null) {
      // Render map feature into the graphic object.
      IlvGraphic graphic = renderer.makeGraphic(feature,null);
      // Add this object on the first layer of the manager.
      manager.addObject(graphic, 0, false);
      feature = reader.getNextFeature();
    }
    
To read KML features and create vector data using the IlvKMLDataSource:
  1. Create an IlvKMLDataSource :
    String KMLpath = "C:/maps/KML_0606_ed8/KMLT";
    IlvKMLDataSource source = new IlvKMLDataSource(KMLpath); 
    
  2. Connect this data source to the manager of the view:
    source.setManager(getView().getManager());
    
  3. Start the KML data source:
    source.start();
    
Note
JViews Maps does not support KML styles. This is because JViews Maps styles are made for layers whereas KML styles are made for individual objects and are therefore not compatible.