Skip to content

cocoahero/android-geojson

Repository files navigation

Android GeoJSON

A complete GeoJSON implementation for Android.

Table of Contents

Requirements

  • Android SDK 8 or Higher

Installation

Android Studio / Gradle

dependencies {
  compile 'com.cocoahero.android:geojson:1.0.1@jar'
}

Maven

<dependency>
  <groupId>com.cocoahero.android</groupId>
  <artifactId>geojson</artifactId>
  <version>1.0.1</version>
  <type>jar</type>
</dependency>

Sample Usage

Whether you need to parse existing GeoJSON from a file or web server, or create new GeoJSON from user input, this library has got you covered.

Parsing GeoJSON

If you have existing GeoJSON that you need to parse, you have three source options with this library:

  1. String
  2. JSONObject
  3. InputStream

Once you have your GeoJSON in one of the above formats, simply pass it to GeoJSON#parse.

String
String string; // A string containing GeoJSON

try {
    GeoJSONObject geoJSON = GeoJSON.parse(string);
}
catch (JSONException e) {
    e.printStackTrace();
}
JSONObject
JSONObject json; // A JSONObject formatted as GeoJSON

GeoJSONObject geoJSON = GeoJSON.parse(json);
InputStream
InputStream stream; // An InputStream pointing to GeoJSON

try {
    GeoJSONObject geoJSON = GeoJSON.parse(stream);
}
catch (IOException e) {
    e.printStackTrace();
}
catch (JSONException e) {
    e.printStackTrace();
}

The returned object instance will be a subclass of GeoJSONObject, depending on the type property of the GeoJSON.

Creating GeoJSON

Parsing existing GeoJSON is only half the fun! Why not create new GeoJSON?! Simply create a new instance of which ever GeoJSONObject sub-type you would like, then call #toJSON on it to get a properly formatted JSONObject instance.

For example, the following sample code creates a GeoJSON Feature with a Point geometry.

// Create geometry
Point point = new Point(38.889462878011365, -77.03525304794312);

// Create feature with geometry
Feature feature = new Feature(point);

// Set optional feature identifier
feature.setIdentifier("MyIdentifier");

// Set optional feature properties
feature.setProperties(new JSONObject());

// Convert to formatted JSONObject
JSONObject geoJSON = feature.toJSON();

The resulting GeoJSON can be seen here.

About

A complete GeoJSON implementation for Android.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages