从我的谷歌地图android导出KML文件

时间:2013-08-24 09:34:57

标签: android kml

我在谷歌地图上绘制了一些线条和多边形,我想将此地图导出为kml文件,我该怎么做?

3 个答案:

答案 0 :(得分:1)

如果您想在Google地图上绘制线条,多边形,圆形等,只需使用www.gmapgis.com该应用程序允许将图形导出为kml文件。

答案 1 :(得分:0)

当我解析kml时,我将所有内容放入一个类中。我在第二个应用程序中使用了第一个应用程序中的相同类。刚刚扩展了原来的课程。对我来说,从另一个应用程序中100%重用一个类并使用超级操作码非常酷。

反正。基本上是折线选项列表和标记选项列表。

super.get_po是折线选项 super.get_mo是标记选项

然后,当我写kml时,我只是将该类序列化。我的高尔夫应用程序取决于标记和线条的顺序。对于每个洞它是标记,线,标记,我使用与http://maps.google.com相同的样式,其中每个元素都有自己的风格。

来自my golf app,其中有两个令人震惊的地球,两个下载我,可能是我的妻子手机。

    @Override
public void saveGolfMap() {
    // TODO Auto-generated method stub
    FileOutputStream fileos = null;
    StringBuilder sb = new StringBuilder();
    try {
        fileos = new FileOutputStream(super.get_pathstring());

    } catch (FileNotFoundException e) {
        // Log.e("FileNotFoundException", e.toString());
        e.printStackTrace();
    }

    // serialize the file();
    try {

        xmlSerializer = XmlPullParserFactory.newInstance().newSerializer();
    } catch (XmlPullParserException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }

    try {
        xmlSerializer.setOutput(fileos, "UTF-8");
        xmlSerializer.startDocument(null, null);
        xmlSerializer.setFeature(
                "http://xmlpull.org/v1/doc/features.html#indent-output",
                true);
        xmlSerializer.startTag(null, "kml");
        xmlSerializer.attribute(null, "xmlns",
                "http://earth.google.com/kml/2.2");
        xmlSerializer.startTag(null, "Document");

        xmlSerializer.startTag(null, "name");
        xmlSerializer.text("todo put the name here");
        xmlSerializer.endTag(null, "name");

        xmlSerializer.startTag(null, "description");
        xmlSerializer.cdsect("todo put the description here");
        xmlSerializer.endTag(null, "description");

        // spin through the marker options and create a style section for
        // each two
        // markers create three style sections
        // <Style id="style1">
        // <IconStyle>
        // <Icon>
        // <href>http://maps.gstatic.com/mapfiles/ms2/micons/golfer.png</href>
        // </Icon>
        // </IconStyle>
        // </Style>
        // <Style id="style2">
        // <IconStyle>
        // <Icon>
        // <href>http://maps.gstatic.com/mapfiles/ms2/micons/flag.png</href>
        // </Icon>
        // </IconStyle>
        // </Style>
        // <Style id="style3">
        // <LineStyle>
        // <color>73FF0000</color>
        // <width>3</width>
        // </LineStyle>
        // </Style>
        setInt();
        int y = 0;
        for (int k = 0; k < mosize; k++) {
            y += 1;
            if (k % 2 == 0) {
                // write out the tee marker style
                xmlSerializer.startTag(null, "Style");
                xmlSerializer.attribute(null, "id",
                        "style" + Integer.toString(y));
                xmlSerializer.startTag(null, "IconStyle");
                xmlSerializer.startTag(null, "Icon");
                xmlSerializer.startTag(null, "href");
                xmlSerializer
                        .text("http://maps.gstatic.com/mapfiles/ms2/micons/golfer.png");
                xmlSerializer.endTag(null, "href");
                xmlSerializer.endTag(null, "Icon");
                xmlSerializer.endTag(null, "IconStyle");
                xmlSerializer.endTag(null, "Style");

                // write out the polyline style
                y += 1;
                xmlSerializer.startTag(null, "Style");
                xmlSerializer.attribute(null, "id",
                        "style" + Integer.toString(y));
                xmlSerializer.startTag(null, "LineStyle");
                xmlSerializer.startTag(null, "color");
                xmlSerializer.text("73FF0000");
                xmlSerializer.endTag(null, "color");
                xmlSerializer.startTag(null, "width");
                xmlSerializer.text("10");
                xmlSerializer.endTag(null, "width");
                xmlSerializer.endTag(null, "LineStyle");
                xmlSerializer.endTag(null, "Style");

            } else {
                // write out the cup marker style
                xmlSerializer.startTag(null, "Style");
                xmlSerializer.attribute(null, "id",
                        "style" + Integer.toString(y));
                xmlSerializer.startTag(null, "IconStyle");
                xmlSerializer.startTag(null, "Icon");
                xmlSerializer.startTag(null, "href");
                xmlSerializer
                        .text("http://maps.gstatic.com/mapfiles/ms2/micons/flag.png");
                xmlSerializer.endTag(null, "href");
                xmlSerializer.endTag(null, "Icon");
                xmlSerializer.endTag(null, "IconStyle");
                xmlSerializer.endTag(null, "Style");

            }
        }

        // write placemark for tee
        // write poly lines
        // write placemark for cup
        // <Placemark>
        // <name>Tee Hole 1</name>
        // <description><![CDATA[]]></description>
        // <styleUrl>#style1</styleUrl>
        // <Point>
        // <coordinates>-xx.999999,xx.999999,0.000000</coordinates>
        // </Point>
        // </Placemark>

        y = 0;
        for (int k = 0; k < mosize; k++) {
            y += 1;
            // write out the tee or cup PlaceMark
            mo = super.get_mo().get(k);
            xmlSerializer.startTag(null, "Placemark");
            xmlSerializer.startTag(null, "name");
            xmlSerializer.text(mo.getTitle());
            xmlSerializer.endTag(null, "name");
            xmlSerializer.startTag(null, "description");
            xmlSerializer.cdsect("");
            xmlSerializer.endTag(null, "description");
            xmlSerializer.startTag(null, "styleUrl");
            xmlSerializer.text("#style" + Integer.toString(y));
            xmlSerializer.endTag(null, "styleUrl");
            xmlSerializer.startTag(null, "Point");
            xmlSerializer.startTag(null, "coordinates");
            ll = mo.getPosition();

            xmlSerializer.text(Double.toString(ll.longitude) + ","
                    + Double.toString(ll.latitude) + ",0.000000");
            xmlSerializer.endTag(null, "coordinates");
            xmlSerializer.endTag(null, "Point");
            xmlSerializer.endTag(null, "Placemark");

            // write out the polyline options
            // <Placemark>
            // <name>Hole One</name>
            // <description><![CDATA[]]></description>
            // <styleUrl>#style3</styleUrl>
            // <LineString>
            // <tessellate>1</tessellate>
            // <coordinates>
            // -xx.999999,xx.999999,0.000000
            // -xx.999999,xx.999999,0.000000
            // -xx.999999,xx.999999,0.000000
            // -xx.9999999,xx.999999,0.000000
            // -xx.9999999,xx.999999,0.000000
            // </coordinates>
            // </LineString>
            // </Placemark>if (hole > 0){
            if (k % 2 == 0) {
                int z = k / 2;
                if (z < posize) {
                    y += 1;
                    po = this.get_po().get(z);
                    xmlSerializer.startTag(null, "Placemark");
                    xmlSerializer.startTag(null, "name");
                    xmlSerializer.endTag(null, "name");
                    xmlSerializer.startTag(null, "description");
                    xmlSerializer.cdsect("");
                    xmlSerializer.endTag(null, "description");
                    xmlSerializer.startTag(null, "styleUrl");
                    xmlSerializer.text("#style" + Integer.toString(y));
                    xmlSerializer.endTag(null, "styleUrl");
                    xmlSerializer.startTag(null, "LineString");
                    xmlSerializer.startTag(null, "tessellate");
                    xmlSerializer.text("1");
                    xmlSerializer.endTag(null, "tessellate");
                    xmlSerializer.startTag(null, "coordinates");
                    sb.setLength(0);
                    for (LatLng ll3 : po.getPoints()) {
                        sb.append(Double.toString(ll3.longitude));
                        sb.append(",");
                        sb.append(Double.toString(ll3.latitude));
                        sb.append(",0.000000\n");
                    }
                    sb.setLength(sb.length() - 2);
                    String s = sb.toString();
                    xmlSerializer.text(s);
                    xmlSerializer.endTag(null, "coordinates");
                    xmlSerializer.endTag(null, "LineString");
                    xmlSerializer.endTag(null, "Placemark");
                }
            }
        }

        xmlSerializer.endTag(null, "Document");
        xmlSerializer.endTag(null, "kml");
        xmlSerializer.endDocument();
        xmlSerializer.flush();
        fileos.close();

    } catch (Exception e) {
        e.printStackTrace();
        // Log.e("Exception", "Exception occured in wroting");
    }

}

祝你好运

答案 2 :(得分:-1)

要在不使用应用程序的情况下将kml加载到Android上的Google地图中,您可以在某处创建一个小的html文件,其中包含指向kml文件的geo-uri链接,然后在任何Android网络浏览器中点击该链接。

例如:假设你的kml文件位于/sdcard/overlay.kml中,那么你写一个这样的geo-uri链接:

<html>
<head><title>Example KML link page using a geo-uri</title></head>
<body>
  <a href="geo:0,0?q=file:///sdcard/overlay.kml">overlay.kml</a>
</body>
</html>

点击该链接将启动地图,然后地图将尝试加载您的kml。

(显然,如果您的kml文件位于Web服务器上,则file://部分可以替换为http:// servername)

请注意,Android版本的地图似乎不会处理与桌面版(或桌面版Google地球版)相同的kml元素版本/范围。

相关问题