2013-08-24 80 views

回答

-1

要在不編寫應用程序的情況下將kml加載到Google地圖上,可以在地理位置創建一個帶有geo-uri鏈接到kml文件的小文件,然後在任意Android Web瀏覽器中單擊該鏈接。

例如:假設您的KML文件位於/sdcard/overlay.kml那麼你寫一個這樣的地理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> 

點擊鏈接可啓動Google地圖,然後地圖就會嘗試加載和你的kml。

(顯然,如果您的KML文件是一個Web服務器上的文件://部分可以用HTTP替換://服務器),但是

被警告說,地圖的Android版本沒有出現來處理與桌面版本(或桌面Google地球)相同的版本/ kml元素範圍。

0

我把所有東西放到一個類中,而我正在解析kml進來。我使用第二個應用程序中的第一個應用程序中的同一個類。只是延長了原來的課程。對於我來說,從另一個應用程序100%重用一個類並使用超級操作碼非常酷。

反正。基本上它是一個多段線選項列表和一個標記選項列表。

super.get_po這是折線選項 super.get_mo這是標記選項

然後我序列化類退了出來,當我在寫KML。我的高爾夫應用取決於標記和線條的順序。對於每個孔它會去標記,線條和標記,我使用與每個元素都有自己的樣式的http://maps.google.com相同的樣式。

my golf app它有一個驚人的地球破碎兩個下載我,可能我的wifes手機。

@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"); 
    } 

} 

好運

+0

只要注意,如果你沒有正確關閉標籤,xmlserializer將會發生異常。 – danny117

1

如果您希望藉助谷歌地圖線,多邊形,圓形等,只需用www.gmapgis.com該應用程序允許的圖紙爲KML文件導出。