Google Maps Android GeoJSON 实用程序

请选择平台: Android iOS JavaScript
  1. 简介
  2. 向您的地图添加 GeoJsonLayer
  3. 移除 GeoJsonLayer
  4. 添加和移除 GeoJsonFeature
  5. 访问 GeoJsonFeature 及其属性
  6. 为 GeoJsonLayer 和 GeoJsonFeature 设置样式
  7. 观摩演示版应用

简介

GeoJSON 是对 JSON 数据格式的扩展,表示地理数据。您可以使用此实用程序以 GeoJSON 格式存储地貌,并将它们渲染为覆盖在地图上的图层。如需在地图中添加和移除 GeoJSON 数据,请分别调用 addLayerToMap()removeLayerFromMap()。同样,您可以通过调用 addFeature()removeFeature(),并传入 GeoJsonFeature 对象来添加和移除个别地图项。如果您想访问地图项,可以调用 getFeatures() 来获取所有已添加至图层的 GeoJsonFeature 对象的 iterable。

您还可以通过调用 getDefaultPointStyle()getDefaultLineStringStyle() getDefaultPolygonStyle() 并为它们逐一设置样式选项,在向图层添加地图项前,设置应用到地图项的默认样式。此外,您还可以通过对地图项调用 setPointStyle()setLineStringStyle()setPolygonStyle() 并传入相关样式对象,为个别 GeoJsonFeature 设置样式。

向您的地图添加 GeoJsonLayer

如需向地图添加 GeoJson 图层,请先创建一个 GeoJsonLayer 类实例。有两种方法可以实例化 GeoJsonLayer。

如需从 JSONObject 导入,您需要下列项:

  • 需要渲染的图层所在的 GoogleMap 对象
  • 包含需要向图层添加的 GeoJSON 数据的 JSONObject

Java


JSONObject geoJsonData = // JSONObject containing the GeoJSON data
GeoJsonLayer layer = new GeoJsonLayer(map, geoJsonData);

      

Kotlin


val geoJsonData: JSONObject? = // JSONObject containing the GeoJSON data
val layer = GeoJsonLayer(map, geoJsonData)

      

如需从本地 GeoJSON 文件导入,您需要下列项:

  • 需要渲染的图层所在的 GoogleMap 对象
  • 包含 GeoJSON 数据的本地资源文件
  • 打开本地资源文件所需的 Context 对象

Java


GeoJsonLayer layer = new GeoJsonLayer(map, R.raw.geojson_file, context);

      

Kotlin


val layer = GeoJsonLayer(map, R.raw.geojson_file, context)

      

在您创建 GeoJsonLayer 后,调用 addLayerToMap() 将导入的数据添加到地图上:

Java


layer.addLayerToMap();

      

Kotlin


layer.addLayerToMap()

      

移除 GeoJsonLayer

让我们假定您添加了以下图层

Java


JSONObject geoJsonData = // JSONObject containing the GeoJSON data
GeoJsonLayer layer = new GeoJsonLayer(map, geoJsonData);

      

Kotlin


val geoJsonData: JSONObject? = // JSONObject containing the GeoJSON data
val layer = GeoJsonLayer(map, geoJsonData)

      

要清除 GeoJsonLayer,请调用 removeLayerFromMap()

Java


layer.removeLayerFromMap();

      

Kotlin


layer.removeLayerFromMap()

      

添加和移除 GeoJsonFeature

GeoJSON 中的地图项类型为“feature”。它包含一个几何图形、一个属性成员以及可选的边界框或 ID。

您可以分别创建 GeoJsonFeature 对象,并将它们添加到 GeoJsonLayer 中。

让我们假定您创建了一个地图项,它包含一个坐标为 (0, 0) 的点,属性中有一个条目,并且没有边界框。

Java


GeoJsonPoint point = new GeoJsonPoint(new LatLng(0, 0));
HashMap<String, String> properties = new HashMap<>();
properties.put("Ocean", "South Atlantic");
GeoJsonFeature pointFeature = new GeoJsonFeature(point, "Origin", properties, null);

      

Kotlin


val point = GeoJsonPoint(LatLng(0.0, 0.0))
val properties = hashMapOf("Ocean" to "South Atlantic")
val pointFeature = GeoJsonFeature(point, "Origin", properties, null)

      

如需向图层添加该地图项,请调用 addFeature() 并传入要添加的地图项。

Java


layer.addFeature(pointFeature);

      

Kotlin


layer.addFeature(pointFeature)

      

如果向图层添加某个地图项后需要将其移除,请调用 removeFeature() 并传入要移除的地图项。

Java


layer.removeFeature(pointFeature);

      

Kotlin


layer.removeFeature(pointFeature)

      

访问 GeoJsonFeatures 及其属性

如需访问所有已添加到图层中的 GeoJsonFeature,您可以对已创建的 GeoJsonLayer 调用 getFeatures()。这将返回一个 GeoJsonFeatures 的 iterable,您可以按如下所示使用 for-each 循环对其进行访问。

Java


for (GeoJsonFeature feature : layer.getFeatures()) {
    // Do something to the feature
}

      

Kotlin


for (feature in layer.features) {
    // Do something to the feature
}

      

hasProperty()getProperty() 方法与 getFeatures() 方法结合使用可检查存储的每个地图项是否都有特定属性,并在特定属性存在时进行访问。

Java


if (feature.hasProperty("Ocean")) {
    String oceanProperty = feature.getProperty("Ocean");
}

      

Kotlin


if (feature.hasProperty("Ocean")) {
    val oceanProperty = feature.getProperty("Ocean")
}

      

GeoJSON 几何图形点击事件

您可以使用 GeoJsonLayer.OnFeatureClickListener() 监听地图上的几何图形地图项上的点击事件。以下示例会在用户点击某个地图项时记录地图项标题:

Java


// Set a listener for geometry clicked events.
layer.setOnFeatureClickListener(new Layer.OnFeatureClickListener() {
    @Override
    public void onFeatureClick(Feature feature) {
        Log.i("GeoJsonClick", "Feature clicked: " + feature.getProperty("title"));
    }
});

      

Kotlin


// Set a listener for geometry clicked events.
layer.setOnFeatureClickListener { feature ->
    Log.i("GeoJsonClick", "Feature clicked: ${feature.getProperty("title")}")
}

      

为 GeoJsonLayer 和 GeoJsonFeature 设置样式

您可以为 GeoJsonLayer 设置默认样式,或者为图层中的个别地图项设置样式。

默认样式

在 GeoJsonLayer 中,您可以为添加到图层中的任何点、linestring 和多边形设置默认样式。仅在没有为地图项的任何几何图形设置样式时,才会为其应用默认样式。此外,您对默认样式所做的任何更改也会体现在所有使用默认样式的地图项中。

应用默认样式的步骤如下

  1. 检索相关的默认样式对象,该对象可以是 GeoJsonPointStyleGeoJsonLineStringStyleGeoJsonPolygonStyle 中的一项。
  2. 对样式应用所需选项。

例如,以下代码示例展示了如何修改默认点样式来使点变为可拖动点,并带有标题和信息摘要。

Java


GeoJsonPointStyle pointStyle = layer.getDefaultPointStyle();
pointStyle.setDraggable(true);
pointStyle.setTitle("Hello, World!");
pointStyle.setSnippet("I am a draggable marker");

      

Kotlin


val pointStyle = layer.defaultPointStyle
pointStyle.isDraggable = true
pointStyle.title = "Hello, World!"
pointStyle.snippet = "I am a draggable marker"

      

GeoJsonFeature 专属样式

您还可以为图层中的个别地图项设置样式。对 GeoJsonFeature 应用样式的步骤如下。

  1. 创建相关的样式对象,该对象可以是 GeoJsonPointStyleGeoJsonLineStringStyleGeoJsonPolygonStyle
  2. 对样式应用所需选项。
  3. 将样式对象传递给 GeoJsonFeature 的相关方法,该方法可以是 setPointStyle()setLineStringStyle()setPolygonStyle()

例如,可以按照以下方式自定义 GeoJsonFeature 的 LineString 样式,将颜色设置为红色。

Java


// Create a new feature containing a linestring
List<LatLng> lineStringArray = new ArrayList<LatLng>();
lineStringArray.add(new LatLng(0, 0));
lineStringArray.add(new LatLng(50, 50));
GeoJsonLineString lineString = new GeoJsonLineString(lineStringArray);
GeoJsonFeature lineStringFeature = new GeoJsonFeature(lineString, null, null, null);

// Set the color of the linestring to red
GeoJsonLineStringStyle lineStringStyle = new GeoJsonLineStringStyle();
lineStringStyle.setColor(Color.RED);

// Set the style of the feature
lineStringFeature.setLineStringStyle(lineStringStyle);

      

Kotlin


// Create a new feature containing a linestring
val lineStringArray: MutableList<LatLng> = ArrayList()
lineStringArray.add(LatLng(0.0, 0.0))
lineStringArray.add(LatLng(50.0, 50.0))
val lineString = GeoJsonLineString(lineStringArray)
val lineStringFeature = GeoJsonFeature(lineString, null, null, null)

// Set the color of the linestring to red
val lineStringStyle = GeoJsonLineStringStyle()
lineStringStyle.color = Color.RED

// Set the style of the feature
lineStringFeature.lineStringStyle = lineStringStyle

      

观摩演示版应用

如需查看从网址导入 GeoJSON 文件并利用其创建图层的示例,请看一看实用程序库附带的演示版应用中的 GeoJsonDemoActivity设置指南向您介绍了如何运行演示版应用。