如何在 ArcGIS 中通过鼠标单击检索缩放位置?

发布于 2024-10-14 07:20:29 字数 3884 浏览 3 评论 0原文

我正在使用 ArcGIS 地图来显示位置并允许用户缩放并拖动到目的地。当他们单击“保存”按钮时,我想保存坐标,并在回发中向他们显示相同的缩放位置。我能够保存范围值,但不知道如何通过单击鼠标将其恢复。 setExtent 对我不起作用。请看以下内容。

 <script type="text/javascript">
  dojo.require("esri.map");
  dojo.require("esri.tasks.locator");

  var map, locator;

  function init() {

      var initExtent = new esri.geometry.Extent({ xmin: -20098296, ymin: -2804413, xmax: 5920428, ymax: 15813776, spatialReference: { wkid: 54032} });
      map = new esri.Map("map", { extent: initExtent });

    var tiledMapServiceLayer = new esri.layers.ArcGISTiledMapServiceLayer("http://maps.dcgis.dc.gov/DCGIS/rest/services/DCGIS_DATA/DC_Basemap_WebMercator/MapServer");
    map.addLayer(tiledMapServiceLayer);

    locator = new esri.tasks.Locator("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Locators/ESRI_Geocode_USA/GeocodeServer");
    dojo.connect(locator, "onAddressToLocationsComplete", showResults);

    dojo.connect(map, "onExtentChange", showExtent);
    dojo.connect(map, "onMouseMove", showCoordinates);
    dojo.connect(map, "onMouseDrag", showCoordinates);


//    setExtent(map, document.getElementById('extent').value);

}
function showCoordinates(evt) {
    //get mapPoint from event
    var mp = evt.mapPoint;
    //display mouse coordinatesLabel1
    dojo.byId("Label1").innerHTML = mp.x + ", " + mp.y;

}


function refreshMap() {       

    setExtent(map, document.getElementById('extent').value);
}

function setExtent(map,ext) {
    //        var startExtent = new esri.geometry.Extent(ext,
    //          new esri.SpatialReference({ wkid: 54032 }));
   debugger
    var coordinate = ext.split(" ");
    var xmin=coordinate[1];        
    var ymin=coordinate[3];
    var xmax=coordinate[5];
    var ymax = coordinate[7];
    var newExtent = new esri.geometry.Extent();
    newExtent.xmin = xmin;
    newExtent.ymin = ymin;
    newExtent.xmax = xmax;
    newExtent.ymax = ymax;
    newExtent.spatialReference = new esri.SpatialReference({ wkid: 31466 });
    map.setExtent(newExtent);
    dojo.connect(locator, "onAddressToLocationsComplete", showResults);
}
function showExtent(ext) {

      var s = "";
      s = "XMin: " + ext.xmin + 
   " YMin: " + ext.ymin +
   " XMax: " + ext.xmax + 
   " YMax: " + ext.ymax;
      var spatialref = ext.spatialReference.wkid;
   dojo.byId("onExtentChangeInfo").innerHTML = s;
   document.getElementById('extent').value = s;          
  }

  function locate() {
    map.graphics.clear();
    var add = dojo.byId("address").value.split(",");
    var address = {
      Address : add[0],
      City: add[1],
      State: add[2],
      Zip: add[3]
    };
    locator.addressToLocations(address,["Loc_name"]);
  }

  function showResults(candidates) {
    var candidate;
    var symbol = new esri.symbol.SimpleMarkerSymbol();
    var infoTemplate = new esri.InfoTemplate("Location", "Address: ${address}<br />Score: ${score}<br />Source locator: ${locatorName}");

    symbol.setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE);
    symbol.setColor(new dojo.Color([255,0,0,0.75]));

    var points =  new esri.geometry.Multipoint(map.spatialReference);

    for (var i=0, il=candidates.length; i<il; i++) {
      candidate = candidates[i];
      if (candidate.score > 70) {
        var attributes = { address: candidate.address, score:candidate.score, locatorName:candidate.attributes.Loc_name };
        var geom = esri.geometry.geographicToWebMercator(candidate.location);
        var graphic = new esri.Graphic(geom, symbol, attributes, infoTemplate);
        map.graphics.add(graphic);
        map.graphics.add(new esri.Graphic(geom, new esri.symbol.TextSymbol(attributes.address).setOffset(0, 8)));
        points.addPoint(geom);
      }
  }
   // map.centerAndZoom(points, 6);

    map.setExtent(points.getExtent().expand(1));
  }

  dojo.addOnLoad(init);
</script>

I am using ArcGIS map to display the location and allow the user to zoom and drag to their destination. When they click the Save button, I want to save the co-ordinates and in my postback I want to show them the same zoomed location. I am able to save the extent values and don't know how to bring it back on mouse click. The setExtent is not working for me. Please see the following.

 <script type="text/javascript">
  dojo.require("esri.map");
  dojo.require("esri.tasks.locator");

  var map, locator;

  function init() {

      var initExtent = new esri.geometry.Extent({ xmin: -20098296, ymin: -2804413, xmax: 5920428, ymax: 15813776, spatialReference: { wkid: 54032} });
      map = new esri.Map("map", { extent: initExtent });

    var tiledMapServiceLayer = new esri.layers.ArcGISTiledMapServiceLayer("http://maps.dcgis.dc.gov/DCGIS/rest/services/DCGIS_DATA/DC_Basemap_WebMercator/MapServer");
    map.addLayer(tiledMapServiceLayer);

    locator = new esri.tasks.Locator("http://sampleserver1.arcgisonline.com/ArcGIS/rest/services/Locators/ESRI_Geocode_USA/GeocodeServer");
    dojo.connect(locator, "onAddressToLocationsComplete", showResults);

    dojo.connect(map, "onExtentChange", showExtent);
    dojo.connect(map, "onMouseMove", showCoordinates);
    dojo.connect(map, "onMouseDrag", showCoordinates);


//    setExtent(map, document.getElementById('extent').value);

}
function showCoordinates(evt) {
    //get mapPoint from event
    var mp = evt.mapPoint;
    //display mouse coordinatesLabel1
    dojo.byId("Label1").innerHTML = mp.x + ", " + mp.y;

}


function refreshMap() {       

    setExtent(map, document.getElementById('extent').value);
}

function setExtent(map,ext) {
    //        var startExtent = new esri.geometry.Extent(ext,
    //          new esri.SpatialReference({ wkid: 54032 }));
   debugger
    var coordinate = ext.split(" ");
    var xmin=coordinate[1];        
    var ymin=coordinate[3];
    var xmax=coordinate[5];
    var ymax = coordinate[7];
    var newExtent = new esri.geometry.Extent();
    newExtent.xmin = xmin;
    newExtent.ymin = ymin;
    newExtent.xmax = xmax;
    newExtent.ymax = ymax;
    newExtent.spatialReference = new esri.SpatialReference({ wkid: 31466 });
    map.setExtent(newExtent);
    dojo.connect(locator, "onAddressToLocationsComplete", showResults);
}
function showExtent(ext) {

      var s = "";
      s = "XMin: " + ext.xmin + 
   " YMin: " + ext.ymin +
   " XMax: " + ext.xmax + 
   " YMax: " + ext.ymax;
      var spatialref = ext.spatialReference.wkid;
   dojo.byId("onExtentChangeInfo").innerHTML = s;
   document.getElementById('extent').value = s;          
  }

  function locate() {
    map.graphics.clear();
    var add = dojo.byId("address").value.split(",");
    var address = {
      Address : add[0],
      City: add[1],
      State: add[2],
      Zip: add[3]
    };
    locator.addressToLocations(address,["Loc_name"]);
  }

  function showResults(candidates) {
    var candidate;
    var symbol = new esri.symbol.SimpleMarkerSymbol();
    var infoTemplate = new esri.InfoTemplate("Location", "Address: ${address}<br />Score: ${score}<br />Source locator: ${locatorName}");

    symbol.setStyle(esri.symbol.SimpleMarkerSymbol.STYLE_CIRCLE);
    symbol.setColor(new dojo.Color([255,0,0,0.75]));

    var points =  new esri.geometry.Multipoint(map.spatialReference);

    for (var i=0, il=candidates.length; i<il; i++) {
      candidate = candidates[i];
      if (candidate.score > 70) {
        var attributes = { address: candidate.address, score:candidate.score, locatorName:candidate.attributes.Loc_name };
        var geom = esri.geometry.geographicToWebMercator(candidate.location);
        var graphic = new esri.Graphic(geom, symbol, attributes, infoTemplate);
        map.graphics.add(graphic);
        map.graphics.add(new esri.Graphic(geom, new esri.symbol.TextSymbol(attributes.address).setOffset(0, 8)));
        points.addPoint(geom);
      }
  }
   // map.centerAndZoom(points, 6);

    map.setExtent(points.getExtent().expand(1));
  }

  dojo.addOnLoad(init);
</script>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

仅冇旳回忆 2024-10-21 07:20:29

在我的应用程序中,我还在地图中计算和设置范围。它可以工作,但就我而言,我不是在 arcgis 地图上工作,而是在 C# 上编码的 ArcMap 上工作,因此在我的示例代码中,方法/类看起来有点不同(例如,我使用 ESRI.ArcGIS.Carto. IMxDocument 中的 IActiveView 而不是 esri.map 类来获取/发送范围),但它看起来是一个类似的过程,所以也许这也适合您。

我认为您唯一缺少的是视图/地图的刷新?代码的相关部分在我的代码上看起来像这样:

// zoom
activeView.Extent = envelope.Envelope;  // activeView.Extent is of class IEnvelope 
activeView.Refresh();

所以也许您可以使用 map.refresh() 或类似的东西?

In my app I'm also calculating and setting the Extent in the map. And it works, but in my case i'm not working on arcgis map but on ArcMap coded on C# and because of this on my example code the methods/classes look a little bit different (e.g. I use the ESRI.ArcGIS.Carto.IActiveView from the IMxDocument instead of an esri.map class to get/sent the extents) but it looks as a similar procedure, so maybe this will work for you too.

The only thing I think you're missing is a refresh of the view/map? The relevant part of the code looks like this on my code:

// zoom
activeView.Extent = envelope.Envelope;  // activeView.Extent is of class IEnvelope 
activeView.Refresh();

so maybe you can use map.refresh() or something similar?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文