如何让这个javascript函数自动运行?

时间:2011-04-19 07:49:27

标签: php javascript ajax latitude-longitude

我正在尝试使用经度/纬度转换器来显示OSGR值。这是我目前的代码:

    <?php

$longitude = "-0.310150 N";
$latitude = "59.688930 W";
?>

<script language="JavaScript" type="text/JavaScript">
<onload = function() {
           CalculateOSGB();
         };
</script>

<script language="JavaScript" type="text/JavaScript">
function ELMNT(elementName)
{
  return document.getElementById(elementName);
}

function CalculateLL()
{
  ENEN = new EastNorth;
  RefToCoords ( ELMNT("OSRef").value, ENEN );

//  ELMNT("OSRef").value = ELMNT("OSRef").value + ENEN.Easting + " " + ENEN.Northing + "\t";
  CoordsToLL(ENEN,LatLong);

  ELMNT("latitude").value = LatLong.Lat;
  ELMNT("longitude").value = LatLong.Long;
//  ELMNT("OSRef").value = ELMNT("OSRef").value + LatLong.Lat + "\t" + LatLong.Long + "\t" + ELMNT("OSRef").value + "\n";
}

function RefToCoords ( InRef, EastNorth )
{
    var  Length1 = 0;
    var  Length2 = 0;
    var  Index;
    Numbers  = new String("");
    Letters  = new String("");
    Easting  = new Array("");
    Northing = new Array("");
    Grid_X = new Array(   0, 100, 200, 300, 400,   0, 100, 200,   0, 300, 400,   0, 100, 200, 300, 400,   0, 100, 200, 300, 400,  0, 100, 200, 300, 400, 0);
    Grid_Y = new Array( 400, 400, 400, 400, 400, 300, 300, 300,   0, 300, 300, 200, 200, 200, 200, 200, 100, 100, 100, 100, 100,  0,   0,   0,   0,   0, 0);
    Lookup = new String("                                                                 ABCDEFGHIJKLMNOPQRSTUVWXYZ");

    Ref = new String(InRef);
    Ref.toUpperCase();

    Length1 = Ref.length;
    for ( i=0; i<Length1; i++ )
    {
        if ( Ref.charAt(i).toUpperCase() >= 'A' && Ref.charAt(i).toUpperCase() <= 'Z' )
            Letters = Letters + Ref.charAt(i).toUpperCase();
        else
            if ( Ref.charAt(i) >= '0' && Ref.charAt(i) <= '9' )
                Numbers = Numbers + Ref.charAt(i);
    }

    if ( Letters.length != 2 )
    {
        ELMNT("OSRef").value = ELMNT("OSRef").value + "ERROR: OS Ref. Letters must be 2 long\n";
        return -1;
    }

    Length2 = Numbers.length;
    if ( !Length2 || Length2 & 0x01 )
    {
        ELMNT("OSRef").value = ELMNT("OSRef").value + "ERROR: OS Ref. Must not be odd number of numbers\n";
        return -1;
    }

    Length2 = Length2 / 2;
    Length2 = Math.floor(Length2)

    Easting = Numbers.slice(0,Length2);
    Easting = Easting + "00000";

    Northing = Numbers.slice(5,5+Length2);
    Northing = Northing + "00000";

    Index = Lookup.indexOf(Letters.charAt(0)) - 65;
    EastNorth.Easting = 5000 * Grid_X[Index];
    EastNorth.Northing = 5000 * Grid_Y[Index];

    Easting= Easting /100000;
    Northing = Northing / 100000;
    Index = Lookup.indexOf(Letters.charAt(1)) - 65;
    EastNorth.Easting = EastNorth.Easting + ( Grid_X[Index] * 1000 );
    EastNorth.Northing = EastNorth.Northing + ( Grid_Y[Index] * 1000 );

    EastNorth.Easting = EastNorth.Easting + ( parseInt(Easting) - 1000000 );
    EastNorth.Northing = EastNorth.Northing + ( parseInt(Northing) - 500000 );

    return 0;
}

function LLToCoords(LatLong,EastNorth)
{
  var dLongI = 0.0; 
  var dLatI  = 0.0;
  var Rad    = 0.0174532925199432957692369076848;

  dLongI = Rad * (parseFloat ( LatLong.Long )+2.0 );
  dLatI  = parseFloat ( LatLong.Lat ) * Rad;

  dT     = Math.tan ( dLatI );
  dTSq   = dT * dT;
  dJ     = dLongI * Math.cos ( dLatI );
  dJSq   = dJ * dJ;
  dETASq = Math.cos ( dLatI );
  dETASq = dETASq * dETASq * 0.0067153346685;

  dNu = Math.sin ( dLatI );
  dNu = 6377563.396 / Math.sqrt ( 1.0 - ( dNu * dNu * 0.006670539761597 ) );  

  dA7 = ( 61.0 - dTSq * ( 479.0 - 179.0 * dTSq + dTSq * dTSq ) ) /5040.0;
  dA5 = ( 5.0 - dTSq * ( 18.0 - dTSq ) + dETASq * ( 14.0 - 58.0 * dTSq ) ) / 120.0;
  dA3 = ( 1.0 - dTSq + dETASq ) / 6.0;
  dA1 = 0.9996012717 * dNu;

  EastNorth.Easting = dA1 * dJ * ( 1.0 + ( dJSq * ( dA3 + ( dJSq * ( dA5 + dA7 * dJSq ) ) ) ) );

  dM=6335021.60578487*(1.0050342114*dLatI-0.5*0.0050449242*Math.sin(2.0*dLatI)+0.25*0.0000105505*Math.sin(4.0*dLatI));
  dA2 = 0.5 * dA1 * dT;
  dA4 = ( 5 - dTSq + dETASq*(9.0+4.0*dETASq) ) / 12.0;
  dA6 = ( 61.0 - dTSq*(58.0+dTSq) + dETASq*(270.0-330.0*dTSq) ) / 360.0;

  EastNorth.Northing = 0.9996012717 * dM + dA2 * dJSq * ( 1.0 + dJSq * ( dA4 + dA6 * dJSq ) );

  EastNorth.Easting  = EastNorth.Easting + 400000;
  EastNorth.Northing = EastNorth.Northing - 5527063.0;

  return dLongI;
}

function EastNorth(East,North)
{
  this.Easting = East;
  this.Northing = North;
  return 0;
}

function LatLong(Lat,Long)
{
  this.Lat = Lat;
  this.Long = Long;
  return 0;
}

function CoordsToLL(InEastNorth,InLatLong)
{
  var Rad = 0.0174532925199432957692369076848;
  var K = 1 / (  Rad * 6377563.396 );
  var dx;
  var dy;
  var myloop = 0;

  EN = new EastNorth(InEastNorth.Easting,InEastNorth.Northing);
  LL = new LatLong(52.0,-3.0);
  do
  {
    myloop = myloop + 1;
    LLToCoords(LL,EN);
    dx = InEastNorth.Easting - EN.Easting;
    dy = InEastNorth.Northing - EN.Northing;
    LL.Lat = LL.Lat + dy * K;
    LL.Long = LL.Long + dx * K / ( Math.cos(Rad) * LL.Lat );
  } while ( ( ( (Math.abs ( dx ) + Math.abs ( dy )) > 0.0000001 )) && myloop < 5000);
  InLatLong.Lat = LL.Lat;
  InLatLong.Long = LL.Long;

  return 0;
}


function CalculateOSGB()
{

  var lat = "<?php echo $latitude; ?>";
  var lon = "<?php echo $longitude; ?>";
  LatLong.Lat  = (lat);
  LatLong.Long = (lon);
  LLToCoords ( LatLong, EastNorth );
  ELMNT("OSRef").value = OSReference ( EastNorth );

}


function OSReference(EastNorth)
{
  var Codes = new Array ( 'V','W','X','Y','Z','Q','R','S','T','U','L','M','N','O','P','F','G','H','J','K','A','B','C','D','E');
  var East = 0.0;
  var North = 0.0;
  var I = 0;
  var J = 0;
  var A = ' ';
  var B = ' ';

  East  = parseInt ( EastNorth.Easting ) + 1000000.0;
  North = parseInt ( EastNorth.Northing ) + 500000.0;

  I = parseInt ( East  / 500000 );
  J = parseInt ( North / 500000 );
  A = Codes[parseInt ( I + J*5 )];

  East  = parseInt (  East % 500000 );
  North = parseInt ( North % 500000 );
  I = parseInt ( parseInt ( East  ) / 100000 );
  J = parseInt ( parseInt ( North ) / 100000 );
  B = Codes[parseInt ( I + J*5 )];

  C = (parseFloat(East)+100000)+"";
  D = (parseFloat(North)+400000)+"";

  return A + B + ' ' + C.substring(C.length-5,6) + ' ' + D.substring(D.length-5,6);
}
</script>


<div id="Layer3" style="position:absolute; width:695px; height:115px; z-index:1; left: 189px; top: 115px;"> 
  <table align="center" border="0">
    <tbody><tr> 
      <td><font face="Arial, Helvetica, sans-serif">Latitude:</font></td>
      <td><font face="Arial, Helvetica, sans-serif"> 
        <input value="" id="latitude" name="latitude" size="30" type="text">
        </font></td>
      <td><font face="Arial, Helvetica, sans-serif">6° South should be entered 
        as -6.0</font></td>
    </tr>
    <tr> 
      <td><font face="Arial, Helvetica, sans-serif">Longitude:</font></td>
      <td><font face="Arial, Helvetica, sans-serif"> 
        <input value="" id="longitude" name="longitude" size="30" type="text">
        </font></td>
      <td><font face="Arial, Helvetica, sans-serif">4° West should be entered 
        as -4.0</font></td>
    </tr>
    <tr> 
      <td></td>
      <td> <font face="Arial, Helvetica, sans-serif"> 
        <input value="Convert Lat/Long to OSGB" onclick="CalculateOSGB()" name="LL2OSGB" type="button">
        </font></td>
      <td> <font face="Arial, Helvetica, sans-serif"> 
        <input value="Convert OSGB to Lat/Long" onclick="CalculateLL()" name="OSGB2LL" type="button">
        </font></td>
    </tr>
    <tr> 
      <td><font face="Arial, Helvetica, sans-serif">OS Ref.</font></td>
      <td colspan="2"><font face="Arial, Helvetica, sans-serif"> 
        <input value="" id="OSRef" name="OSRef" size="30" type="text">
        </font></td>
    </tr>
    <tr> 
      <td></td>
      <td colspan="2"><font face="Arial, Helvetica, sans-serif">If you see 'NaN' 
        your Lat/Long is out of range for an OSGB reference</font></td>
    </tr>
  </tbody></table>
  <div id="results" style="position:absolute; width:695px; height:50;"></div>
</div>

我不太了解javascript,但我相信在点击计算按钮后,calculateOSGB()函数会运行OSGR。有人可以解释我如何让它自动工作而不必点击按钮?我想能够插入经度&amp;来自数据库的纬度(为什么我使用了PHP变量)并让这个脚本自动显示&amp;输出OSGR值。

感谢您的帮助

2 个答案:

答案 0 :(得分:1)

如果你不使用像jQuery这样的JS库,你可以使用onload

onload = function() {
           // do something here
         };

就是这样。有些人喜欢使用window.onload,但这是一回事。

参考:
https://developer.mozilla.org/en/DOM/window.onload
Best practice for using window.onload

如果您考虑使用jQuery或其他库,从长远来看可能会节省您的时间。如果使用jQuery,则可以使用

$(function() {   
  // do something
});

它将在DOM准备就绪时运行,这比完全加载页面的时间要早​​,就像上面的第一个方法一样。

答案 1 :(得分:0)

你有两种实现方法,你可以使用<body onload="CalculateOSGB();">调用该函数 - 这将在正文加载时执行该函数。

你的另一个选择是使用jQuery这样的库,并在DOM加载时使用$(document).ready(function()来调用你的函数。

希望有所帮助。