0

I'm working with the ArcGIS API for JavaScript. I wondering is there any features I can use to get the coordinates of each vertex after I draw using the Sketch tool in the ArcGiS API?

Update

I try to use the webmercator method but it keep appear this error to me "webMercatorUtils.webMercatorToGeographic is not a function"

this is the code i written to parse it.

 sketch.on("create", (e: __esri.SketchCreateEvent) => {

    if (e.state === "complete") {
      // this.rings = e.graphic.geometry.toJSON().rings.webMercatorUtils.webMercatorToGeographic();

      this.rings = webMercatorUtils.webMercatorToGeographic(e.graphic.geometry);
    }
  });

Error

Error

kiku
  • 147
  • 1
  • 10

1 Answers1

3

Sketch widget has a series of events that you can bind to get the information you are looking for.

In your case, use create event.

ArcGIS API - Sketch

Take a look at this simple example I put for you.

<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
  <title>Sketch</title>
  <style>
    html, body, #viewDiv {
      padding: 0;
      margin: 0;
      height: 100%;
      width: 100%;
    }
  </style>

  <link rel="stylesheet" href="https://js.arcgis.com/4.21/esri/css/main.css">
  <script src="https://js.arcgis.com/4.21/"></script>

  <script>
    require([
        "esri/widgets/Sketch",
        "esri/Map",
        "esri/layers/GraphicsLayer",
        "esri/views/MapView"
      ], (Sketch, Map, GraphicsLayer, MapView) => {
        const graphicsLayer = new GraphicsLayer();

        const map = new Map({
          basemap: "topo-vector",
          layers: [graphicsLayer]
        });

        const view = new MapView({
          container: "viewDiv",
          map: map,
          zoom: 5,
          center: [90, 45]
        });

        view.when(() => {
          const sketch = new Sketch({
            layer: graphicsLayer,
            view: view
          });
          sketch.on("create", function(event) {
            if (event.state === "complete") {
              console.log(event.graphic.geometry.toJSON());
            }
          });
          view.ui.add(sketch, "top-right");
        });
      });
  </script>
</head>
<body>
  <div id="viewDiv"></div>
</body>
</html>

UPDATE

Here is the same code with the addition to convert the geometry from WebMercator (wkid 102100 or 3857) to geographics (wkid 4326)

<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
  <title>Sketch</title>
  <style>
    html, body, #viewDiv {
      padding: 0;
      margin: 0;
      height: 100%;
      width: 100%;
    }
  </style>

  <link rel="stylesheet" href="https://js.arcgis.com/4.21/esri/css/main.css">
  <script src="https://js.arcgis.com/4.21/"></script>

  <script>
    require([
        "esri/widgets/Sketch",
        "esri/Map",
        "esri/layers/GraphicsLayer",
        "esri/views/MapView",
        "esri/geometry/support/webMercatorUtils"
      ], (Sketch, Map, GraphicsLayer, MapView, webMercatorUtils) => {
        const graphicsLayer = new GraphicsLayer();

        const map = new Map({
          basemap: "topo-vector",
          layers: [graphicsLayer]
        });

        const view = new MapView({
          container: "viewDiv",
          map: map,
          zoom: 5,
          center: [90, 45]
        });

        view.when(() => {
          const sketch = new Sketch({
            layer: graphicsLayer,
            view: view
          });
          sketch.on("create", function(event) {
            if (event.state === "complete") {
              console.log(event.graphic.geometry.toJSON());
              console.log(webMercatorUtils.webMercatorToGeographic(event.graphic.geometry).toJSON());
            }
          });
          view.ui.add(sketch, "top-right");
        });
      });
  </script>
</head>
<body>
  <div id="viewDiv"></div>
</body>
</html>
cabesuon
  • 4,860
  • 2
  • 15
  • 24
  • thanks alot, and one more question. Did you have any idea how to implement the pop out template into the selected region? – kiku Oct 19 '21 at 14:20
  • btw the values that that shown in the console is what kind of value? is it geocoordinate or other type or value ? Because the values i get it like (11326141.897662453, 348220.3335735654) and i can't find the location in google maps – kiku Oct 19 '21 at 14:31
  • 1
    hi, those coordinates are web mercator the "defacto" projection for web maps .. if you need geographic coordinates (latitude, longitude), you will need to transform the geometry for example using [webMercatorToGeographic](https://developers.arcgis.com/javascript/latest/api-reference/esri-geometry-support-webMercatorUtils.html#webMercatorToGeographic) method – cabesuon Oct 20 '21 at 08:28
  • to open the view popup just use the [open](https://developers.arcgis.com/javascript/latest/api-reference/esri-widgets-Popup.html#open) method – cabesuon Oct 20 '21 at 08:31
  • I can't success to apply this method to change the coordinate value. See my update file is there any error? – kiku Oct 22 '21 at 05:24
  • It looks ok, maybe you forgot to import it .. I have updated the answer to include this so you can check – cabesuon Oct 22 '21 at 11:02
  • i have tried ur method but it show out this kind of error. I update the error in the question there – kiku Oct 24 '21 at 15:53
  • That is weird .. It seems by the error you are using angular, can you update the question showing how you are importing `webMercatorUtils`? – cabesuon Oct 25 '21 at 09:02
  • 1
    i found out the problem already. It seems like the problem of esri-loader library problem. It can't load the webMercatorUtils library if using the esri-loader. The problem solved after i remove the loader and import all the library using the ESM modules – kiku Oct 27 '21 at 01:27
  • Glad you solve it! – cabesuon Oct 27 '21 at 15:44