Jordan Matas Jordan Matas - 5 months ago 56
JSON Question

Center GeoJson in google map Android

I'm new here, so please be sympatic , if that question is already asked( normaly not ) or as a newbie, maybe I doing it wrong.

So there's the problem.

I would like to center in google map android, my geojson.

There is my geoJson.

{
"type": "Polygon",
"coordinates": [
[
[
2.1096056699752808,
49.007069721997176
],
[
2.1101635694503784,
49.00640113120101
],
[
2.1103888750076294,
49.00579235387832
],
[
2.1109735965728755,
49.00497770668481
],
[
2.1093428134918213,
49.004180641564616
],
[
2.10762619972229,
49.003237519337205
],
[
2.1063923835754395,
49.00271668298914
],
[
2.104933261871338,
49.0023225329428
],
[
2.103109359741211,
49.00157645467159
],
[
2.1024978160858154,
49.00125268137821
],
[
2.102004289627075,
49.000830365223656
],
[
2.1010172367095947,
48.99976048160506
],
[
2.1006417274475098,
48.9995141235692
],
[
2.0986783504486084,
48.99873280859517
],
[
2.0958673954010005,
48.997838856609434
],
[
2.093796730041504,
48.99738131592137
],
[
2.0936572551727295,
48.99733204205815
],
[
2.09065318107605,
48.998359743969125
],
[
2.0894408226013184,
48.99889470369554
],
[
2.088373303413391,
48.99951060415987
],
[
2.0878690481185913,
49.00182632175247
],
[
2.0877134799957275,
49.002618145770015
],
[
2.087423801422119,
49.003913190807005
],
[
2.0922625064849854,
49.0047999956991
],
[
2.09663987159729,
49.00562344324437
],
[
2.101478576660156,
49.006489104187075
],
[
2.104707956314087,
49.007101391864836
],
[
2.1054375171661377,
49.0071295428414
],
[
2.1062850952148438,
49.00706620312175
],
[
2.108291387557983,
49.007059165370144
],
[
2.1096056699752808,
49.007069721997176
]
]
],
"properties": {
"Territoire" : 2
}
}


I'm able to take this into a string and then a JsonObject. like this :

String json = loadJSONFromAsset(ville, terr);



try {
JSONObject obj = new JSONObject(json);
GeoJsonLayer layer = new GeoJsonLayer(mMap, obj);
layer.addLayerToMap();
(......)

} catch (JSONException e) {
e.printStackTrace();


}

public String loadJSONFromAsset(String ville, String terr) {
String json = null;
try {
InputStream is = getAssets().open(ville+"/"+terr+".geojson");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
json = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return json;
}


Here i'm good well i think.... :-D

But know i would like to center my layer in my google map,with the good zoom level...there is the problem !
I have spend time in internet, but can't do it. I tried something but doesn't work :

JSONArray jsonArray = obj.getJSONArray("coordinates");
ArrayList<LatLng> listdata = new ArrayList<LatLng>();

if (jsonArray != null) {
for (int i=0;i<jsonArray.length();i++){
String[] coord = jsonArray.toString().split(",");
double x = Double.parseDouble(coord[0]);
double y = Double.parseDouble(coord[1]);
listdata.add(new LatLng(x,y));
}
}

LatLng center = getPolygonCenterPoint(listdata);
mMap.moveCamera(CameraUpdateFactory.newLatLng(center));


private LatLng getPolygonCenterPoint(ArrayList<LatLng> polygonPointsList){
LatLng centerLatLng = null;
LatLngBounds.Builder builder = new LatLngBounds.Builder();
for(int i = 0 ; i < polygonPointsList.size() ; i++)
{
builder.include(polygonPointsList.get(i));
}
LatLngBounds bounds = builder.build();
centerLatLng = bounds.getCenter();

return centerLatLng;
}


Someone know the answer ?

Thanks you !

Jordan

Answer

You can center the map to the full extension of the LatLngBounds bounds object that you are generating (documentation):

mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 0));

Note that you can set a padding (I have set it as 0)

In your code:

private LatLngBounds getPolygonBounds(ArrayList<LatLng> polygonPointsList){
    LatLngBounds.Builder builder = new LatLngBounds.Builder();
    for(int i = 0 ; i < polygonPointsList.size() ; i++) {
        builder.include(polygonPointsList.get(i));
    }
    return builder.build();
}

Center it using:

LatLngBounds bounds = getPolygonBounds(listdata);
mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 0));