kml文件android编程

时间:2018-03-14 18:04:23

标签: android kml

我在android studio上使用google maps api试图在我的地图中显示一个kml文件。

在我的Kml上,每个多边形都有像这样的儿子值

<ExtendedData><SchemaData schemaUrl="#Zonas_VDP21_Marzo_18">
    <SimpleData name="Ciudad">SAN ANTONIO CORONADO</SimpleData>
    <SimpleData name="Agente">VDP21</SimpleData>
    <SimpleData name="ZIP">NORESTE</SimpleData>
    <SimpleData name="PROVINCIA">1</SimpleData>
    <SimpleData name="GerenciaGe">CMD3</SimpleData>
    <SimpleData name="ID Bolsa_1">CI277</SimpleData>
    <SimpleData name="DTA Distri">11104</SimpleData>
</SchemaData></ExtendedData>

我要做的是在点击它之后让每个多边形显示它的数据。像谷歌地图在线做(https://www.google.com/maps/d/u/0)。

Google Maps Example

但我可以像这样显示kml图层:

Android Map

这没关系,但我无法点击任何多边形来访问其信息这是我的代码

public void DownloadFiles(){

    try {
        URL u = new URL("http://XXX.XXX.XXX.XXX:XXXX/XXXXXX/XXXXXX/XXXX.kml");
        final InputStream is = u.openStream();
        layer = new KmlLayer(map, is, getApplicationContext());
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                try {


                    layer.addLayerToMap();
                    layer.setOnFeatureClickListener(new KmlLayer.OnFeatureClickListener() {
                        @Override
                        public void onFeatureClick(com.google.maps.android.data.Feature feature) {
                            Toast.makeText(MapsActivity.this,
                                    "Feature clicked: " + feature.getId(),
                                    Toast.LENGTH_SHORT).show();
                        }
                    });

                } catch (IOException e) {
                    e.printStackTrace();
                } catch (XmlPullParserException e) {
                    e.printStackTrace();
                } catch (Exception ex){
                    ex.getMessage();
                }
            }
        });


    } catch (MalformedURLException mue) {
        Log.e("SYNC getUpdate", "malformed url error", mue);
    } catch (IOException ioe) {
        Log.e("SYNC getUpdate", "io error", ioe);
    } catch (SecurityException se) {
        Log.e("SYNC getUpdate", "security error", se);
    } catch (XmlPullParserException e) {
        e.printStackTrace();
    }
}

记住这一点: 1.地图正确加载并在我的应用上创建。 2.我的网络服务正确下载了kml文件。 3. kml文件格式是正确的。

1 个答案:

答案 0 :(得分:0)

在KML中,您可以将共享气球模板应用于所有要素(地标,点,线,多边形等)。你可以使用共享的BalloonStyle来实现这个目标。用于所需气球布局的CSS,以及用于拉入当前要素数据的变量(例如:$ [my_variable])。请参阅下面示例KML中<BalloonStyle>标记的内容,该标记使用您提供的示例数据。

我不确定在Android上使用它是否能完全解决您的问题,但这就是您在KML中执行此操作的方式,以便在Google地球和地图中查看。

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/kml/2.2 http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd http://www.google.com/kml/ext/2.2 https://developers.google.com/kml/schema/kml22gx.xsd http://www.w3.org/2005/Atom http://schemas.opengis.net/kml/2.2.0/atom-author-link.xsd">
  <Document id="root">
    <name>Polygon Balloon Example</name>
    <open>1</open>

    <Style id="my_style_id">
      <LineStyle>
        <color>ff0000ff</color>
        <width>2</width>
      </LineStyle>
      <PolyStyle>
        <color>770000ff</color>
        <fill>1</fill>
        <outline>1</outline>
      </PolyStyle>
      <BalloonStyle>
        <text><![CDATA[
          <html>
          <head>
            <meta charset="UTF-8" />
            <style>
            .rTable {
                display: table;
                width: 100%;
            }
            .rTableBody {
                display: table-row-group;
            }
            .rTableRow {
                display: table-row;
            }
            .rTableCell {
                display: table-cell;
                padding: 3px 10px;
                border: 0px;
            }
            .dataName {
                font-weight: bold;
            }
            </style>
          </head>
          <body>
            <h1>$[name]</h1>
            <div class="rTable">
              <div class="rTableBody">
                <div class="rTableRow">
                  <div class="rTableCell"><span class="dataName">$[Zonas/Ciudad/displayName]</span></div>
                  <div class="rTableCell">$[Zonas/Ciudad]</div>
                </div>
                <div class="rTableRow">
                  <div class="rTableCell"><span class="dataName">$[Zonas/Agente/displayName]</span></div>
                  <div class="rTableCell">$[Zonas/Agente]</div>
                </div>
                <div class="rTableRow">
                  <div class="rTableCell"><span class="dataName">$[Zonas/ZIP/displayName]</span></div>
                  <div class="rTableCell">$[Zonas/ZIP]</div>
                </div>
                <div class="rTableRow">
                  <div class="rTableCell"><span class="dataName">$[Zonas/PROVINCIA/displayName]</span></div>
                  <div class="rTableCell">$[Zonas/PROVINCIA]</div>
                </div>
                <div class="rTableRow">
                  <div class="rTableCell"><span class="dataName">$[Zonas/GerenciaGe/displayName]</span></div>
                  <div class="rTableCell">$[Zonas/GerenciaGe]</div>
                </div>
                <div class="rTableRow">
                  <div class="rTableCell"><span class="dataName">$[Zonas/ID Bolsa_1/displayName]</span></div>
                  <div class="rTableCell">$[Zonas/ID Bolsa_1]</div>
                </div>
                <div class="rTableRow">
                  <div class="rTableCell"><span class="dataName">$[Zonas/DTA Distri/displayName]</span></div>
                  <div class="rTableCell">$[Zonas/DTA Distri]</div>
                </div>
              </div>
            </div>
          </body>
          </html>
        ]]></text>
      </BalloonStyle>
    </Style>    

    <Schema name="Zonas" id="Zonas_VDP21_Marzo_18">   
      <SimpleField type="string" name="Ciudad">     
        <displayName>Ciudad</displayName>   
      </SimpleField>
      <SimpleField type="string" name="Agente">     
        <displayName>Agente</displayName>   
      </SimpleField>
      <SimpleField type="string" name="ZIP">     
        <displayName>ZIP</displayName>   
      </SimpleField>
      <SimpleField type="string" name="PROVINCIA">     
        <displayName>PROVINCIA</displayName>   
      </SimpleField>
      <SimpleField type="string" name="GerenciaGe">     
        <displayName>GerenciaGe</displayName>   
      </SimpleField>
      <SimpleField type="string" name="ID Bolsa_1">     
        <displayName>ID Bolsa_1</displayName>   
      </SimpleField>
      <SimpleField type="string" name="DTA Distri">     
        <displayName>DTA Distri</displayName>   
      </SimpleField>
    </Schema> 

    <Placemark id="myPlace">
      <name>Sample Polygon</name>
      <snippet></snippet>
      <description><![CDATA[description_HTML]]></description>
      <styleUrl>#my_style_id</styleUrl>
      <ExtendedData><SchemaData schemaUrl="#Zonas_VDP21_Marzo_18">
        <SimpleData name="Ciudad">SAN ANTONIO CORONADO</SimpleData>
        <SimpleData name="Agente">VDP21</SimpleData>
        <SimpleData name="ZIP">NORESTE</SimpleData>
        <SimpleData name="PROVINCIA">1</SimpleData>
        <SimpleData name="GerenciaGe">CMD3</SimpleData>
        <SimpleData name="ID Bolsa_1">CI277</SimpleData>
        <SimpleData name="DTA Distri">11104</SimpleData>
      </SchemaData></ExtendedData>      
      <Polygon>
        <outerBoundaryIs>
          <LinearRing>
            <coordinates>0,0,0 0,10,0 10,10,0 10,0,0 0,0,0</coordinates>
          </LinearRing>
        </outerBoundaryIs>
      </Polygon>
    </Placemark>

  </Document>
</kml>