Select to view content in your preferred language

How to zoom to the center of a drawn circle on the map

1399
3
01-30-2014 05:03 AM
fuliaoli
Deactivated User
I want to draw a circle on the map and zoom to the center of the circle. I got the drawing result which contains a geometry of rings, spatialReference and type. Then I used polygon.getCentroid() to get the center of it and zoom to it. However, it didn't work. It zooms to Africa instead of within US. 
What did I do wrong here?
Thanks.
li
0 Kudos
3 Replies
AnthonyGiles
Honored Contributor
Li,

I would use map.centerAt(graphic.geometry.center). The sample code below allows you to draw a circle and pan to its center.

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
    <title></title>
    <link rel="stylesheet" href="https://community.esri.com//js.arcgis.com/3.8/js/esri/css/esri.css">
    <style>
      html, body, #map {
        height: 100%; width: 100%; margin: 0; padding: 0; 
      }
      #controls {
        background: #fff;
        box-shadow: 0 6px 6px -6px #999;
        color: #444;
        font-family: sans-serif;
        height: auto;
        left: 1em;
        padding: 1em;
        position: absolute;
        top: 1em;
        width: auto;
        z-index: 40;
      }
      #controls div {
        padding: 0 0 1em 0;
      }
    </style>

    <script src="//js.arcgis.com/3.8/"></script>
    <script>
      var map;
    
      require([
        "esri/map", "esri/geometry/Circle", "esri/symbols/SimpleFillSymbol", 
        "esri/graphic", "esri/layers/GraphicsLayer",
        "dojo/dom", "dojo/dom-attr", "dojo/domReady!"
      ], function(
        Map, Circle, SimpleFillSymbol, 
        Grahpic, GraphicsLayer, 
        dom, domAttr
      ) {
        map = new Map("map", {
          basemap: "streets",
          center: [-120.741, 56.39],
          slider: false,
          zoom: 6
        });
        var symbol = new SimpleFillSymbol().setColor(null).outline.setColor("blue");
        var gl = new GraphicsLayer({ id: "circles" });
        var geodesic = dom.byId("geodesic");
        map.addLayer(gl);
        map.on("click", function(e) {
          var radius = map.extent.getWidth() / 10;
          var circle = new Circle({
            center: e.mapPoint,
            geodesic: domAttr.get(geodesic, "checked"),
            radius: radius
          });
          var graphic = new Grahpic(circle, symbol);
          gl.add(graphic);
          map.centerAt(graphic.geometry.center);
        });
      });
    </script>
  </head>
  <body>
    <div id="map"></div>
    <div id="controls">
      <div>Click the map.</div>
      <input type="checkbox" id="geodesic">
      <label for="geodesic">Geodesic?</label>
    </div>
  </body>
</html>


Regards

Anthony
0 Kudos
fuliaoli
Deactivated User
Thanks a lot for your reply.
What I did is using Draw tool to draw a circle(polygon) and get back the results. However, I don't know an easy to extract the extent from the results.geometry as I did with draw extent, which worked perfectly for me.
Any ideas?
li
0 Kudos
fuliaoli
Deactivated User
Hi Anthony,
Finally I found a way to get a polygon from the results and zoom to the polygon extent. It worked!
Thanks again for your help.
li
0 Kudos