Why is Geometry Server Project failing with toJson is not a function?

1828
4
Jump to solution
02-02-2020 09:35 PM
CodyMcDonald
New Contributor

I've inherited a mapviewer which provides a function that accepts latitude and longitude values, and should then move to that point on the map and place a pin. However, the input x and y come from Google Maps wkid 4326 and I need to convert them to wkid 2357. The function formerly relied on projectionPromise which was throwing web assembly errors in both IE and Chrome. I tried coding around this to use the Geometry Server Project function. Now I receive Uncaught TypeError: a.toJson is not a function.

Can somebody save me?

dropPin: function(e){
var latY = ""
var lonX = ""
var alertMessage = "Both Latttitude and Longitude must be entered"
if(!this.labelLayer){
this.labelLayer = new GraphicsLayer({id: "labelLayer"})
}
if(!this.resultsLayer){
this.resultsLayer = new GraphicsLayer({id: "resultsLayer"})
}
if(e.srcElement.dataset.dojoAttachPoint == "DropPinBtn"){
if(this.LatTxt.value == "" || this.LongTxt.value == ""){
alert(alertMessage)
return
} else {
lonX = this.LongTxt.value
latY = this.LatTxt.value
}
}

if(parseFloat(latY) && parseFloat(lonX)){
latY = parseFloat(latY)
lonX = parseFloat(lonX)
if(latY>-90 && latY<90){
if(lonX>-180 && lonX<180){
//Main Body
//Create new graphic and symbolize
var newPin = {"geometry":{"x":lonX,"y":latY, "spatialReference":{"wkid":4326}} }
//var rePin = Projection.project(newPin, this.map.spatialReference)
var symbol = this.defaultSymbol;
var graphic = new Graphic(newPin);
graphic.setSymbol(symbol);
console.log(graphic);
var featureSet = jimuUtils.toFeatureSet(graphic);
jimuUtils.zoomToFeatureSet(this.map, featureSet);

//the above successfully re-centers the map, but does not draw the graphic
var geoService = new GeometryService(this.geometryService);
var outSpatialReference = new SpatialReference(this.map.spatialReference);
var coordParams = new ProjectParameters();
coordParams.inSR = newPin.geometry.spatialReference;
coordParams.outSR = outSpatialReference;
coordParams.geometries = [newPin.geometry.x, newPin.geometry.y];
console.log(coordParams);
var newCoords = geoService.project(coordParams);
//function fails at the above line of code
this.resultsLayer.add(graphic);
//this.map.graphics.add(graphic);
} else{
alert("Longitude is not within accpetable range of values")
}
} else{
alert("Lattitude is not within accpetable range of values")
}
} else {
alert("Either lattitude or logitude is not a valid number")
}
}

0 Kudos
1 Solution

Accepted Solutions
LeoLiu1
Occasional Contributor

Please try this:

reference esri/geometry/Point module, and change 

var newPin = {"geometry":{"x":lonX,"y":latY, "spatialReference":{"wkid":4326}} }

to

var newPin = new Point(lonX, latY, new SpatialReference(4326));

I am guessing your newPin is an object other than a Point object, therefore doesn't have the 'toJson' method.

View solution in original post

4 Replies
LeoLiu1
Occasional Contributor

coordParams.geometries = [newPin.geometry.x, newPin.geometry.y];

Looks like [newPin.geometry.x, newPin.geometry.y] is a coordinates array other than a geometry array.

try this: 

coordParams.geometries = [newPin.geometry];

0 Kudos
CodyMcDonald
New Contributor

Thank you for the feedback. 

I just tested using your suggestion and coordParams.geometries takes the x and y values along with the spatialReference of newPin. However, the same error is thrown. 

0 Kudos
LeoLiu1
Occasional Contributor

Please try this:

reference esri/geometry/Point module, and change 

var newPin = {"geometry":{"x":lonX,"y":latY, "spatialReference":{"wkid":4326}} }

to

var newPin = new Point(lonX, latY, new SpatialReference(4326));

I am guessing your newPin is an object other than a Point object, therefore doesn't have the 'toJson' method.

CodyMcDonald
New Contributor

Awesome, thank you!

Here is the updated code for the Main Body of the function for completeness sake:

var newPin = new Point(lonX, latY, new SpatialReference(4236));
var symbol = this.defaultSymbol;
console.log(newPin);
var geoService = new GeometryService(this.geometryService);
var outSpatialReference = new SpatialReference(this.map.spatialReference);
var coordParams = new ProjectParameters();
coordParams.inSR = newPin.spatialReference;
coordParams.outSR = outSpatialReference;
coordParams.geometries = [newPin];
geoService.project(coordParams,lang.hitch(this, function(result){

  var pnt = new Point(result[0]);

  var graphic = new Graphic(pnt);
  graphic.setSymbol(symbol);
  this.map.graphics.add(graphic);
  var newMapExtent
  var zoomBuffer = 750
  newMapExtent = new Extent(pnt.x-zoomBuffer,pnt.y-zoomBuffer,pnt.x+zoomBuffer,pnt.y+zoomBuffer,          pnt.spatialReference)
  this.map.setExtent(newMapExtent);
}))

I will mark this as answered, I appreciate your help!

0 Kudos