Select platform: [Android](https://developers.google.com/maps/documentation/android-sdk/utility/geojson.md.txt "View this page for the Android platform docs.") [iOS](https://developers.google.com/maps/documentation/ios-sdk/utility/geojson.md.txt "View this page for the iOS platform docs.") [JavaScript](https://developers.google.com/maps/documentation/javascript/datalayer.md.txt "View this page for the JavaScript platform docs.")

1. [Introduction](https://developers.google.com/maps/documentation/android-sdk/utility/geojson.md.txt#introduction)
2. [Add a
   GeoJsonLayer to your map](https://developers.google.com/maps/documentation/android-sdk/utility/geojson#add)
3. [Remove the
   GeoJsonLayer](https://developers.google.com/maps/documentation/android-sdk/utility/geojson#remove)
4. [Add and remove a
   GeoJsonFeature](https://developers.google.com/maps/documentation/android-sdk/utility/geojson#feature)
5. [Access the GeoJsonFeatures
   and their properties](https://developers.google.com/maps/documentation/android-sdk/utility/geojson#properties)
6. [Style the GeoJsonLayer and
   GeoJsonFeatures](https://developers.google.com/maps/documentation/android-sdk/utility/geojson#style)
7. [See the demo app](https://developers.google.com/maps/documentation/android-sdk/utility/geojson.md.txt#demo-app)

## Introduction

[GeoJSON](http://geojson.org/) 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

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

      
```

### Java

```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

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

      
```

### Java

```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

```kotlin
layer.addLayerToMap()

      
```

### Java

```java
layer.addLayerToMap();

      
```

## Remove the GeoJsonLayer

Let's assume that you have added this layer

### Kotlin

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

      
```

### Java

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

      
```

To clear the GeoJsonLayer, call `removeLayerFromMap()`

### Kotlin

```kotlin
it.removeLayerFromMap()

      
```

### Java

```java
geoJsonLayer.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

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

      
```

### Java

```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

```kotlin
layer.addFeature(pointFeature)

      
```

### Java

```java
layer.addFeature(pointFeature);

      
```

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

### Kotlin

```kotlin
layer.removeFeature(pointFeature)

      
```

### Java

```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

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

      
```

### Java

```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

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

      
```

### Java

```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

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

      
```

### Java

```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

```kotlin
val pointStyle = layer.getDefaultPointStyle()
pointStyle.setDraggable(true)
pointStyle.setTitle("Hello, World!")
pointStyle.setSnippet("I am a draggable marker")
val lineStyle = layer.getDefaultLineStringStyle()
val polygonStyle = layer.getDefaultPolygonStyle()

      
```

### Java

```java
GeoJsonPointStyle pointStyle = layer.getDefaultPointStyle();
pointStyle.setDraggable(true);
pointStyle.setTitle("Hello, World!");
pointStyle.setSnippet("I am a draggable marker");
GeoJsonLineStringStyle lineStyle = layer.getDefaultLineStringStyle();
com.google.maps.android.data.geojson.GeoJsonPolygonStyle polygonStyle =
        layer.getDefaultPolygonStyle();

      
```

### 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

```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

```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](https://developers.google.com/maps/documentation/android-sdk/utility/setup.md.txt) shows you how
to run the demo app.