我的代码将 kml 解析到 Google Earth 时有什么问题?
无法将 kml 解析到我的 GE,我的代码有什么问题吗?
我被困在这个问题上两周了,并尝试自己做许多其他方法,
欢迎任何帮助,
Rafael Jesus
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();
}
In can't parse the kml to my GE, whats wrong with my code?
I'm stuck at this for 2 weeks and tried myself to do many others ways,
Any help will be welcome,
Rafael Jesus
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();
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
getKMLFromObra()
函数未返回有效的 XML:开头
>
code> 标签:应该是
为了避免在生成的 XML 中出现此类错误,您应该考虑使用专门的 java 类而不是字符串连接,以下是使用
DOM
或SAX
生成 XML 的一些示例代码> : http://www.javazoom.net/services/newsletter/xml Generation.htmlThe
getKMLFromObra()
function doesn't return a valid XML :The closing bracket
>
is missing in the opening<kml ...>
tag :should be
To avoid errors like this in the generated XML you should consider using specialized java classes instead of String concatenation, here are some exemple of XML generation with
DOM
orSAX
: http://www.javazoom.net/services/newsletter/xmlgeneration.htmlshowPics()
不返回结果。您在回调函数中返回结果。试试这个:然后像这样调用它:
showPics()
does not return a result. you return your result inside thecallback
function. Try this:Then call it up like this:
您有一个额外的
关闭标记和一个浮动
关闭标记,但没有实际的
Model
元素。您可以使用 XML 验证器检查输出,例如 Oxygen 甚至仅使用 TextMate 中的 XML 包。
You have an extra
</Style>
close tag, and a floating</Model>
close tag, with no actualModel
element.You can check output with an XML validator, something like Oxygen or even just using the XML bundle in TextMate.
我收到消息“kml 未定义!”
我认为这是 jsp 上的解析问题,我更改了 getKMLFromObra() 只是为了测试,我插入下面的代码:
PS:我在 kml Interactive 上测试了这个 kml 并且它在那里工作,
getKMLFromObra()
下面是我的 google_earth.jsp :
I got the message "kml is undefined!"
I think it is a parse problem on jsp, i've changed my getKMLFromObra() just for tests, i insert the code below:
PS: I tested this kml on kml interactive and it worked there,
getKMLFromObra()
My google_earth.jsp below: