0

The file Kml is downloaded using FTP with the apache library and saved on storage:

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

How can I convert the File into type KmlLayer and show it in the map? I can show the file if is imported from raw package on resources with no problems.

    KmlLayer layer = new KmlLayer(mMap,R.raw.test,getContext());
    layer.addLayerToMap();
Olaf Kock
  • 46,930
  • 8
  • 59
  • 90
mike1
  • 71
  • 7

1 Answers1

0

You can use something like that:

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;
}

and use it this way:

@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();
    }
}
Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79