Google Maps Multilayer Utility

  1. Introduction
  2. Adding multiple cluster, KML, and GeoJSON layers
  3. Adding your own features
  4. Handling click events
  5. See the demo app

Introduction

In previous tutorials, you’ve learned how to add features from KML and GeoJSON to your map, as well as clusters of markers. But what if you want to add several of these layers on the same map and get independent click events for each?

Adding multiple cluster, KML, and GeoJSON layers

The library includes Managerobjects to help manage click events for multiple types of layers. So, before you set up your layers you’ll first need to instantiate these and pass in your GoogleMap:

MarkerManager markerManager = new MarkerManager(getMap());
GroundOverlayManager groundOverlayManager = new GroundOverlayManager(getMap());
PolygonManager polygonManager = new PolygonManager(getMap());
PolylineManager polylineManager = new PolylineManager(getMap());

Next, you can pass these manager classes into the constructors of the other layers when you set them up:

ClusterManager<MyItem> clusterManager = new ClusterManager<>(this, getMap(), markerManager);
GeoJsonLayer geoJsonLineLayer = new GeoJsonLayer(getMap(), R.raw.south_london_line_geojson, this, markerManager, polygonManager, polylineManager, groundOverlayManager);
KmlLayer kmlPolylineLayer = new KmlLayer(getMap(), R.raw.south_london_line_kml, this, markerManager, polygonManager, polylineManager, groundOverlayManager, null);

Adding your own features

If you want to add your own markers, ground overlays, polylines, or polygons alongside these layers, simply create your own Collection and then use the Managers to add the feature instead of directly adding them to the GoogleMap object. For example, if you want to add a new marker:

MarkerManager.Collection markerCollection = markerManager.newCollection();
markerCollection.addMarker(new MarkerOptions()
        .position(new LatLng(51.150000, -0.150032))
        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE))
        .title("Unclustered marker"));

Handling click events

For clusters, KML, and GeoJSON, click listeners work like normal - as long as you pass in the Manager classes in the constructor of the layer you’re setting. For example, here’s how to set up a click listener for the KML layer:

kmlPolylineLayer.addLayerToMap();
kmlPolylineLayer.setOnFeatureClickListener(feature -> Toast.makeText(MultiLayerDemoActivity.this,
        "KML polyline clicked: " + feature.getProperty("name"),
        Toast.LENGTH_SHORT).show());

When you add your own markers, ground overlays, polylines, or polygons, just be sure to add click listeners to those Collection objects. For example, here’s how to set up the marker click listener on the markerCollection:

markerCollection.setOnMarkerClickListener(marker -> {
    Toast.makeText(MultiLayerDemoActivity.this,
            "Marker clicked: " + marker.getTitle(),
            Toast.LENGTH_SHORT).show();
    return false;
});

See the demo app

For an example of adding multiple layers, take a look at the MultiLayerDemoActivity in the demo app that ships with the utility library. The setup guide shows you how to run the demo app.