Skip to content

This sample demonstrates how to add custom formats to the Coordinate Conversion component in a 3D Scene.

First, we need to import some classes to help with the new formatting and conversion options.

Use dark colors for code blocksCopy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169  const [Conversion, Format, Point, SpatialReference, webMercatorUtils] = await $arcgis.import([  "@arcgis/core/widgets/CoordinateConversion/support/Conversion.js",  "@arcgis/core/widgets/CoordinateConversion/support/Format.js",  "@arcgis/core/geometry/Point.js",  "@arcgis/core/geometry/SpatialReference.js",  "@arcgis/core/geometry/support/webMercatorUtils.js",  ]); 

Create a new custom Format to define a convert function and a reverse convert function, or a SpatialReference and a reverse convert function. If a SpatialReference is provided, a convert function is not required; the Geometry Service will be used to project as needed. Adding this new format will allow elevation information to be displayed.

Use dark colors for code blocksCopy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169  const coordinateConversion = document.querySelector("arcgis-coordinate-conversion");  await coordinateConversion.componentOnReady();   // Regular expression to find a number  const numberSearchPattern = /-?\d+[\.]?\d*/;   /**  * Create a new Format called XYZ, which looks like: "<Latitude>, <Longitude>, <Z>"  *  * We need to define a convert function, a reverse convert function,  * and some formatting information.  */  const newFormat = new Format({  // The format's name should be unique with respect to other formats used by the component  name: "XYZ",  conversionInfo: {  // Define a convert function  // Point -> Position  convert: function (point) {  const returnPoint = point.spatialReference.isWGS84  ? point  : webMercatorUtils.webMercatorToGeographic(point);  const x = returnPoint.x.toFixed(4);  const y = returnPoint.y.toFixed(4);  const z = returnPoint.z.toFixed(4);  return {  location: returnPoint,  coordinate: `${x}, ${y}, ${z}`,  };  },  // Define a reverse convert function  // String -> Point  reverseConvert: function (string) {  const parts = string.split(",");  return new Point({  x: parseFloat(parts[0]),  y: parseFloat(parts[1]),  z: parseFloat(parts[2]),  spatialReference: { wkid: 4326 },  });  },  },  // Define each segment of the coordinate  coordinateSegments: [  {  alias: "X",  description: "Longitude",  searchPattern: numberSearchPattern,  },  {  alias: "Y",  description: "Latitude",  searchPattern: numberSearchPattern,  },  {  alias: "Z",  description: "Elevation",  searchPattern: numberSearchPattern,  },  ],  defaultPattern: "X°, Y°, Z",  }); 

Now we add another new custom Format in the California StatePlane Zone I Spatial Reference, which will be useful as our map is centered around Sequoia National Park.

Use dark colors for code blocksCopy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169  /**  * Create a new Format 'SPS I', which looks like: "<X>, <Y>" in the  * California StatePlane Zone I Spatial Reference, described by wkid 102241  *  * For this Format, we only need to provide a spatialReference with the correct  * wkid. The geometry service can take care of the rest.  */  const stateplaneCA = new Format({  name: "SPS I",  conversionInfo: {  spatialReference: new SpatialReference({ wkid: 102241 }),  reverseConvert: function (string, format) {  const parts = string.split(",");  return new Point({  x: parseFloat(parts[0]),  y: parseFloat(parts[1]),  spatialReference: { wkid: 102241 },  });  },  },  coordinateSegments: [  {  alias: "X",  description: "easting",  searchPattern: numberSearchPattern,  },  {  alias: "Y",  description: "northing",  searchPattern: numberSearchPattern,  },  ],  defaultPattern: "X, Y",  }); 

Lastly, we set-up our new spatial references, add the new formats to the Coordinate Conversion component, and then move the new formats to the top of the list. We set-up our Coordinate Conversion component to be expanded by default, so we should see these new formats available immediately.

Use dark colors for code blocksCopy
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169  // set custom spatial references  coordinateConversion.spatialReferences = [  new SpatialReference({ wkid: 2263 }), // State Plane New York Long Island (ftUS)  new SpatialReference({ wkid: 26918 }), // UTM Zone 18N  ];   coordinateConversion.formats.add(newFormat);  coordinateConversion.formats.add(stateplaneCA);   // Add the two custom formats to the top of the component's display  coordinateConversion.conversions.splice(  0,  0,  new Conversion({  format: newFormat,  }),  new Conversion({  format: stateplaneCA,  }),  ); 

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.