Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does the Android API support KML files?

Tags:

Is there a way in Android to load KML files from Google Earth into a map widget?

Specifically I am looking to load saved shapes created in Google Earth overtop a map widget as easily as possible. Has anybody ever tried this before?

like image 385
benstpierre Avatar asked Mar 15 '10 01:03

benstpierre


People also ask

Can you import KML file into Google Maps Android?

You can import map features like lines, shapes, and places to your map from KML files, spreadsheets and other files.

What apps support KML files?

A KML file is a Keyhole Markup Language file. Open one with Google Earth, Merkaartor, or Marble.

Is KML the same as XML?

Keyhole Markup Language (KML) is an XML-based format for storing geographic data and associated content and is an official Open Geospatial Consortium (OGC) standard.


1 Answers

It is an old question but this answer might be of some use if someone happens to stumble upon it.

Do you mean to add the kml file onto a mapfragment inside an app programmatically? If so, you can use this method.

 private void loadKml(File file) {

    try( InputStream inputstream = new FileInputStream(file) ) {

        // Set kmllayer to map
        // map is a GoogleMap, context is the Activity Context

        KmlLayer layer = new KmlLayer(map, inputstream, context);

        layer.addLayerToMap();



    // Handle these errors

    } catch (FileNotFoundException e) {

        e.printStackTrace();

    } catch (IOException e) {

        e.printStackTrace();

    } catch (XmlPullParserException e) {

        e.printStackTrace();
    }

}

The downside is that you have little to no control as to how the kml layer is displayed. You can find a complete reference here: https://developers.google.com/maps/documentation/android-api/utility/kml

You can also try to parse the kml file to a POJO using JAK and JAXB and draw that data programmatically. If you can get it to work it is relatively painless. https://labs.micromata.de/projects/jak.html

like image 160
Willem Leuverink Avatar answered Oct 12 '22 08:10

Willem Leuverink