2012-02-13 38 views
0

在无法解析我的GE的kml,我的代码有什么问题?我的代码将kml解析为Google Earth时出了什么问题?

我被困在这2周,并试图自己做许多其他的方式,

任何帮助将受到欢迎,

拉斐尔耶稣

google_earth.jsp

var ge; 

google.load("earth", "1"); 

function init() { 
    google.earth.createInstance('map3d', initCB, failureCB); 
    window.scroll(0, 10000); 
} 

function initCB(instance) { 
    ge = instance; 
    ge.getWindow().setVisibility(true); 

    // add a navigation control 
    ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO); 

    // add some layers 
    ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, true); 
    ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, true); 

    // directs the exact location of the placemark 
    var lookAt = ge.getView().copyAsLookAt(ge.ALTITUDE_RELATIVE_TO_GROUND); 
    lookAt.setLatitude(-15.26108113514467); 
    lookAt.setLongitude(-57.77290131815782); 
    lookAt.setRange(8007066.726300671); 

    ge.getView().setAbstractView(lookAt); 
    ge.getLayerRoot().enableLayerById(ge.LAYER_TERRAIN, false); 

    var kmlString = showPics(); 
    // I put a alert in here and show in a window the value "undefined" 
    var kmlObject = ge.parseKml(kmlString); 
    ge.getFeatures().appendChild(kmlObject); 

}

// dwr function that brings the kml with his values 
      // for now it is in hard coded, just for tests!! 
    function showPics() { 
     PainelEarthAjax.geraFotosObra({ 
      callback : function(kmlString) { 
          // I put a alert function in here, and it has openned a window 
          // with the entire kmlString brought from the java method geraFotosObra(). 
        return kmlString; 
      } 
     }); 
    } 

function failureCB(errorCode) { 

}  
google.setOnLoadCallback(init); 

geraFotosObra.java

public String geraFotosObra() throws Exception { 
    try { 
     return new KMLGenerator().getKMLFromObra(); 
    } catch (Exception e) { 
     log.error(e.getLocalizedMessage(), e); 
     return null; 
    } 
} 

KMLGenerator.java

public static String getKMLFromObra() { 
    StringBuffer sb = new StringBuffer(); 
    sb.append("<?xml version='1.0' encoding='UTF-8'?>"); 
    sb.append("<kml xmlns='http://www.opengis.net/kml/2.2' "); 

    sb.append("<Document>"); 
    sb.append("<name>ConstruMobil</name>"); 

    sb.append("<Style id='defaultStyles'>"); 
    sb.append("<IconStyle>"); 
    sb.append("<Icon>"); 
    sb.append("<href>" + "http://maps.google.com/mapfiles/kml/pal4/icon38.png" + "</href>"); 
    sb.append("</Icon>"); 
    sb.append("</IconStyle>"); 
    sb.append("</Style>"); 
    sb.append("</Style>"); 

    sb.append("<Placemark>"); 
    sb.append("<name>" + "Some name" + "</name>"); 
    sb.append("<styleUrl>" + "#defaultStyles"+ "</styleUrl>"); 
    sb.append("<altitudeMode>" + "relativeToGround" + "</altitudeMode>"); 

    sb.append("<Location>"); 
    sb.append("<longitude>" + -122.3599987260313 + "</longitude>"); 
    sb.append("<latitude>" + 47.62949781133496 + "</latitude>"); 
    sb.append("<altitude>"+ 15.49615401024533 + "</altitude>"); 
    sb.append("</Location>"); 

    sb.append("<Link>"); 
    sb.append("<href>" + "http://localhost:8080/myCompany/lib/img/dubai.jpg" + "</href>"); 
    sb.append("</Link>"); 
    sb.append("</Model>"); 
    sb.append("</Placemark>"); 

    sb.append("</Document>"); 
    sb.append("</kml>"); 

    return sb.toString(); 
} 
+1

你的错误是什么?你能缩小它吗? – ArtB 2012-02-13 12:47:57

+0

将您的方法的kml输出粘贴到文本文件中,并很好地识别它,然后您会看到它被盗用。我看到的第一个明显的是你有一个/ Model(endtag)没有对应的starttag – Eddy 2012-02-13 19:26:01

+0

男人tks第一,我得到了msg“kml是undefined”,我减少了我的kml只是为了测试,我得到了那个消息,我已经已经在kml interactive中测试过了,我在我的应用程序中得到了同样的kml,它在那里工作 – 2012-02-14 18:01:02

回答

0

showPics()没有返回结果。您在callback函数中返回结果。试试这个:

function showPics(ge) { 
    PainelEarthAjax.geraFotosObra({ 
     callback : function(kmlString) { 
      var kmlObject = ge.parseKml(kmlString); 
      ge.getFeatures().appendChild(kmlObject); 
     } 
    }); 
} 

然后调用它像这样:

ge.getView().setAbstractView(lookAt); 
ge.getLayerRoot().enableLayerById(ge.LAYER_TERRAIN, false); 

showPics(ge); 
+0

感谢您的回复, 我已经修复了“>”问题,现在我收到了消息“kml未定义” 我已经测试过[在此处输入链接描述] [1] [1]:http:// kml-samples.googlecode.com/svn/trunk/interactive/index.html 它工作得很好,它显示我的照片在测试中, 我几乎在那里,还有什么? Tks很多 – 2012-02-13 17:50:08

+0

你有没有试过我建议的? – epoch 2012-02-14 06:20:33

+0

嗨时代,你可以提出一些建议吗? [链接] http://stackoverflow.com/questions/9329945/whats-the-best-pratice-to-show-multiple-placemarks-with-photos-inside-of-the-bal – 2012-02-17 14:29:06

1

getKMLFromObra()函数不返回一个有效的XML:

结束括号>在开幕<kml ...>标签缺失:

sb.append("<kml xmlns='http://www.opengis.net/kml/2.2' "); 

应该是

sb.append("<kml xmlns='http://www.opengis.net/kml/2.2'>"); 

为了避免这样的错误在生成的XML,你应该考虑使用,而不是字符串连接专门的Java类,这里是XML生成的一些为例与DOMSAXhttp://www.javazoom.net/services/newsletter/xmlgeneration.html

0

你有一个额外</Style>关闭标记以及浮动</Model>关闭标记,但没有实际的Model元素。

您可以使用XML验证程序(如Oxygen或甚至仅在TextMate中使用XML包)来检查输出。

+0

我已经修复它马诺,现在这个消息是“kml is undefined!”,但是我的kml是好的,因为我已经在kml interetility上测试过了,任何帮助都会受到欢迎..您可以使用 – 2012-02-14 18:02:48

+0

来验证它是否符合OGC KML架构? http://schemas.opengis.net/kml/2.2.0/ogckml22.xsd – 2012-02-14 18:04:52

+0

马诺我没有明白,你是什么意思validationg模式?我所知道的是验证Shema是当你使用Web服务的时候,我该怎么做,对于缺乏知识感到抱歉! – 2012-02-14 19:13:21

0

我收到消息“kml is undefined!”

我认为这是在JSP解析的问题,我已经改变了我的getKMLFromObra()只是为了测试,我插入下面的代码:

PS:我测试这个KML KML上的互动,并在那里工作,

getKMLFromObra()

StringBuffer sb = new StringBuffer(); 
    sb.append("<?xml version='1.0' encoding='UTF-8'?>"); 
    sb.append("<kml xmlns='http://www.opengis.net/kml/2.2' >"); 
    sb.append("<Placemark>"); 
    sb.append("<name>Simple placemark</name>"); 
    sb.append("<description>testing fucking kml</description>"); 
    sb.append("<Point>"); 
    sb.append("<coordinates>-122.0822035425683,37.42228990140251,0</coordinates>"); 
    sb.append("</Point>"); 
    sb.append("</Placemark>"); 
    sb.append("</kml>"); 
    return sb.toString(); 

我google_earth。jsp如下:

var ge; 

google.load("earth", "1"); 

function init() { 
    google.earth.createInstance('map3d', initCB, failureCB); 
    window.scroll(0, 10000); 

} 

function initCB(instance) { 
    ge = instance; 
    ge.getWindow().setVisibility(true); 

    // add a navigation control 
    ge.getNavigationControl().setVisibility(ge.VISIBILITY_AUTO); 

    // add some layers 
    ge.getLayerRoot().enableLayerById(ge.LAYER_BORDERS, true); 
    ge.getLayerRoot().enableLayerById(ge.LAYER_ROADS, true); 
    ge.getLayerRoot().enableLayerById(ge.LAYER_TERRAIN, false); 

    showPics(ge); 
} 

function showPics(ge) { 
    PainelEarthAjax.geraFotosObra({ 
     callback : function(kmlString) { 
      var kmlObject = ge.parseKml(kmlString); 
      ge.getFeatures().appendChild(kmlObject); 
     } 
    }); 
    return null; 
} 

function failureCB(errorCode) { 

} 
google.setOnLoadCallback(init); 
相关问题