2012-07-14 56 views
2

我正在开发GPS应用程序,我想根据WGS84基准将我的纬度和经度转换为x-y坐标。我发现这个js页面执行此操作:http://www.uwgb.edu/dutchs/usefuldata/ConvertUTMNoOZ.HTM。我也看了一下其他计算器,但这个更准确。现在,如果您看到源代码,则正在提供执行转换的功能。我googled我怎么能在Java中使用js代码,并发现这个库可以用来做到这一点:http://www.mozilla.org/rhino/。我将jar添加到了我的eclipse项目中,并从此处运行示例(http://www.mozilla.org/rhino/examples.html),但无法弄清楚如何使用该页面源代码中的函数。如何使用Rhino库在Java中使用JavaScript代码?

function GeogToUTM(){ 
    //Convert Latitude and Longitude to UTM 
    Declarations(); 
    k0 = 0.9996;//scale on central meridian 
    b = a*(1-f);//polar axis. 
    //alert(a+" "+b); 
    //alert(1-(b/a)*(b/a)); 
    e = Math.sqrt(1 - (b/a)*(b/a));//eccentricity 
    //alert(e); 
    //Input Geographic Coordinates 
    //Decimal Degree Option 
    latd0 = parseFloat(document.getElementById("DDLatBox0").value); 
    lngd0 = parseFloat(document.getElementById("DDLonBox0").value); 
    latd1 = Math.abs(parseFloat(document.getElementById("DLatBox0").value)); 
    latd1 = latd1 + parseFloat(document.getElementById("MLatBox0").value)/60; 
    latd1 = latd1 + parseFloat(document.getElementById("SLatBox0").value)/3600; 
    if (parseFloat(document.getElementById("DLatBox0").value)<0){latd1=-latd1;} 
    lngd1 = Math.abs(parseFloat(document.getElementById("DLonBox0").value)); 
    lngd1 = lngd1 + parseFloat(document.getElementById("MLonBox0").value)/60; 
    lngd1 = lngd1 + parseFloat(document.getElementById("SLonBox0").value)/3600; 
    if (parseFloat(document.getElementById("DLonBox0").value)<0){lngd1=-lngd1;} 

    lngd=lngd0; 
    latd=latd0; 
    if(isNaN(latd)){ 
    latd = latd1; 
    document.getElementById("DDLatBox0").value = Math.floor(1000000*latd)/1000000; 
    lngd=lngd1; 
    document.getElementById("DDLonBox0").value = Math.floor(1000000*lngd)/1000000; 
    } 

    if(isNaN(lngd)){lngd = latd1;} 
     if(isNaN(latd)|| isNaN(lngd)){ 
     alert("Non-Numeric Input Value"); 
     } 
    if(latd <-90 || latd> 90){ 
     alert("Latitude must be between -90 and 90"); 
     } 
    if(lngd <-180 || lngd > 180){ 
     alert("Latitude must be between -180 and 180"); 
     } 

    xd = lngd; 
    yd = latd; 
    DDtoDMS(); 
    //Read Input from DMS Boxes 
    document.getElementById("DLatBox0").value = Math.floor(ydd); 
    document.getElementById("MLatBox0").value = ym; 
    document.getElementById("SLatBox0").value = Math.floor(1000*ys)/1000; 
    document.getElementById("DLonBox0").value = Math.floor(xdd); 
    document.getElementById("MLonBox0").value = xm; 
    document.getElementById("SLonBox0").value = Math.floor(1000*xs)/1000; 


    phi = latd*drad;//Convert latitude to radians 
    lng = lngd*drad;//Convert longitude to radians 
    utmz = 1 + Math.floor((lngd+180)/6);//calculate utm zone 
    latz = 0;//Latitude zone: A-B S of -80, C-W -80 to +72, X 72-84, Y,Z N of 84 
    if (latd > -80 && latd < 72){latz = Math.floor((latd + 80)/8)+2;} 
    if (latd > 72 && latd < 84){latz = 21;} 
    if (latd > 84){latz = 23;} 

    zcm = 3 + 6*(utmz-1) - 180;//Central meridian of zone 
    //alert(utmz + " " + zcm); 
    //Calculate Intermediate Terms 
    e0 = e/Math.sqrt(1 - e*e);//Called e prime in reference 
    esq = (1 - (b/a)*(b/a));//e squared for use in expansions 
    e0sq = e*e/(1-e*e);// e0 squared - always even powers 
    //alert(esq+" "+e0sq) 
    N = a/Math.sqrt(1-Math.pow(e*Math.sin(phi),2)); 
    //alert(1-Math.pow(e*Math.sin(phi),2)); 
    //alert("N= "+N); 
    T = Math.pow(Math.tan(phi),2); 
    //alert("T= "+T); 
    C = e0sq*Math.pow(Math.cos(phi),2); 
    //alert("C= "+C); 
    A = (lngd-zcm)*drad*Math.cos(phi); 
    //alert("A= "+A); 
    //Calculate M 
    M = phi*(1 - esq*(1/4 + esq*(3/64 + 5*esq/256))); 
    M = M - Math.sin(2*phi)*(esq*(3/8 + esq*(3/32 + 45*esq/1024))); 
    M = M + Math.sin(4*phi)*(esq*esq*(15/256 + esq*45/1024)); 
    M = M - Math.sin(6*phi)*(esq*esq*esq*(35/3072)); 
    M = M*a;//Arc length along standard meridian 
    //alert(a*(1 - esq*(1/4 + esq*(3/64 + 5*esq/256)))); 
    //alert(a*(esq*(3/8 + esq*(3/32 + 45*esq/1024)))); 
    //alert(a*(esq*esq*(15/256 + esq*45/1024))); 
    //alert(a*esq*esq*esq*(35/3072)); 
    //alert(M); 
    M0 = 0;//M0 is M for some origin latitude other than zero. Not needed for standard UTM 
    //alert("M ="+M); 
    //Calculate UTM Values 
    x = k0*N*A*(1 + A*A*((1-T+C)/6 + A*A*(5 - 18*T + T*T + 72*C -58*e0sq)/120));//Easting relative to CM 
    x=x+500000;//Easting standard 
    y = k0*(M - M0 + N*Math.tan(phi)*(A*A*(1/2 + A*A*((5 - T + 9*C + 4*C*C)/24 + A*A*(61 - 58*T + T*T + 600*C - 330*e0sq)/720))));//Northing from equator 
    yg = y + 10000000;//yg = y global, from S. Pole 
    if (y < 0){y = 10000000+y;} 
    //Output into UTM Boxes 
    document.getElementById("UTMzBox1").value = utmz; 
    document.getElementById("UTMeBox1").value = Math.round(10*(x))/10; 
    document.getElementById("UTMnBox1").value = Math.round(10*y)/10; 
    if (phi<0){document.getElementById("SHemBox").checked=true;} 
    //document.getElementById("UTMzBox1").value = utmz; 
    //document.getElementById("UTMeBox1").value = Math.round(10*(500000+x))/10; 
    document.getElementById("UTMLonZoneBox2").value = utmz; 
    document.getElementById("UTMLatZoneBox2").value = DigraphLetrsE[latz]; 
    document.getElementById("UTMeBox2").value = Math.round(10*(x-100000*Math.floor(x/100000)))/10; 
    document.getElementById("UTMnBox2").value = Math.round(10*(y-100000*Math.floor(y/100000)))/10; 
//Generate Digraph 
    MakeDigraph(); 
    document.getElementById("UTMDgBox2").value = Digraph; 

}//close Geog to UTM 
/////////////////////////////////////////////////////////////////////// 

我知道我不能使用这个函数,因为它嵌入在HTML中。但是我从来没有在js上工作过,所以如果我需要对代码进行最小限度的更改,这对我来说会更容易。

回答

1

如果你只是想运行JavaScript和你不特别在意使用犀牛与更通用的Java脚本API,请参阅下面的博客文章我写:

http://springinpractice.com/2012/05/13/how-to-run-javascript-from-java/

事实上,如果你想不管是什么原因实际犀牛代码,这是一个使用Rhino的一些示例代码:

https://github.com/springinpractice/sip09/blob/03/src/main/java/com/springinpractice/ch09/comment/service/impl/RichTextFilter.java

+0

第二个链接处于脱机状态。 – 2013-08-17 12:01:09

+0

固定,谢谢Davide。 – 2013-08-17 23:24:40

1

你要问,如果你可以先使用它 - 但他说他是GE很乐意让人们有权使用他的作品。

所有对document.getElementById的调用都是指html页面上的输入。 您必须重写该函数,以便将这些变量传递给它 - 因为在运行它时无法访问javascript dom。

你必须找出你想要它返回的东西,它看起来像在几个字段中显示计算值,所以你可能必须返回一个字段名称的映射到计算的值 - 然后你将在你的java程序中使用结果。

您发布的脚本也引用了这些缺少的功能,因此您也必须找到它们,并将它们包含在您的脚本中:
MakeDigraph(); DDtoDMS(); 声明();

我建议你删除代码的确认位与警报和公正处理,这是你的Java代码

这不会是非常有效的,但如果不要紧那么好运气!

相关问题