What method of AGSSketchEditor for inserting the first vertex?

434
6
Jump to solution
07-27-2018 06:31 PM
ReedHunter
New Contributor III

I need to support starting a sketch programmatically with a GPS point, not just tapping the screen.  How do I do that for line and polygon layers?

AGSSketchEditor.replaceGeometry works for point layers only. 

AGSSketchEditor.insertVertexAfterSelectedVertexWithPoint works if your sketch has at least one point already. 

AGSSketchEditor.startWithGeometry fails if you pass in a polyline or polygon that just has one vertex.  

Thanks

0 Kudos
1 Solution

Accepted Solutions
MarkDostal
Esri Contributor

Reed,

You can pass polylines and polygons to AGSSketchEditor.replaceGeometry since that method takes an AGSGeometry.  If you have the gps point, you can do any of these to set it into the sketch editor, depending on what type of geometry you are editing:

replaceGeometry(point)

replaceGeometry(AGSPolyline(points: [point]))

replaceGeometry(AGSPolygon(points: [point]))

The caveat being that the new geometry must be the same type as what you are replacing.

Let me know if that doesn't work for you.

Mark

View solution in original post

0 Kudos
6 Replies
MarkDostal
Esri Contributor

Reed,

The doc for insertVertexAfterSelectedVertexWithPoint states:  "Adds a new vertex, and selects it. If geometry is empty, this adds a new point.".  Have you tried it with an empty geometry as the initial SketchEditor geometry?

Markl

0 Kudos
ReedHunter
New Contributor III

I have, and the respondToGeomChanged event is not triggered. The line after the call to insertVertexAfterSelectedVertexWithPoint, a debug po of the geometry shows nothing new:

   po self.sketchEditor.geometry

   AGSPolyline: [], sr: 3857

I have also tried calling clearGeometry right before insertVertexAfterSelectedVertexWithPoint just to be sure, but aside from triggering respondToGeomChanged in that case, there is no difference.

The point passed to insertVertexAfterSelectedVertexWithPoint is confirmed having the same spatial reference as the target line layer.

0 Kudos
MarkDostal
Esri Contributor

Reed,

You can pass polylines and polygons to AGSSketchEditor.replaceGeometry since that method takes an AGSGeometry.  If you have the gps point, you can do any of these to set it into the sketch editor, depending on what type of geometry you are editing:

replaceGeometry(point)

replaceGeometry(AGSPolyline(points: [point]))

replaceGeometry(AGSPolygon(points: [point]))

The caveat being that the new geometry must be the same type as what you are replacing.

Let me know if that doesn't work for you.

Mark

View solution in original post

0 Kudos
ReedHunter
New Contributor III

I had tried this approach before using the AGSPolylineBuilder and AGSPolygonBuilder without success, but just writing it straight out as an AGSPolyline and AGSPolygon constant did the trick, it's working now.

Minor caveat, I'm working in a hybrid project with both swift and Objective-C modules.  In this case the translation to Objective-C was as follows:

NSArray<AGSPoint *> * pointArray = [NSArray<AGSPoint*> arrayWithObjects: mapPoint, nil];

[self.sketchEditor replaceGeometry: [AGSPolyline polylineWithPoints:pointArray]];

Thanks Mark, the quest for a production release can now continue.

-Reed

0 Kudos
MarkDostal
Esri Contributor

Excellent, I'm glad you have it working.  If there's anything else, let us know!

Mark

0 Kudos
Dinakarsoma
New Contributor

Hi,

I had the same issue and tried your approach but that didn't work.

What I am doing?:

1. Create instance of SketchEditor based based on Selection of button (Point/Polyline/Polygon). 

2. Add/assign the sketcheditor instance to map

3. Press a button to fetch GPS location

4. On receive location, project the point to my map's spatial reference 

5. Add the point to Sketch editor

Below is the code:

 let sketchEditor  = self.sketchEditor()   //Applied a style in the constructor

 let skectchEditConfiguration = self.getEditConfiguration()  //Applied some configurations

Point:

 isDrawToolStarted = sketchEditor!.start(with: .point, editConfiguration: skectchEditConfiguration)

Polyline: 

isDrawToolStarted = sketchEditor!.start(with: .polyline, editConfiguration: skectchEditConfiguration)

Polygon:

isDrawToolStarted = sketchEditor!.start(with: .polygon, editConfiguration: skectchEditConfiguration)

if isDrawToolStarted { self.mapView.sketchEditor = sketchEditor }

Code for step 5:

For Point:

 self.sketchEditor.replaceGeometry(projectedPoint as! AGSPoint)

Output: The new point is not shown. If I tap on the map, a point is marked but if I fetch a GPS point and replace no point is shown. The debug log statements prints a point with correct spatial reference, but no visual.

For Line: 

var arrayOfPoints = [AGSPoint]()

arrayOfPoints.append(projectedPoint as! AGSPoint)

let newPolyline = AGSPolyline(points: arrayOfPoints)

self.sketchEditor?.replaceGeometry(newPolyline)

and 

 let isInserted = self.sketchEditor?.insertVertexAfterSelectedVertex(with: projectedPoint as! AGSPoint)

Output: 

The new point is shown only if the sketch has at least one point already.

The new point is not shown if no point is available on editor. log shows  AGSPolyline: [], sr: 25833 and insertVertexAfterSelectedVertex returns false. If replace is applied no point is shown either.

What could be the issue? Can you please have a look at the snippets. I added only the relevant code to the description rather than pasting all the code.

Thanks.

0 Kudos