Question

Hello I am having an issue using the proj4js library. Here is my source:

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
      <script type="text/javascript" src="lib/proj4js-combined.js"></script>
   </head>

   <script type="text/javascript">
    function go()
    {
        var lon = document.getElementById("xOrg").value;
        var lat = document.getElementById("yOrg").value;
        var reprojected = reproject(Number(lon),Number(lat));

        document.getElementById("xNew").value = reprojected.x;
        document.getElementById("yNew").value = reprojected.y;
    }

    function reproject(lon,lat)
        {
            var sourceSys = new Proj4js.Proj('WGS84');
            var destSys = new Proj4js.Proj('EPSG:32187');

            var pointSource = new Proj4js.Point(lon,lat);
            var pointDest = Proj4js.transform(sourceSys, destSys, pointSource);

            return pointDest;
        }
   </script>

   <body>
    <div>
    <input id="xOrg" type="text" value="-73.56"/>
    <input id="yOrg" type="text" value="45.49"/>
    </div>
    <div>
    <input id="xNew" type="text" value=""/>
    <input id="yNew" type="text" value=""/>
    </div>
    <div>
        <input type="button" value="go" onclick="go()"/>
    </div>
   </body>
</html>

I don't understand why the reprojection only works when I click the button twice, when I click on it the first time, the same values are returned. It seems to only work when I click the button twice or more. Here is this page online: click

Was it helpful?

Solution

var sourceSys = new Proj4js.Proj('WGS84'); 
var destSys = new Proj4js.Proj('EPSG:32187'); 

to here:

...

<script type="text/javascript">  
    var sourceSys = new Proj4js.Proj('WGS84'); 
    var destSys = new Proj4js.Proj('EPSG:32187'); 

    function go()
{
...  

And for whatever reason it now works...

OTHER TIPS

I couln't get the page to load, but is it a refresh problem? Ie. can you explicitly cause the xNew and yNew input tags to refresh themselves immediately after their values are set?

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top