在Maps API V2中导入KML

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

是否存在用于在Android项目中导入KML文件并在地图中显示它们的现有库? 我在堆栈溢出上找到了一些代码( 如何使用kml文件在地图上绘制路径? ),这不是库。

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

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

创建折线和多边形:

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

只是更新了关于Maps API V2的KML库的一部分问题。 目前, Google Maps KML导入实用程序已推出测试版。

它是Google Maps Android API实用程序库的一部分 。 据记载,它允许从流中加载KML文件

 KmlLayer layer = new KmlLayer(getMap(), kmlInputStream, getApplicationContext()); 

或当地资源

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

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

 layer.addLayerToMap();