Task description
When the mouse moves on the map, it produces a series of MouseMove (mouse movements) event, which we can pass on the client Javascript The script snaps to the coordinates of the mouse event, which is based on the screen coordinates of the browser window. If you want to display the map coordinates of the mouse point, there are two ways: first, you keep sending the server ajax request, on the server side, convert screen coordinates to map coordinates, and then return to the client, displayed in the status bar. In this case there will be a problem, that is, when the mouse drag will continue to send requests, greatly increasing the burden on the server. Of course, we can achieve a balance between visual effects and server load by setting time intervals, such as updating coordinates in half a second. The second method is to send a ajax request, get the current map's X Y mousemove event, Calculates the map coordinates of the mouse point in real time based on the screen coordinates of the mouse and the coordinates of the four corners of the map (upper left, bottom left, upper right, bottom right).
Obviously, the second method is more efficient.
implementation step
var left_top;
var Right_bottom;
The second step is to add an onload event to the body of the page , with the following function:
function Initshowmapxy () {
Left_top = new Esripoint ();
Right_bottom = new Esripoint ();
var map = esricontrols.maps["Map0"];
Add an update listener to the map
Map.addupdatelistener ("Showmapxylistener", showmapxyrequest);
Showmapxyrequest (map);
}
In this function, the global variables are initialized first, and then an update listener is added to the map so that the listener is called every time the map state is updated. the first 7 lines of code call the listener function to get the current map coordinates when the page is first displayed, otherwise the coordinates show an exception when the page is first displayed.
The third step is to implement the listener.
function Showmapxyrequest (map) {
Get Server URL
var url = esriutils.getserverurl (Map.formid);
Set Request parameters
var params = "showmapxy=showmapxy&formid=" + Map.formid + "&mapid=" + map.id + "&"
+ Esriutils.buildrequestparams (map.formid);
Send an AJAX request and set the response handler function
var xmlHttp = esriutils.sendajaxrequest (URL, params, true, function () {updateshowmapxyresponse (xmlHttp);});
}
function Updateshowmapxyresponse (xmlHttp) {
if (xmlhttp!== null && xmlhttp.readystate = = 4 && xmlhttp.status = 200) {
Get XML response
var xml = Xmlhttp.responsexml;
left_top.x = Xml.getelementsbytagname ("Minx"). Item (0). Firstchild.nodevalue;
LEFT_TOP.Y = Xml.getelementsbytagname ("Miny"). Item (0). Firstchild.nodevalue;
right_bottom.x = Xml.getelementsbytagname ("Maxx"). Item (0). Firstchild.nodevalue;
RIGHT_BOTTOM.Y = Xml.getelementsbytagname ("Maxy"). Item (0). Firstchild.nodevalue;
}
}
The listener handler function wants the server to send an AJAX request, the server-side request handler function is not elaborated here, the processing result is the XML format response, contains four data:<minx>,<miny> ,<maxx> and <maxy>Update the coordinates of the upper-left and lower-right corners based on these four data.
Finally, the processing function of MouseMove is implemented.
function Showmapxyonstatus (event) {
var Mapcell = document.getElementById ("Esrimapcell_map0");
var bounds = esriutils.getelementpagebounds (Mapcell);
var mousepoint = Esriutils.getxy (event). Offset (-bounds.left,-bounds.top);
var mapx = mousepoint.x;
var mapy = Mousepoint.y;
if (mapx > 0 && mapx < mapcell.offsetwidth && mapy > 0 && mapy < mapcell.offsetheight) {
Window.status = Calculatex (mapx,mapcell.offsetwidth) + "," + Calculatey (mapy,mapcell.offsetheight);
}
}
In this function, the screen coordinates of the mouse punctuation are corrected, and the coordinate origin is moved to the upper-left corner of the map (the original point of the mouse point coordinates is the upper-left corner of the browser window). Next determine the mouse position, if the mouse is moved within the map extent, the coordinates are displayed in the status bar, where Calculatex is based on the screen coordinates of the mouse point (note: At this point the origin is already the top left corner of the map), The width of the map and the coordinates of the upper-left and lower-right corners of the map calculate the map coordinates of the mouse point in real time. the same is true of Calculatey.
function Calculatex (screenx,screenwidth) {
var mapwidth = right_bottom.x-left_top.x;
Return parsefloat ((screenx*mapwidth)/screenwidth) + parsefloat (left_top.x);
}Original address: http://bbs.esrichina-bj.cn/ESRI/thread-23832-1-1.html
Show map coordinates in real time with mouse move in status bar (reprint)