<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Set initial extent of map with FeatureLayer in ArcGIS JavaScript Maps SDK Questions</title>
    <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/set-initial-extent-of-map-with-featurelayer/m-p/665728#M62181</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks &lt;A href="https://community.esri.com/migrated-users/3856" target="_blank"&gt;Ken Buja&lt;/A&gt;​&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have made the following code if anybody need (can be optimized):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;// Geometry Service
&lt;SPAN&gt;var gs = new GeometryService("&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2Fserver%2Farcgis%2Frest%2Fservices%2FUtilities%2FGeometry%2FGeometryServer" target="_blank"&gt;https://server/arcgis/rest/services/Utilities/Geometry/GeometryServer&lt;/A&gt;&lt;SPAN&gt;");&lt;/SPAN&gt;


// wkid of map and feature layer
var map_wkid = map.extent.spatialReference.wkid;
var feature_wkid = featureLayer.fullExtent.spatialReference.wkid;


// if wkid of map and feature layer is different, project
if (map_wkid != feature_wkid) {

&amp;nbsp; // get the map spatialReference
&amp;nbsp; var sr = new SpatialReference(map_wkid);

&amp;nbsp; // project
&amp;nbsp; var project = gs.project([featureLayer.fullExtent], sr);


&amp;nbsp; // anonymous functions (success and error)
&amp;nbsp; // success
&amp;nbsp; project.then(function(result) {
&amp;nbsp;&amp;nbsp;&amp;nbsp; // if a map exist, destroy first
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (map) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; map.destroy();


&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // create a new map with extent of feature layer
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; map = new Map("map-view", {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; extent: result[0],
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; basemap: "topo",
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; });
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // add the feature layer
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; map.addLayers([featureLayer]);
&amp;nbsp;&amp;nbsp;&amp;nbsp; }


&amp;nbsp; // error
&amp;nbsp; }, function(err) {
&amp;nbsp;&amp;nbsp;&amp;nbsp; console.log('error');
&amp;nbsp;&amp;nbsp;&amp;nbsp; console.log(err);
&amp;nbsp; });
}&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Sun, 12 Dec 2021 04:08:01 GMT</pubDate>
    <dc:creator>RobertoOliveira</dc:creator>
    <dc:date>2021-12-12T04:08:01Z</dc:date>
    <item>
      <title>Set initial extent of map with FeatureLayer</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/set-initial-extent-of-map-with-featurelayer/m-p/665726#M62179</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Trying to set the initial extent of map with same extent of a existing Feature Layer.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Code:&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;&lt;SPAN&gt;var featureLayer = new FeatureLayer("&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=http%3A%2F%2Fserver%2Farcgis%2Frest%2Fservices%2FMyService%2F0" target="_blank"&gt;http://server/arcgis/rest/services/MyService/0&lt;/A&gt;&lt;SPAN&gt;", {&lt;/SPAN&gt;
&amp;nbsp; mode: FeatureLayer.MODE_ONDEMAND,
&amp;nbsp; outFields: ["*"]
});

featureLayer.on("load", function(e) {
&amp;nbsp; var initialExtent = featureLayer.fullExtent;

&amp;nbsp; map = new Map('map-view', {
&amp;nbsp;&amp;nbsp;&amp;nbsp; basemap: "streets",
&amp;nbsp;&amp;nbsp;&amp;nbsp; extent: initialExtent
&amp;nbsp; });
}&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This don't work, and I have the following error on console:&lt;/P&gt;&lt;P&gt;Map: Geometry (wkid: 31982) cannot be converted to spatial reference of the map (wkid: 102100)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The Feature Layer was using the Spatial Reference wkid 31982.&lt;/P&gt;&lt;P&gt;Why the map have the Spatial Reference wkid 102100?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This is the only feature layer existing on code, and I "loaded" the Feature Layer before initialize the map, so, until I initialize the map, map = null&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;What I need to do for the map start up with the same extent of the feature layer?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:07:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/set-initial-extent-of-map-with-featurelayer/m-p/665726#M62179</guid>
      <dc:creator>RobertoOliveira</dc:creator>
      <dc:date>2021-12-12T04:07:58Z</dc:date>
    </item>
    <item>
      <title>Re: Set initial extent of map with FeatureLayer</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/set-initial-extent-of-map-with-featurelayer/m-p/665727#M62180</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;You can use a &lt;A href="https://developers.arcgis.com/javascript/jsapi/geometryservice-amd.html"&gt;GeometryService&lt;/A&gt; to project the extent of your service into the Web Mercator projection of the map.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 14 Apr 2016 14:18:05 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/set-initial-extent-of-map-with-featurelayer/m-p/665727#M62180</guid>
      <dc:creator>KenBuja</dc:creator>
      <dc:date>2016-04-14T14:18:05Z</dc:date>
    </item>
    <item>
      <title>Re: Set initial extent of map with FeatureLayer</title>
      <link>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/set-initial-extent-of-map-with-featurelayer/m-p/665728#M62181</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Thanks &lt;A href="https://community.esri.com/migrated-users/3856" target="_blank"&gt;Ken Buja&lt;/A&gt;​&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I have made the following code if anybody need (can be optimized):&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE class="lia-code-sample line-numbers language-none"&gt;// Geometry Service
&lt;SPAN&gt;var gs = new GeometryService("&lt;/SPAN&gt;&lt;A class="jive-link-external-small" href="https://community.esri.com/external-link.jspa?url=https%3A%2F%2Fserver%2Farcgis%2Frest%2Fservices%2FUtilities%2FGeometry%2FGeometryServer" target="_blank"&gt;https://server/arcgis/rest/services/Utilities/Geometry/GeometryServer&lt;/A&gt;&lt;SPAN&gt;");&lt;/SPAN&gt;


// wkid of map and feature layer
var map_wkid = map.extent.spatialReference.wkid;
var feature_wkid = featureLayer.fullExtent.spatialReference.wkid;


// if wkid of map and feature layer is different, project
if (map_wkid != feature_wkid) {

&amp;nbsp; // get the map spatialReference
&amp;nbsp; var sr = new SpatialReference(map_wkid);

&amp;nbsp; // project
&amp;nbsp; var project = gs.project([featureLayer.fullExtent], sr);


&amp;nbsp; // anonymous functions (success and error)
&amp;nbsp; // success
&amp;nbsp; project.then(function(result) {
&amp;nbsp;&amp;nbsp;&amp;nbsp; // if a map exist, destroy first
&amp;nbsp;&amp;nbsp;&amp;nbsp; if (map) {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; map.destroy();


&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // create a new map with extent of feature layer
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; map = new Map("map-view", {
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; extent: result[0],
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; basemap: "topo",
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; });
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; 
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; // add the feature layer
&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; map.addLayers([featureLayer]);
&amp;nbsp;&amp;nbsp;&amp;nbsp; }


&amp;nbsp; // error
&amp;nbsp; }, function(err) {
&amp;nbsp;&amp;nbsp;&amp;nbsp; console.log('error');
&amp;nbsp;&amp;nbsp;&amp;nbsp; console.log(err);
&amp;nbsp; });
}&lt;/PRE&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Dec 2021 04:08:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/arcgis-javascript-maps-sdk-questions/set-initial-extent-of-map-with-featurelayer/m-p/665728#M62181</guid>
      <dc:creator>RobertoOliveira</dc:creator>
      <dc:date>2021-12-12T04:08:01Z</dc:date>
    </item>
  </channel>
</rss>

