I'm currently working with the Android Google Maps utility library. I receive geojson of a given area via an API call and need to display this area on the map.
I do so by calling
GeoJsonLayer layer = new GeoJsonLayer(getMap(), geoJsonData);
layer.addLayerToMap()
where getMap() returns a GoogleMap object and geoJsonData is a JSONObject. This code draws a border around the area associated with the geojson.
The code below draws a red border around the area and fills it with yellow.
GeoJsonLayer layer = new GeoJsonLayer(getMap(), geoJsonData);
GeoJsonPolygonStyle polygonStyle = layer.getDefaultPolygonStyle();
polygonStyle.setStrokeColor(ContextCompat.getColor(this, R.color.red));
polygonStyle.setFillColor(ContextCompat.getColor(this, R.color.yellow));
layer.addLayerToMap();
I'm running into trouble trying to style this GeoJsonLayer where the whole map is yellow, the border of the area is red, and the fill color of the area is normal color.
Can someone tell me how to achieve this in Android using the GeoJson data? One option is to create my own polygon shape. I can declare a polygon using coordinates that cover the whole map, and i add a hole using the coordinates returned from the geojson.
But I'm thinking there has to be something simpler that allows me to use the built in GeoJsonLayer class and won't require me to parse the geojson myself.
Long press the marker to enable dragging. When you take your finger off the screen, the marker will remain in that position. Markers are not draggable by default. You must explicitly set the marker to be draggable either with MarkerOptions.
You can use same approach as you described:
"declare a polygon using coordinates that cover the whole map, and i add a hole using the coordinates returned from the geojson"
GeoJSON also supported Polygon with "holes" so, for GeoJSON with "whole map" coordinates like
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-179.99,
89.99
],
[
-179.99,
0
],
[
-179.99,
-89.99
],
[
0,
-89.99
],
[
179.99,
-89.99
],
[
179.99,
0
],
[
179.99,
89.99
],
[
0,
89.99
],
[
-179.99,
89.99
]
],
[
[
-0.191208,
51.509869
],
[
-0.158464,
51.513287
],
[
-0.151769,
51.50554
],
[
-0.174471,
51.502178
],
[
-0.187989,
51.502444
],
[
-0.191208,
51.509869
]
]
],
"properties": {
"Territoire": 2
}
},
"properties": {
"name": "Hyde Park"
}
}
]
}
you automatically got something like this:
If you didn't have access to back-end where GeoJSON produced, and receive only polygons for target area you can add "whole map" part
[
[
-179.99,
89.99
],
[
-179.99,
0
],
[
-179.99,
-89.99
],
[
0,
-89.99
],
[
179.99,
-89.99
],
[
179.99,
0
],
[
179.99,
89.99
],
[
0,
89.99
],
[
-179.99,
89.99
]
]
into position 0 (before coordinates of target area) of coordinates
array of Polygon by code like that:
JSONObject featureCollection = geoJsonData;
JSONArray features = featureCollection.getJSONArray("features");
for(int i = 0; i < features.length(); i++) {
JSONObject feature = features.getJSONObject(i);
JSONObject geometry = feature.getJSONObject("geometry");
String geometryType = geometry.getString("type");
if ("Polygon".equals(geometryType)) {
JSONArray coordinates = geometry.getJSONArray("coordinates");
if (coordinates.length() == 1) {
coordinates.put(coordinates.get(0));
JSONArray wholeMap = new JSONArray(
"[\n" +
" [\n" +
" -179.99,\n" +
" 89.99\n" +
" ],\n" +
" [\n" +
" -179.99,\n" +
" 0\n" +
" ],\n" +
" [\n" +
" -179.99,\n" +
" -89.99\n" +
" ],\n" +
" [\n" +
" 0,\n" +
" -89.99\n" +
" ],\n" +
" [\n" +
" 179.99,\n" +
" -89.99\n" +
" ],\n" +
" [\n" +
" 179.99,\n" +
" 0\n" +
" ],\n" +
" [\n" +
" 179.99,\n" +
" 89.99\n" +
" ],\n" +
" [\n" +
" 0,\n" +
" 89.99\n" +
" ],\n" +
" [\n" +
" -179.99,\n" +
" 89.99\n" +
" ]\n" +
" ]"
);
coordinates.put(0, wholeMap);
} else {
Log.d("OAA", "No need insert");
}
}
}
where geoJsonData
- your GeoJSON data without "whole map" part e.g.:
JSONObject geoJsonData = new JSONObject(
"{\n" +
" \"type\": \"FeatureCollection\",\n" +
" \"features\": [\n" +
" {\n" +
" \"type\": \"Feature\",\n" +
" \"geometry\": {\n" +
" \"type\": \"Polygon\",\n" +
" \"coordinates\": [\n" +
" [\n" +
" [\n" +
" -0.191208,\n" +
" 51.509869\n" +
" ],\n" +
" [\n" +
" -0.158464,\n" +
" 51.513287\n" +
" ],\n" +
" [\n" +
" -0.151769,\n" +
" 51.50554\n" +
" ],\n" +
" [\n" +
" -0.174471,\n" +
" 51.502178\n" +
" ],\n" +
" [\n" +
" -0.187989,\n" +
" 51.502444\n" +
" ]\n" +
" ]\n" +
" ],\n" +
" \"properties\": {\n" +
" \"Territoire\": 2\n" +
" }\n" +
" },\n" +
" \"properties\": {\n" +
" \"name\": \"Hyde Park\"\n" +
" }\n" +
" }\n" +
" ]\n" +
"}"
);
Update GeoJSON also supported polygon with multi "holes", that GeoJSON shows 2 "holes" for Hyde & Regent's Parks:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Polygon",
"coordinates": [
[
[
-179.99,
89.99
],
[
-179.99,
0
],
[
-179.99,
-89.99
],
[
0,
-89.99
],
[
179.99,
-89.99
],
[
179.99,
0
],
[
179.99,
89.99
],
[
0,
89.99
],
[
-179.99,
89.99
]
],
[
[
-0.191208,
51.509869
],
[
-0.158464,
51.513287
],
[
-0.151769,
51.50554
],
[
-0.174471,
51.502178
],
[
-0.187989,
51.502444
],
[
-0.191208,
51.509869
]
],
[
[
-0.167685,
51.530226
],
[
-0.163737,
51.534924
],
[
-0.151849,
51.537566
],
[
-0.151849,
51.537566
],
[
-0.146914,
51.535964
],
[
-0.145625,
51.525325
],
[
-0.155538,
51.523589
],
[
-0.167685,
51.530226
]
]
],
"properties": {
"Territoire": 2
}
},
"properties": {
"name": "Hyde Park"
}
}
]
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With