Google Maps KML Importing Utility

Select platform: Android iOS JavaScript
  1. Introduction
  2. Add a KML layer
  3. Clear a KML layer
  4. Access KML containers
  5. Access KML placemarks and KML ground overlays
  6. Access KML properties
  7. KML supported features

Introduction

KML is an extension of the XML data format and represents geographical data on a map. Using this utility, you can convert KML objects into geographical shapes and render them as a layer on top of a map. To add and remove your KML data to and from the map, call addLayerToMap() and removeLayerFromMap() respectively. To access properties in a KML object, call getProperties() on any Placemark, GroundOverlay, Document or Folder.

Add a KML layer to the map

To add a data layer to the map, first create an instance of the KmlLayer class. There are two ways to instantiate a KmlLayer.

To import and render a KML dataset from a local resource, you need:

  • A GoogleMap object where the layer is to be rendered.
  • A local resource file containing the KML data.
  • A Context object, which is required to open a local resource file.

Kotlin

val layer = KmlLayer(map, R.raw.geojson_file, context)

      

Java

KmlLayer layer = new KmlLayer(map, R.raw.geojson_file, context);

      

To import and render a KML dataset from a local stream, you need:

  • A GoogleMap object where the layer is to be rendered.
  • An InputStream containing the KML data.
  • A Context object, which is required to open local resources.

Kotlin

val inputStream: InputStream? =  // InputStream containing KML data
val layer = KmlLayer(map, inputStream, context)

      

Java

InputStream inputStream = // InputStream containing KML data
KmlLayer layer = new KmlLayer(map, inputStream, context);

      

After you have created a KmlLayer, call addLayerToMap()() to add the imported data onto the map.

Kotlin

layer.addLayerToMap()

      

Java

layer.addLayerToMap();

      

Clear a KML layer

Let's assume that you have created this KmlLayer:

Kotlin

val inputStream: InputStream? =  // InputStream containing KML data
val layer = KmlLayer(map, inputStream, context)

      

Java

InputStream inputStream = // InputStream containing KML data
KmlLayer layer = new KmlLayer(map, inputStream, context);

      

To remove the layer from the map, call removeLayerFromMap():

Kotlin

layer.removeLayerFromMap()

      

Java

layer.removeLayerFromMap();

      

Access KML containers

To access any containers that have been added to your layer, you can call getContainers() on the layer that you have created. To check if any container has nested containers, you can call hasContainers(). To access these nested containers, similar to what you are able to do in your layer, you can call getContainers()

To access containers which are not nested in a KmlLayer or KmlContainer:

Kotlin

for (containers in layer.containers) {
    // Do something to container
}

      

Java

for (KmlContainer containers : layer.getContainers()) {
    // Do something to container
}

      

To access containers which are nested in a KmlLayer or KmlContainer:

Kotlin

fun accessContainers(containers: Iterable<KmlContainer>) {
    for (container in containers) {
        if (container.hasContainers()) {
            accessContainers(container.containers)
        }
    }

      

Java

public void accessContainers(Iterable<KmlContainer> containers) {
    for (KmlContainer container : containers) {
        if (container.hasContainers()) {
            accessContainers(container.getContainers());
        }
    }
}

      

Access KML placemarks and KML ground overlays

To access any placemark or ground overlay that has been added to the layer, you can call getPlacemarks() or getGroundOverlays() on a layer or container. Calling either will return an iterable of KmlPlacemarks or KmlGroundOverlays respectively.

For example, to access a KmlPlacemark objects from a layer:

Kotlin

for (placemark in layer.placemarks) {
    // Do something to Placemark
}

      

Java

for (KmlPlacemark placemark : layer.getPlacemarks()) {
    // Do something to Placemark
}

      

Access KML properties

To access any property in a container or placemark, call getProperty() and give it a property key. You can also call hasProperty() to check if it exists. This sample shows how to retrieve the property value "name" from a container, if it exists.

Kotlin

for (container in layer.containers) {
    if (container.hasProperty("name")) {
        Log.i("KML", container.getProperty("name"))
    }
}

      

Java

for (KmlContainer container : layer.getContainers()) {
    if (container.hasProperty("name")) {
        Log.i("KML", container.getProperty("name"));
    }
}

      

KML Geometry Click Events

You can use KmlLayer.OnFeatureClickListener() to listen for click events on the geometry features on the map. The following example logs the ID of a feature when the user clicks the feature:

Kotlin

// Set a listener for geometry clicked events.
layer.setOnFeatureClickListener { feature ->
    Log.i(
        "KML",
        "Feature clicked: " + feature.id
    )
}

      

Java

// Set a listener for geometry clicked events.
layer.setOnFeatureClickListener(new KmlLayer.OnFeatureClickListener() {
    @Override
    public void onFeatureClick(Feature feature) {
        Log.i("KML", "Feature clicked: " + feature.getId());
    }
});

      

See the demo app

For an example of importing a KML file and creating a layer with it, take a look at the KmlDemoActivity in the demo app that ships with the utility library. The setup guide shows you how to run the demo app.

KML supported features

KML Element Supported? Comment
<address> partially Stored as a property value
<AddressDetails> no
<Alias> no
<altitude> no
<altitudeMode> no
<atom:author> no
<atom:link> no
<atom:name> no
<BalloonStyle> partially only <text> is supported
<begin> N/A <TimeSpan> is not supported
<bgColor> no
<bottomFov> N/A <PhotoOverlay> is not supported
<Camera> no
<Change> partially only style changes are supported
<color> partially includes #AABBGGRR and #BBGGRR; not supported in <ScreenOverlay>, and <GroundOverlay>
<colorMode> yes
<cookie> no <NetworkLinkControl> not supported
<coordinates> yes
<Create> no
<Data> no
<Delete> no
<description> yes Plain text only, no HTML content is supported
<displayMode> no
<displayName> no
<Document> yes
<drawOrder> yes
<east> yes
<end> N/A <TimeSpan> is not supported
<expires> no <NetworkLinkControl> not supported
<ExtendedData> partially untyped <Data> only, no <SimpleData> or <Schema>, and entity replacements of the form$[dataName] are unsupported.
<extrude> no
<fill> yes
<flyToView> no <NetworkLinkControl> not supported
<Folder> yes
<gridOrigin> N/A <PhotoOverlay> is not supported
<GroundOverlay> yes
<heading> yes
<hotSpot> yes
<href> yes
<httpQuery> no
<Icon> yes
<IconStyle> yes
<ImagePyramid> N/A <PhotoOverlay> is not supported
<innerBoundaryIs> yes implicitly from <LinearRing> order
<ItemIcon> N/A <ListStyle> is not supported
<key> yes
<kml> yes
<LabelStyle> no
<latitude> yes
<LatLonAltBox> no
<LatLonBox> yes
<leftFov> N/A <PhotoOverlay> is not supported
<LinearRing> yes
<LineString> yes
<LineStyle> yes
<Link> no
<linkDescription> N/A <NetworkLinkControl> not supported
<linkName> N/A <NetworkLinkControl> not supported
<linkSnippet> N/A <NetworkLinkControl> not supported
<listItemType> N/A <ListStyle> is not supported
<ListStyle> no
<Location> N/A <Model> is not supported
<Lod> yes
<longitude> yes
<LookAt> no
<maxAltitude> no
<maxFadeExtent> no
<maxHeight> N/A <PhotoOverlay> is not supported
<maxLodPixels> no
<maxSessionLength> no
<maxWidth> N/A <PhotoOverlay> is not supported
<message> no
<minAltitude> no
<minFadeExtent> no
<minLodPixels> no
<minRefreshPeriod> no <NetworkLink>
<Model> no
<MultiGeometry> yes
<name> yes
<near> N/A <PhotoOverlay> is not supported
<NetworkLink> no
<NetworkLinkControl> no
<north> yes
<open> yes Stored as a property value
<Orientation> N/A <Model> is not supported
<outerBoundaryIs> yes implicitly from <LinearRing> order
<outline> yes
<overlayXY> no
<Pair> yes
<phoneNumber> partially Stored as a property value
<PhotoOverlay> no
<Placemark> yes
<Point> yes
<Polygon> yes
<PolyStyle> yes
<range> yes
<refreshInterval> no
<refreshMode> no
<refreshVisibility> no
<Region> yes
<ResourceMap> N/A <Model> is not supported
<rightFov> N/A <PhotoOverlay> is not supported
<roll> N/A <Camera> and <Model> are not supported
<rotation> yes
<rotationXY> no
<Scale> N/A <Model> is not supported
<scale> yes
<Schema> no
<SchemaData> no
<ScreenOverlay> no
<screenXY> N/A <ScreenOverlay> is not supported
<shape> N/A <PhotoOverlay> is not supported
<SimpleData> N/A <SchemaData> are not supported
<SimpleField> N/A <Schema> are not supported
<size> yes
<Snippet> no
<south> yes
<state> N/A <ListStyle> is not supported
<Style> yes
<StyleMap> partially Highlighted style not supplied. Inline StyleMaps not supported
<styleUrl> yes
<targetHref> no <Alias> is not supported
<tessellate> no
<text> yes
<textColor> no
<tileSize> N/A <PhotoOverlay> is not supported
<tilt> no
<TimeSpan> no
<TimeStamp> no
<topFov> N/A <PhotoOverlay> is not supported
<Update> N/A <NetworkLinkControl< not supported
<value> yes
<viewBoundScale> no
<viewFormat> no
<viewRefreshMode> no
<viewRefreshTime> no
<ViewVolume> N/A <PhotoOverlay> is not supported
<visibility> yes
<west> yes
<when> N/A <TimeStamp> is not supported
<width> yes