Getting MapView to stay within bounds

02-25-2019 08:58 AM
New Contributor III

Working with a map to only allow panning within a certain set bounds. In my case I want the application to allow the user to pan around just within the bounds of a specific Island .

0 Kudos
2 Replies
MVP Esteemed Contributor

Here is a sample that constrains the map to a certain view extent.

<!DOCTYPE html>

  <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.10</title>
    #viewDiv {
      padding: 0;
      margin: 0;
      height: 100%;
      width: 100%;

  <link rel="stylesheet" href="">
  <script src=""></script>

    ], function(Map, MapView) {

      var map = new Map({
        basemap: "streets"

      var view = new MapView({
        container: "viewDiv",
        map: map,
        zoom: 8,
        center: [15, 65] // longitude, latitude

      view.when(function() {

      function limitMapExtent(view) {
        var initialExtent = view.extent;'stationary', function(event) {
          if (!event) {
          //If the map has moved to the point where it's center is
          //outside the initial boundaries, then move it back to the
          //edge where it moved out
          var currentCenter =;
          if (!initialExtent.contains(currentCenter)) {

            var newCenter =;

            //check each side of the initial extent and if the
            //current center is outside that extent,
            //set the new center to be on the edge that it went out on
            if (currentCenter.x < initialExtent.xmin) {
              newCenter.x = initialExtent.xmin;
            if (currentCenter.x > initialExtent.xmax) {
              newCenter.x = initialExtent.xmax;
            if (currentCenter.y < initialExtent.ymin) {
              newCenter.y = initialExtent.ymin;
            if (currentCenter.y > initialExtent.ymax) {
              newCenter.y = initialExtent.ymax;


  <div id="viewDiv"></div>

New Contributor III

Thanks for such a quick reply, i will test it out and report my results.

0 Kudos