从存储中加载Kml文件并在地图中显示

时间:2020-03-10 09:28:13

标签: android google-maps ftp kml

文件Kml是通过FTP和apache库下载的,并保存在存储中:

    File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
    File downloaded = new File(dir.getAbsolutePath() + "/0001_0005.kml");
    OutputStream outputStream1 = new BufferedOutputStream(new FileOutputStream(downloaded));
    boolean success = ftp.retrieveFile("/0001_0005.kml", outputStream1);
    outputStream1.close();

如何将File转换为KmlLayer类型并在地图中显示?我可以显示文件是否是从资源上的原始包导入的,没有问题。

    KmlLayer layer = new KmlLayer(mMap,R.raw.test,getContext());
    layer.addLayerToMap();

1 个答案:

答案 0 :(得分:0)

您可以使用类似的内容:

private KmlLayer createLayerFromFile(String kmlFileName) {
    KmlLayer kmlLayer = null;

    InputStream inputStream;

    try {
        inputStream = new FileInputStream(kmlFileName);
        kmlLayer = new KmlLayer(mGoogleMap, inputStream, getApplicationContext());
        inputStream.close();
    }
    catch(IOException e)
    {
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    }

    return kmlLayer;
}

并以这种方式使用它:

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

    // path to your kml file 
    String kmlFileName = dir.getAbsolutePath() + "/0001_0005.kml";
    try {
        KmlLayer kmlLayer = createFromKml(kmlFileName);
        kmlLayer.addLayerToMap();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    }
}