在Maps API V2中导入KML

时间:2013-01-21 15:49:21

标签: java android google-maps overlay kml

我有多个KML文件,这些文件在Google地球中绘制并包含不同的路线。现在我正在尝试使用Maps API V2在我的android项目中显示这些内容。

是否存在用于在Android项目中导入KML文件并在地图中显示它们的现有库? 我在堆栈溢出(How to draw a path on a map using kml file?)上发现了一些不是库的代码。

如果没有可用的库,我只是从头开始构建它。

2 个答案:

答案 0 :(得分:0)

现在我只是假设没有公共图书馆为我们这样做,所以在解析我的KML文件中的数据后,我将使用Google的代码将折线和多边形添加到我的地图中。 如果找到了库,将更新此答案。

创建折线和多边形:

// Instantiates a new Polyline object and adds points to define a rectangle
PolylineOptions rectOptions = new PolylineOptions()
        .add(new LatLng(37.35, -122.0))
        .add(new LatLng(37.45, -122.0))  // North of the previous point, but at the same longitude
        .add(new LatLng(37.45, -122.2))  // Same latitude, and 30km to the west
        .add(new LatLng(37.35, -122.2))  // Same longitude, and 16km to the south
        .add(new LatLng(37.35, -122.0)); // Closes the polyline.

// Set the rectangle's color to red
rectOptions.color(Color.RED);

// Get back the mutable Polyline
Polyline polyline = myMap.addPolyline(rectOptions);

答案 1 :(得分:0)

仅针对Maps API V2的 KML库的更新部分提问。现在有Google Maps KML Importing Utility的测试版。

它是Google Maps Android API Utility Library的一部分。据记载,它允许从流

加载KML文件
KmlLayer layer = new KmlLayer(getMap(), kmlInputStream, getApplicationContext());

或本地资源

KmlLayer layer = new KmlLayer(getMap(), R.raw.kmlFile, getApplicationContext());

创建KmlLayer后,调用addLayerToMap()将导入的数据添加到地图上。

layer.addLayerToMap();