Android Google Maps GeoJson,如何为整个 map 着色一种颜色

标签 android google-maps google-maps-android-api-2 geojson

我目前正在使用 Android 版 Google map 实用程序库。我通过 API 调用接收给定区域的 geojson,并需要在 map 上显示该区域。

我通过调用

GeoJsonLayer layer = new GeoJsonLayer(getMap(), geoJsonData);
layer.addLayerToMap()

其中 getMap() 返回一个 GoogleMap 对象,geoJsonData 是一个 JSONObject。 此代码在与 geojson 关联的区域周围绘制边框。

下面的代码在该区域周围绘制了一个红色边框并用黄色填充。

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

我在尝试设置整个 map 为黄色、区域边界为红色且区域填充颜色为正常颜色的 GeoJsonLayer 样式时遇到了麻烦。

有人能告诉我如何使用 GeoJson 数据在 Android 中实现这一点吗? 一种选择是创建我自己的多边形形状。我可以使用覆盖整个 map 的坐标声明一个多边形,并使用从 geojson 返回的坐标添加一个洞。

但我认为必须有一些更简单的东西可以让我使用内置的 GeoJsonLayer 类,而不需要我自己解析 geojson。

最佳答案

您可以使用与您描述的相同的方法:

"declare a polygon using coordinates that cover the whole map, and i add a hole using the coordinates returned from the geojson"

GeoJSON 也支持 Polygon with "holes",因此,对于具有“整个 map ”坐标的 GeoJSON

{
  "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"
      }
    }
  ]
}

你会自动得到这样的东西:

GeoJSON polygon with hole

如果您无法访问生成 GeoJSON 的后端,并且只收到目标区域的多边形,您可以添加“整个 map ”部分

[
    [
      -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(目标区域坐标之前):

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

其中 geoJsonData - 没有“整个 map ”部分的 GeoJSON 数据,例如:

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" +
            "}"
    );

更新 GeoJSON 还支持具有多个“孔”的多边形,GeoJSON 为海德和摄政公园显示了 2 个“孔”:

{
  "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"
      }
    }
  ]
}

关于Android Google Maps GeoJson,如何为整个 map 着色一种颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49825918/

相关文章:

android - 通过pyqtdeploy和Qt5将PyQt5应用部署到Android

android - 以编程方式获取主题属性

PHP/谷歌地图 - 地理围栏

java - Android Geocoder getFromLocationName 总是返回 null

android - IllegalArgumentException : the bind value at index 1 is null

android - 为什么 LoadParams.requestedLoadSize 将指定的页面大小返回给 LivePagedListBuilder 3 倍?

ios - Google 映射 iOS SDK 详细级别多段线

google-maps - 提供的 key 不是有效的 Google API key

fragment 中的android map

android - Google Maps Android API v2 和当前位置