You're all set!

To start developing, please head over to our developer documentation.

Activate the Google Maps Android API

To get you started we'll guide you through the Google Developers Console to do a few things first:

  1. Create or choose a project
  2. Activate the Google Maps Android API
  3. Create appropriate keys
Continue

Google Maps Android GeoJSON Utility

  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
GeoJsonLayer layer = new GeoJsonLayer(getMap(), 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
GeoJsonLayer layer = new GeoJsonLayer(getMap(), R.raw.geoJsonFile,
getApplicationContext());

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

layer.addLayerToMap();

Remove the GeoJsonLayer

Let’s assume that you have added this layer

GeoJsonLayer layer = new GeoJsonLayer(getMap(), geoJsonData);

To clear the GeoJsonLayer, call removeLayerFromMap()

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.

GeoJsonPoint point = new GeoJsonPoint(new LatLng(0, 0));
HashMap<String, String> properties = new HashMap<String, String>();
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.

layer.addFeature(pointFeature);

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

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.

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.

GeoJsonPoint point = new Point(new LatLng(0, 0));
HashMap<String, String> properties = new HashMap<String, String>();
properties.put(“Ocean”, “South Atlantic”);
GeoJsonFeature pointFeature = new GeoJsonFeature(point, “Origin”, properties, null);
for (GeoJsonFeature feature : layer.getFeatures()) {
  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:

// Set a listener for geometry clicked events.
mLayer.setOnFeatureClickListener(new GeoJsonLayer.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.

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 as 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.

// Create a new feature containing a linestring
ArrayList<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.

Send feedback about...

Google Maps Android API
Google Maps Android API
Need help? Visit our support page.