如何使用GMaps API将KML文件导入Android应用程序

时间:2016-10-08 15:40:08

标签: java android maps kml

我有一个使用Google Maps API的简单应用程序,我有一个带有1000个标记的KML文件,我希望在我的应用程序的MAP中显示。

如何在地图中导入KML文件?

我使用Android Studio

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;


    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
        // TODO: Consider calling
        //    ActivityCompat#requestPermissions
        // here to request the missing permissions, and then overriding
        //   public void onRequestPermissionsResult(int requestCode, String[] permissions,
        //                                          int[] grantResults)
        // to handle the case where the user grants the permission. See the documentation
        // for ActivityCompat#requestPermissions for more details.
        return;
    }
    mMap.setMyLocationEnabled(true);


}

2 个答案:

答案 0 :(得分:3)

如果要从本地资源加载KML数据集,则如下所示:

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

如果要从本地流加载KML数据集,则如下所示:

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

Maps Andriod API documentation

中的更多详细信息

答案 1 :(得分:2)

您需要渲染地图并在其上添加KML图层。例如:

KmlLayer kmlLayer = new KmlLayer(mMap, R.raw.coordinates, getApplicationContext());
kmlLayer.addLayerToMap();

有关完整样本,请参阅this github示例项目