0

I am trying to create a bounding box for my map in ArcGIS 3.28. I want the bounding box to stay in a given area based on the coordinates given. I found a class called Extent in the API Reference that can set a bounding box, but it does not look like there's a way to implement the bonding box. This is what I have at the moment:`

var map;

  require([
    "esri/map",
    "esri/dijit/InfoWindowLite",
    "esri/InfoTemplate",
    "esri/config",
    "esri/layers/FeatureLayer",
    "dojo/dom-construct",
    "esri/geometry/Extent",
    "esri/dijit/Search",
    "esri/layers/ArcGISTiledMapServiceLayer",
    "esri/SpatialReference",
    "dojo/domReady!"
  ],

  function(
    Map,
    InfoWindowLite,
    InfoTemplate,
    esriConfig,
    FeatureLayer,
    domConstruct,
    Extent,
    Search,
    ArcGISTiledMapServiceLayer,
    SpatialReference
  ){

     var boundingBox = new Extent(-122.3737, 37.454, -121.467, 37.9067, new SpatialReference({ wkid:4326 }));

       //creating the map
     map = new Map("map", {
       extent: boundingBox,
       basemap: "streets",  //For full list of pre-defined basemaps, navigate to http://arcg.is/1JVo6Wd
       center: [-121.91, 37.65], // longitude, latitude
       zoom: 11
     });

Using this, is there a way to create a bounding box or is something I can add to it to make it work?

1 Answer 1

0

I was able to figure out how to create the bounding box. Here is the code I used:

        var boundingBox = new Extent(-122.3737, 37.454, -121.467, 37.9067, new SpatialReference({ wkid:4326 }));

        //create a map
        map = new Map("map", {
            extent: boundingBox,
            basemap: "streets",  
        });

        //bounding box
        var previousExtent = map.extent.getExtent();
        map.on("extent-change", function(){
            let currentExtent = map.extent.getExtent();
            if (boundingBox.contains(map.extent.getCenter())){
            // Update previous extent
                previousExtent = map.extent.getExtent();
            }
            else {
            // if new extent is not in bounding box then reset to previous extent.
                map.setExtent(previousExtent);
            }
        })

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.