How to retrieve the zoomed location on mouse click, in ArcGIS?
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>
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?
精彩评论