Search code examples
javascriptgisarcgis-js-api

Convert Long And Lat to X Y coordinate using ArcGis Api For Javascript


Hello i'm trying to convert long,lat values to X,Y using ArcGis Api For Javascript

 var i = esri.geometry.lngLatToXY(3.13, 36.742)
 console.log(i); //returns Array [ 348541.32567373366, 4403205.668961807 ]

in what system does this conversion take place? is there a method to specify the projection system?

note: the conversion is done from decimal degree to meters

i followed this : https://developers.arcgis.com/javascript/3/jsapi/esri.geometry.webmercatorutils-amd.html


Solution

  • This method is used to convert geographic coordinate system longitude/latitude (wkid 4326) to projected coordinate system Web Mercator (wkid 102100).

    Default esri map use Web Mercator as projection system. If you need to convert your coordinates to an other coordinate system you need to use the project method of GeometryService : https://developers.arcgis.com/javascript/3/jsapi/geometryservice-amd.html

    Example:

    require(["esri/geometry/Point", "esri/tasks/GeometryService", "esri/tasks/ProjectParameters", "esri/SpatialReference", "dojo/domReady!"],
      function(Point, GeometryService, ProjectParameters, SpatialReference) {
    
        var outSR = "YOUR_OUTPUT_COORDINATE_SYSTEM"; // `wkid {number}`
        var geometryService = new GeometryService("https://utility.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");
        var inputpoint = new Point({
          longitude: "YOUR_LONGITUDE_INPUT",
          latitude: "YOUR_LATITUDE_INPUT"
        });
    
        var projectParams = new ProjectParameters();
        projectParams.geometries = [inputpoint];
        projectParams.outSR = new SpatialReference({ wkid: outSR });
    
        geometryService.project(projectParams, (result) => {
          let outputpoint = result[0]; // outputpoint first element of result array
          console.log("Result x:", outputpoint.x, "y :", outputpoint.y);
        });
      });
    

    Wkid numbers can be found here:

    EDIT

    Here is a working example: Plunker