Select to view content in your preferred language

MapView.extent is null

2009
3
Jump to solution
10-26-2017 11:31 AM
EmoryHorvath
Emerging Contributor

I'm creating a new webmap in version 4.5, and am setting the initial extent by using the center+zoom properties.

Works all nice and good, but then when i later try to get the extent, mapview.extent returns null.

Apparently when you use center+zoom, the extent then never gets set?

This sounds like a bug to me.

I need those extent values for some other calculations.  Any idea how to get them or calculate them?

Thanks,

Emory H

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Emory,

   I just tested and I could get the extent just fine.

Are you waiting for the view to be initialized?

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
  <title>Intro to MapView - Create a 2D map - 4.5</title>
  <style>
    html,
    body,
    #viewDiv {
      padding: 0;
      margin: 0;
      height: 100%;
      width: 100%;
    }
  </style>

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

  <script>
    require([
      "esri/Map",
      "esri/views/MapView",
      "dojo/domReady!"
    ], function(Map, MapView) {

      var map = new Map({
        basemap: "streets"
      });

      var view = new MapView({
        container: "viewDiv",
        map: map,
        zoom: 4,
        center: [15, 65] // longitude, latitude
      });
      
      view.then(function(){
        console.info(view.extent);
      })

    });
  </script>
</head>

View solution in original post

3 Replies
RobertScheitlin__GISP
MVP Emeritus

Emory,

   I just tested and I could get the extent just fine.

Are you waiting for the view to be initialized?

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
  <title>Intro to MapView - Create a 2D map - 4.5</title>
  <style>
    html,
    body,
    #viewDiv {
      padding: 0;
      margin: 0;
      height: 100%;
      width: 100%;
    }
  </style>

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

  <script>
    require([
      "esri/Map",
      "esri/views/MapView",
      "dojo/domReady!"
    ], function(Map, MapView) {

      var map = new Map({
        basemap: "streets"
      });

      var view = new MapView({
        container: "viewDiv",
        map: map,
        zoom: 4,
        center: [15, 65] // longitude, latitude
      });
      
      view.then(function(){
        console.info(view.extent);
      })

    });
  </script>
</head>
EmoryHorvath
Emerging Contributor

>> Are you waiting for the view to be initialized?

Ah, sounds like a good idea...  I'll try that.

Slightly Later:  It worked too!  Thanks!

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

lines 36 - 38 in my sample above show the proper way to do it. 

Don't forget to mark your question as answered by click on the "Mark Correct" button on the reply that answered your question.

0 Kudos