3

我有一个项目如下:几张照片是从手机上拍摄的,照片保存在一个网络系统中,然后在里面的谷歌地球上显示照片。我读过很多文章,但都在使用 fetchKml,我读过的一篇好文章是使用 php,但使用的是 fetchKml。我不知道是否可以使用 parseKml 代替。无论如何,我不确定如何使用 kml 执行此操作,所以它看起来像这样:

我的班级 KMLGenerator()

public static String getKMLFromObra (List<Obra> obraFotos) {
    StringBuffer sb = new StringBuffer();
    sb.append("<?xml version='1.0' encoding='UTF-8'?>");
    sb.append("<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'> ");

    if (obraFotos != null && obraFotos.size() > 0) {
        for (Obra o : obraFotos) {
            for (Local local : o.getLocais()) {
                sb.append("<Document>");  
                sb.append("<name>" + local.getName() + "</name>");  

                sb.append("<Style id='defaultStyles'>");  
                sb.append("<IconStyle>");  
                sb.append("<scale>1.1</scale>");
                sb.append("<Icon>");  
                sb.append("<href>" + "http://localhost:8080/ConstruMobilFoto/lib/img/fotoIcon.png" + "</href>");  
                sb.append("</Icon>");  
                sb.append("</IconStyle>");  
                sb.append("</Style>");  

                sb.append("<Placemark>");  
                sb.append("<name>" + "Foto" + "</name>");  
                sb.append("<styleUrl>" + "#defaultStyles"+ "</styleUrl>");  
                sb.append("<altitudeMode>" + "relativeToGround" + "</altitudeMode>");  
                sb.append("<Point>");
                sb.append("<altitudeMode>relativeToGround</altitudeMode>");
                sb.append("<coordinates>" + local.getLongitude() + "," + local.getLatitude() + "," + 50</coordinates>");
                sb.append("</Point>");
                sb.append("<Link>");
                sb.append("<href>" + local.getFotos() + "</href>");
                sb.append("</Link>");

                sb.append("</Placemark>");  
                sb.append("</Document>");  
                sb.append("</kml>");  
                return sb.toString();
            }
        }
    }
    return null;
}

我的 jsp 中有一个 dwr 函数,它调用这个方法,得到它的字符串结果并像这样进行解析:

类 PainelEarth()

@SuppressWarnings("static-access")
public String geraFotosObra (int idObra) throws Exception {
    try {
        List<Obra> obraFotos = obraBusiness.getObraLatLong(new Obra(idObra));
        return new KMLGenerator().getKMLFromObra(obraFotos);
    } catch (Exception e) {
        log.error(e.getLocalizedMessage(), e);
        return null;
    }
}

在我的jsp页面中

function initCB(instance) {
    // other codes

    showPics(ge);
}

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

欢迎任何帮助!

4

1 回答 1

1

在您的代码中,如果您查看方法的签名,geraFotosObra您会发现它采用单个int参数idObra

public String geraFotosObra (int idObra) throws Exception { ...

然而,当您在 jsp 中调用该方法时,您传递的是一个包含回调函数的对象文字。

PainelEarthAjax.geraFotosObra({
        callback : function(kmlString) { ...

因为它是我不知道如何生成 kml,除非可能geraFotosObra是一个重载的方法?此外,即使它是按原样生成的,我也看不到您传递的代替 id 的回调函数是如何被调用的——例如,为什么会kmlString是调用的结果geraFotosObra

// How is this called, what sets kmlString!?
callback : function(kmlString) {
            var kmlObject = ge.parseKml(kmlString);
            ge.getFeatures().appendChild(kmlObject);
        }

总而言之,您发布的所有代码有点令人困惑,如果我遗漏了一些东西,很抱歉......我认为您可能从 fetchKml 示例中复制并粘贴了一些代码,并且与该方法一起使用的异步回调让您感到困惑。

无论如何,根据您发布的内容,您应该将一个传递int id给该geraFotosObra方法,获取字符串结果,然后在插件中对其进行解析。

像下面这样的东西是有道理的。将showPics函数替换为以下内容。

function showPics(ge) {
  var kmlString = PainelEarthAjax.geraFotosObra(1); // or something?
  var kmlObject = ge.parseKml(kmlString);
  ge.getFeatures().appendChild(kmlObject);
}
于 2012-02-27T06:54:17.180 回答