我目前正在使用Android Google地图实用程序库。我通过API调用接收给定区域的geojson,并且需要在地图上显示该区域。
我是通过致电
来这样做的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();
我在尝试设置此GeoJsonLayer的样式时遇到了麻烦,整个地图为黄色,区域边框为红色,区域的填充颜色为正常颜色。
有人能告诉我如何使用GeoJson数据在Android中实现这一目标吗? 一种选择是创建我自己的多边形形状。我可以使用覆盖整个地图的坐标声明一个多边形,然后使用geojson返回的坐标添加一个洞。
但我认为必须有一些更简单的东西允许我使用内置的GeoJsonLayer类,并且不需要我自己解析geojson。
答案 0 :(得分:3)
您可以使用与您描述的方法相同的方法:
“使用覆盖整个地图的坐标来声明多边形,然后我 使用geojson“
返回的坐标添加一个洞
GeoJSON也支持Polygon with "holes"所以,对于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生成的后端,并且只接收目标区域的多边形,则可以添加“整个地图”部分
[
[
-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
]
]
按照以下代码进入coordinates
多边形数组的位置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
- 您的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显示了Hyde&amp;的两个“孔”。摄政公园:
{
"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"
}
}
]
}