谷歌地图Android

时间:2016-06-17 12:32:16

标签: java android json google-maps geojson

我是新来的,所以请保持同情,如果已经问过这个问题(通常不是)或作为新手,也许我做错了。

所以有问题。

我想集中在谷歌地图android,我的geojson。

有我的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
                }
  }

我能够把它变成一个字符串,然后是一个JsonObject。像这样:

    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;
            }

我认为我很好...... ....-D

但是知道我想把我的图层放在我的谷歌地图中,​​具有良好的缩放级别...有问题! 我花时间在互联网上,但不能这样做。我尝试了一些但不起作用:

        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;
    }

有人知道答案吗?

谢谢!

约旦

1 个答案:

答案 0 :(得分:1)

您可以将地图置于要生成的LatLngBounds bounds对象的完整扩展名(documentation):

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

请注意,您可以设置填充(我已将其设置为0)

在您的代码中:

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();
}

使用以下方式居中:

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