GeoJSON is very simple; A shared JSON library should be all you need. Here's how you could create a list of points using json.org code ( http://json.org/java/ ):
JSONObject featureCollection = new JSONObject();
try {
featureCollection.put("type", "featureCollection");
JSONArray featureList = new JSONArray();
for (ListElement obj : list) {
JSONObject point = new JSONObject();
point.put("type", "Point");
JSONArray coord = new JSONArray("["+obj.getLon()+","+obj.getLat()+"]");
point.put("coordinates", coord);
JSONObject feature = new JSONObject();
feature.put("geometry", point);
featureList.put(feature);
featureCollection.put("features", featureList);
}
} catch (JSONException e) {
Log.error("can't save json object: "+e.toString());
}
System.out.println("featureCollection="+featureCollection.toString());
This will output something like this:
{
"features": [
{
"geometry": {
"coordinates": [
-94.149,
36.33
],
"type": "Point"
}
}
],
"type": "featureCollection"
}
source
share