Google Maps Android GeoJSON Utility

Select platform: Android iOS JavaScript
  1. Introduction
  2. Add a GeoJsonLayer to your map
  3. Remove the GeoJsonLayer
  4. Add and remove a GeoJsonFeature
  5. Access the GeoJsonFeatures and their properties
  6. Style the GeoJsonLayer and GeoJsonFeatures
  7. See the demo app

Introduction

GeoJSON is an extension of the JSON data format and represents geographical data. Using this utility, you can store geographical features in GeoJSON format and render them as a layer on top of the map. To add and remove your GeoJSON data to and from the map, call addLayerToMap() and removeLayerFromMap() respectively. Similarly you can add and remove individual features by calling addFeature() and removeFeature() and passing in a GeoJsonFeature object. If you want to access the features, you can call getFeatures() to get an iterable of all GeoJsonFeature objects that have been added to the layer.

You can also set default styles to be applied to features before they are added to the layer, by calling getDefaultPointStyle(), getDefaultLineStringStyle() or getDefaultPolygonStyle() and setting style options on each. Alternatively, you can set the style for an individual GeoJsonFeature by calling setPointStyle(), setLineStringStyle() or setPolygonStyle() on the feature and passing in the relevant style object.

Add a GeoJsonLayer to your map

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

To import from a JSONObject, you need the following:

  • GoogleMap object where the layer is to be rendered
  • JSONObject containing the GeoJSON data to be added to the layer

Kotlin

val geoJsonData: JSONObject? = // JSONObject containing the GeoJSON data
val layer = GeoJsonLayer(map, geoJsonData)

      

Java

JSONObject geoJsonData = // JSONObject containing the GeoJSON data
GeoJsonLayer layer = new GeoJsonLayer(map, geoJsonData);

      

To import from a local GeoJSON file you need the following:

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

Kotlin

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

      

Java

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

      

After you have created the GeoJsonLayer, call addLayerToMap() to add the imported data onto the map:

Kotlin

layer.addLayerToMap()

      

Java

layer.addLayerToMap();

      

Remove the GeoJsonLayer

Let’s assume that you have added this layer

Kotlin

val geoJsonData: JSONObject? = // JSONObject containing the GeoJSON data
val layer = GeoJsonLayer(map, geoJsonData)

      

Java

JSONObject geoJsonData = // JSONObject containing the GeoJSON data
GeoJsonLayer layer = new GeoJsonLayer(map, geoJsonData);

      

To clear the GeoJsonLayer, call removeLayerFromMap()

Kotlin

layer.removeLayerFromMap()

      

Java

layer.removeLayerFromMap();

      

Add and remove a GeoJsonFeature

A feature in GeoJSON has the type "feature". It contains a geometry, a property member and optionally has a bounding box or an id.

You can create GeoJsonFeature objects individually, and add them to the GeoJsonLayer.

Let's assume that you've created a feature containing a point at 0, 0 with one entry in its properties and no bounding box.

Kotlin

val point = GeoJsonPoint(LatLng(0.0, 0.0))
val properties = hashMapOf("Ocean" to "South Atlantic")
val pointFeature = GeoJsonFeature(point, "Origin", properties, null)

      

Java

GeoJsonPoint point = new GeoJsonPoint(new LatLng(0, 0));
HashMap<String, String> properties = new HashMap<>();
properties.put("Ocean", "South Atlantic");
GeoJsonFeature pointFeature = new GeoJsonFeature(point, "Origin", properties, null);

      

To add the feature to the layer, call addFeature() and pass in the feature to add.

Kotlin

layer.addFeature(pointFeature)

      

Java

layer.addFeature(pointFeature);

      

To remove a feature after adding it to the layer, call removeFeature() and pass in the feature to remove.

Kotlin

layer.removeFeature(pointFeature)

      

Java

layer.removeFeature(pointFeature);

      

Access the GeoJsonFeatures and their properties

To access all the GeoJsonFeatures that have been added to the layer, you can call getFeatures() on the GeoJsonLayer that you have created. This will return an iterable of GeoJsonFeatures that you can access using a for-each loop as shown below.

Kotlin

for (feature in layer.features) {
    // Do something to the feature
}

      

Java

for (GeoJsonFeature feature : layer.getFeatures()) {
    // Do something to the feature
}

      

Use the hasProperty() and getProperty() methods in conjunction with the getFeatures() method to check if each stored feature has a particular property and access it if it exists.

Kotlin

if (feature.hasProperty("Ocean")) {
    val oceanProperty = feature.getProperty("Ocean")
}

      

Java

if (feature.hasProperty("Ocean")) {
    String oceanProperty = feature.getProperty("Ocean");
}

      

GeoJSON Geometry Click Events

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

Kotlin

// Set a listener for geometry clicked events.
layer.setOnFeatureClickListener { feature ->
    Log.i("GeoJsonClick", "Feature clicked: ${feature.getProperty("title")}")
}

      

Java

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

      

Style the GeoJsonLayer and GeoJsonFeatures

You can set default styles for a GeoJsonLayer, or style individual features in the layer.

Default Styles

In a GeoJsonLayer, you can set default styles for any points, linestrings and polygons that are added to the layer. Default styles are only applied if the feature does not have a style set for either of its geometries. Any changes you make on the default style will also be reflected in all features that are using the default style.

The steps to apply a default style are as follows

  1. Retrieve the relevant default style object, this can be one of a GeoJsonPointStyle, a GeoJsonLineStringStyle or a GeoJsonPolygonStyle.
  2. Apply your desired options on the style.

For example, the following code sample shows how to modify the default point style which will make points draggable with a title and snippet.

Kotlin

val pointStyle = layer.defaultPointStyle
pointStyle.isDraggable = true
pointStyle.title = "Hello, World!"
pointStyle.snippet = "I am a draggable marker"

      

Java

GeoJsonPointStyle pointStyle = layer.getDefaultPointStyle();
pointStyle.setDraggable(true);
pointStyle.setTitle("Hello, World!");
pointStyle.setSnippet("I am a draggable marker");

      

Styles specific to a GeoJsonFeature

Alternatively, you can style individual features in the layer. The steps to apply a style on a GeoJsonFeature are as follows.

  1. Create the relevant style object, this can be either a GeoJsonPointStyle, GeoJsonLineStringStyle or GeoJsonPolygonStyle.
  2. Apply your desired options on the style.
  3. Pass the style object to the relevant method on the GeoJsonFeature, which will be either setPointStyle(), setLineStringStyle() or setPolygonStyle().

For example, this is how to customise the linestring style for a GeoJsonFeature so that its color is red.

Kotlin

// Create a new feature containing a linestring
val lineStringArray: MutableList<LatLng> = ArrayList()
lineStringArray.add(LatLng(0.0, 0.0))
lineStringArray.add(LatLng(50.0, 50.0))
val lineString = GeoJsonLineString(lineStringArray)
val lineStringFeature = GeoJsonFeature(lineString, null, null, null)

// Set the color of the linestring to red
val lineStringStyle = GeoJsonLineStringStyle()
lineStringStyle.color = Color.RED

// Set the style of the feature
lineStringFeature.lineStringStyle = lineStringStyle

      

Java

// Create a new feature containing a linestring
List<LatLng> lineStringArray = new ArrayList<LatLng>();
lineStringArray.add(new LatLng(0, 0));
lineStringArray.add(new LatLng(50, 50));
GeoJsonLineString lineString = new GeoJsonLineString(lineStringArray);
GeoJsonFeature lineStringFeature = new GeoJsonFeature(lineString, null, null, null);

// Set the color of the linestring to red
GeoJsonLineStringStyle lineStringStyle = new GeoJsonLineStringStyle();
lineStringStyle.setColor(Color.RED);

// Set the style of the feature
lineStringFeature.setLineStringStyle(lineStringStyle);

      

See the demo app

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