我的代码将 kml 解析到 Google Earth 时有什么问题?

发布于 2025-01-04 16:06:19 字数 3839 浏览 2 评论 0原文

无法将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

你丑哭了我 2025-01-11 16:06:19

getKMLFromObra() 函数未返回有效的 XML:

开头 > code> 标签:

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

应该是

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

为了避免在生成的 XML 中出现此类错误,您应该考虑使用专门的 java 类而不是字符串连接,以下是使用 DOMSAX 生成 XML 的一些示例代码> : http://www.javazoom.net/services/newsletter/xml Generation.html

The getKMLFromObra() function doesn't return a valid XML :

The closing bracket > is missing in the opening <kml ...> tag :

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

should be

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

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 or SAX : http://www.javazoom.net/services/newsletter/xmlgeneration.html

爱你是孤单的心事 2025-01-11 16:06:19

showPics() 不返回结果。您在回调函数中返回结果。试试这个:

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);  

showPics() does not return a result. you return your result inside the callback function. Try this:

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

Then call it up like this:

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

showPics(ge);  
黑色毁心梦 2025-01-11 16:06:19

您有一个额外的 关闭标记和一个浮动 关闭标记,但没有实际的 Model 元素。

您可以使用 XML 验证器检查输出,例如 Oxygen 甚至仅使用 TextMate 中的 XML 包。

You have an extra </Style> close tag, and a floating </Model> close tag, with no actual Model element.

You can check output with an XML validator, something like Oxygen or even just using the XML bundle in TextMate.

一抹微笑 2025-01-11 16:06:19

我收到消息“kml 未定义!”

我认为这是 jsp 上的解析问题,我更改了 getKMLFromObra() 只是为了测试,我插入下面的代码:

PS:我在 kml Interactive 上测试了这个 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);

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()

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();

My google_earth.jsp below:

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文