GeometryService is not a constructor

272
6
Jump to solution
08-31-2022 02:39 PM
LoriMcCormack1
New Contributor III

I am trying to create a graphic on the map and grab its Latitude and Longitude values.  To do so, I need to project from State Plane to WGS84 (wkid=4326). 

1. When I try to create a new GeometryService, I get an error saying "GeometryService is not a constructor".

In my require statement, I'm using

"esri/rest/geometryService",
"esri/rest/support/ProjectParameters",

In my function statement, I'm using 

GeometryService,
ProjectParameters,

This is the line that causes the error "GeometryService is not a constructor":

const myGeometryService = new GeometryService("https://utility.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");

2.  If I change my function statement to lower case g:

geometryService,
ProjectParameters,

This is the line that causes a different error "Identifier 'geometryService' has already been declared"

const myGeometryService = new geometryService("https://utility.arcgisonline.com/ArcGIS/rest/services/Geometry/GeometryServer");

 

What am I doing wrong?

Note:  I've also tried URL "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/Geometry/GeometryServer"

0 Kudos
1 Solution

Accepted Solutions
ReneRubalcava
Frequent Contributor II

The snippet is incorrect and will be updated in next release.

 

require([
  "esri/rest/geometryService",
  "esri/rest/support/ProjectParameters"
], function(geometryService, ProjectParameters) {
  const params = new ProjectParameters({
    geometries: [point],
    outSpatialReference: outSpatialReference,
    transformation: transformation
  });

  geometryService.project(url, params).then( ... );
});

 

This can also be written as:

import { project } from "@arcgis/core/rest/geometryService";
import ProjectParameters from "@arcgis/core/rest/support/ProjectParameters";

const params = new ProjectParameters({
  geometries: [point],
  outSpatialReference: outSpatialReference,
  transformation: transformation
});

project(url, params).then( ... );

The snippet assumes you are using ESM, which should only import the project method.

 

View solution in original post

6 Replies
ReneRubalcava
Frequent Contributor II

It's not a class, so you don't create a new instance. It has methods that you just pass the url and options. The old Task based classes were removed at 4.24 after being deprecated for a few releases.

https://developers.arcgis.com/javascript/latest/api-reference/esri-rest-geometryService.html

References:

https://www.esri.com/arcgis-blog/products/js-api-arcgis/developers/rest-is-up-to-the-task/

https://developers.arcgis.com/javascript/latest/release-notes/#breaking-changes

0 Kudos
NLA-ICTNLA-ICT
New Contributor II

Case sensitivity:

Your require statements shout be:  "esri/rest/GeometryService",

Not:  "esri/rest/geometryService",

 

 

0 Kudos
LoriMcCormack1
New Contributor III

1. The documentation shows the require statement as camelcase geometryServiceRequireFor424.png

2. I realize that it changed in 4.19, but the example in the API reference (shown below) says I need the new GeometryService.  So, in the example below from the API Reference, the const geomSer = new GeometryService(...); is wrong? By removing the new GeometryService() statement, the error went away, as long as geometryService was set to camelcase. geometryServiceProjectExample.png

3. How do I get "project" to be recognized?  Now I am getting an error with the code below, saying "ReferenceError:  project is not defined"

    const urlGeomServ = "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/Geometry/GeometryServer";
    view.when(() => {
        view.on("click", function (event) {
            var outSR = new SpatialReference(4326);
            var geomSerParams = new ProjectParameters({
                geometries: [event.mapPoint],
                outSpatialReference: outSR
             });
             project(urlGeomServ, geomSerParams).then(function (projectedPoints) {
                 pt = projectedPoints[0];
                 ptLatitude = pt.y;
                 ptLongitude = pt.x;
                 createPointGraphic(ptLongitude, ptLatitude, "AddPost");
             });
         });
    });

 

0 Kudos
ReneRubalcava
Frequent Contributor II

The snippet is incorrect and will be updated in next release.

 

require([
  "esri/rest/geometryService",
  "esri/rest/support/ProjectParameters"
], function(geometryService, ProjectParameters) {
  const params = new ProjectParameters({
    geometries: [point],
    outSpatialReference: outSpatialReference,
    transformation: transformation
  });

  geometryService.project(url, params).then( ... );
});

 

This can also be written as:

import { project } from "@arcgis/core/rest/geometryService";
import ProjectParameters from "@arcgis/core/rest/support/ProjectParameters";

const params = new ProjectParameters({
  geometries: [point],
  outSpatialReference: outSpatialReference,
  transformation: transformation
});

project(url, params).then( ... );

The snippet assumes you are using ESM, which should only import the project method.

 

KenBuja
MVP Esteemed Contributor

Could you post your full require/function section? This can be caused by a mismatch between the require modules and the function arguments.

0 Kudos
LoriMcCormack1
New Contributor III

I was able to fix the code by simply adding "geometryService." in front of the "project" statement, as shown in the first snippet in ReneRubalcava's response above.

require([
  "esri/rest/geometryService",
  "esri/rest/support/ProjectParameters"
], function(geometryService, ProjectParameters) {
const urlGeomServ = "http://sampleserver6.arcgisonline.com/arcgis/rest/services/Utilities/Geometry/GeometryServer";
    view.when(() => {
        view.on("click", function (event) {
            var outSR = new SpatialReference(4326);
            var geomSerParams = new ProjectParameters({
                geometries: [event.mapPoint],
                outSpatialReference: outSR
             });
             geometryService.project(urlGeomServ, geomSerParams).then(function (projectedPoints) {
                 pt = projectedPoints[0];
                 ptLatitude = pt.y;
                 ptLongitude = pt.x;
                 createPointGraphic(ptLongitude, ptLatitude, "AddPost");
             });
         });
    });

 

Thank you all for responding so quickly to help me identify the problem.  

I've only used the import statements in python code, so I wasn't familiar with that usage in the ArcGIS API for JavaScript.  Great to know! 

0 Kudos