gislifb

Converting between coordinate-systems

Discussion created by gislifb on Jun 19, 2012
Latest reply on Jul 4, 2012 by gislifb
Hi

I recently started at the National Landsurvey of Iceland and I'm having a problem with programming a JS-mapviewer (don't really know how to describe it better).

So my problem is that I have a map where if you click it an alert box pops up (better for small mobile-screens than an infowindow) which contains the ISN93 and LatLong coordinates. I've been trying to code "around" an example on the JS API samples-site but the problem is that I'm not an experienced JS-programmer so I'm not figuring out how to project the point from ISN93 to LatLong coordinates.
I hope you can help, been trying this for some 4-5 hours now but not getting anything. I'll just paste the whole code here but my problem is with the "coordinates"-function.


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 

<html> 
 <head> 
  <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=7,IE=9" />
  <!--The viewport meta tag is used to improve the presentation and behavior of the samples 
  on iOS devices-->
  <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
  <title> Map with scalebar</title> 
  <link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/3.0/js/dojo/dijit/themes/claro/claro.css"> 
  <style> html, body { height: 100%; width: 100%; margin: 0; padding: 0; }#map{padding:0;}</style>
 
  <script type="text/javascript"> 
   var djConfig = {
    parseOnLoad: true
   };
   </script> 

   <script type="text/javascript" src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=2.8"></script> 
   
   <script type="text/javascript"> 
    dojo.require("dojo.parser");
    dojo.require("dijit.layout.BorderContainer");
    dojo.require("dijit.layout.ContentPane");
    dojo.require("esri.map");
    dojo.require("esri.dijit.Scalebar");
    dojo.require("esri.tasks.geometry");

    var map;
    var gsvc = null;
    var pt = null;

    function init() {
     var initExtent = new esri.geometry.Extent({
      "xmin": 210000,
      "ymin": 275000,
      "xmax": 810000,
      "ymax": 705000,
      "spatialReference": {
       "wkid": 3057
      }
     });

     map = new esri.Map("map", {
      extent: initExtent
     });

     var basemap = new esri.layers.ArcGISTiledMapServiceLayer("http://a1arc.lmi.is/arcgis/rest/services/kort-isn93/kort-medornefnum-isn93/MapServer");
     map.addLayer(basemap);

     gsvc = new esri.tasks.GeometryService("http://a1arc.lmi.is/arcgis/rest/services/Geometry/GeometryServer");

     dojo.connect(map, 'onLoad', function(scale) {
      var scalebar = new esri.dijit.Scalebar({
       map: map,
       scalebarUnit:'metric'
      });
      //resize the map when the browser resizes
      dojo.connect(dijit.byId('map'), 'resize', map,map.resize);
     });

     console.log(gsvc); 
     dojo.connect(map, "onClick", coordinates);
    }

    function coordinates(evt) {
     map.graphics.clear();
     var point = evt.mapPoint;
     console.log(point); //works
     var symbol = new esri.symbol.SimpleMarkerSymbol().setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE);
     var outSR = new esri.SpatialReference({wkid: 4326});
     var graphic = new esri.Graphic(point, symbol);
     
     map.graphics.add(graphic);

     function projectToLatLong(point, outSR) {
      gsvc.project([point], outSR, function(projectedPoints) {
       pt = projectedPoints[0];
       console.log(pt); //doesn't work
      });
     }

     console.log(pt) //outputs null
     console.log("ISN93: " + point.x.toFixed(2) + " ; " + point.y.toFixed(2));

    }

    dojo.addOnLoad(init);
   </script> 

 </head> 
 <body class="claro"> 
  <div dojotype="dijit.layout.BorderContainer" design="headline" gutters="false"
  style="width: 100%; height: 100%; margin: 0;"> 
   <div id="map" dojotype="dijit.layout.ContentPane" region="center" style="overflow:hidden;"></div> 
  </div> 
 </body> 

</html>

Outcomes