📄 jitk_dynamic_coordinate.js
字号:
function startCoordsEvent(){
var mapdiv = document.getElementById("map");
mapdiv.onmousemove = updateCoords;
mapdiv.onmouseout = clearCoords;
}
function updateCoords(e){
var point = EsriUtils.getXY(e);
var map = EsriControls.maps['map1'];
var bounds = map.viewBounds;
var maxx = document.getElementById("DynamicCoordinateTask_param_webextentMAXX").value;
var maxy = document.getElementById("DynamicCoordinateTask_param_webextentMAXY").value;
var minx = document.getElementById("DynamicCoordinateTask_param_webextentMINX").value;
var miny = document.getElementById("DynamicCoordinateTask_param_webextentMINY").value;
var mapcontainer = document.getElementById("map");
var rightpanel = document.getElementById("rightPanel");
var mapheader = document.getElementById("mapHeader");
var offsetx = rightpanel.offsetLeft + mapcontainer.offsetLeft;
var offsety = rightpanel.offsetTop + mapcontainer.offsetTop;
var parent = rightpanel.offsetParent;
var parentheight = parent.offsetHeight;
var mapHeaderHeight = mapheader.offsetHeight;
var mapheight = parentheight - rightpanel.offsetHeight;
var clientLeft = mapcontainer.clientLeft;
var rightPanelLeft = rightpanel.clientLeft;
var imgwidth = map.width;
var imgheight = map.height;
var imgleft = map.left;
var imgtop = map.top;
//var imgx = point.x - 730;//imgleft;
//var imgy = point.y + 135;//imgtop;
//var imgx = point.x - 383;//imgleft;
//var imgx = point.x - (rightpanel.offsetLeft + 7);
var MapOffset = findPos(mapcontainer);
// left is off by 2 pixels
var imgx = point.x - (MapOffset[0] + 2); //mapleft position
// top is off by 1 pixel
var imgy = point.y - (MapOffset[1] + 1); //maptop position;
var ratiox = imgx/imgwidth;
var mx = minx - (-(ratiox*(maxx-minx)));
var ratioy = imgy/imgheight;
var my = maxy-(ratioy*(maxy-miny));
var lblCoords = document.getElementById("CoordinatesVal");
if (lblCoords != null)
{
if ( my <= 90 && my >= -90 && mx >= -180 && mx <= 180 ){
lblCoords.innerHTML = "<pre>" + "DD: " + " X:" + mx + "<br>" + " Y:" + my + "<br>" +
"DMS: " + "Lat:" + getLatDMS(my)+"<br>"+ " Lon:" + getLngDMS(mx)+"<br>"+
"UTM: " + getUTMString(my,mx)+"<br>"+
"MGRS:" + getMGRSString(my,mx);
}
else{
clearCoords();
}
}
}
function findPos(obj) {
var curleft = curtop = 0;
if (obj.offsetParent) {
curleft = obj.offsetLeft
curtop = obj.offsetTop
while (obj = obj.offsetParent) {
curleft += obj.offsetLeft
curtop += obj.offsetTop
}
}
return [curleft,curtop];
}
function clearCoords(){
var lblCoords = document.getElementById("CoordinatesVal");
lblCoords.innerHTML = "<pre>" + "DD: " + "<br>" +
"DMS: " + "<br>"+
"UTM: " + "<br>"+
"MGRS:";
}
var bHiddenIsSouth;
var iHiddenZone;
function setHiddenXY(sender){
//status = sender.value;
//alert(sender);
//alert(sender.value);
var f = document.forms[0];
f.hdnXCoord.value = "";
f.hdnYCoord.value = "";
if (sender.value.length>13){
var xy = new Array();
var isValid = getUTMfromMGRS(sender.value, xy);
if (isValid){
var latlon = new Array();
UTMXYToLatLon (xy[0], xy[1], iHiddenZone, bHiddenIsSouth, latlon);
xy[0] = RadToDeg(latlon[1]);
xy[1] = RadToDeg(latlon[0]);
status = "x: "+ xy[0] +" y: "+ xy[1];
f.hdnXCoord.value = xy[0];
f.hdnYCoord.value = xy[1];
}
}
}
function getLatDMS(inY){
var lat;
if (inY < 0) {
lat = DDtoDMS(((-1)*(inY)));
lat += " S";
}else {
lat = DDtoDMS(inY);
lat += " N";
}
return lat;
}
function getLngDMS(inX){
var lng;
if (inX < 0) {
lng = DDtoDMS(((-1)*(inX)));
lng += " W";
}else {
lng = DDtoDMS(inX);
lng += " E";
}
return lng;
}
function DDtoDMS(inVal){
var degrees1;
var degrees2;
var minutes1;
var minutes2;
var seconds1;
var seconds2;
var numStr;
degrees1 = inVal;
degrees2 = degrees1-(degrees1%1);
minutes1 = (degrees1%1)*60;
minutes2 = minutes1 - (minutes1%1);
if (minutes2 < 10) minutes2 = "0" + minutes2;
seconds1 = (minutes1%1)*60;
/*
var n = new NumberFormat(seconds1);
n.places = 2;
seconds2 = n.toFormatted();
//alert (seconds2);
*/
if ((toString(seconds1).length)>6) {
seconds1 = seconds1 + "";
if (seconds1 < 10 ){
seconds2 = seconds1.substring(0,4);
}else{
seconds2 = seconds1.substring(0,5);
}
}else{
seconds2 = toString(seconds1);
}
if ((seconds2.length)<4) {
seconds2 = seconds2 + ".00";
}
//window.status = seconds2.length;
if (seconds1 < 10) seconds2 = "0" + seconds2
if (degrees1 < 100) degrees2 = " " + degrees2
if (degrees1 < 10) degrees2 = " " + degrees2
numStr = degrees2 +"°" + minutes2 + "'" + seconds2 + '"';
//alert (numStr);
return numStr;
}
function getUTMfromMGRS(mgrsStr, xy){
var zoneLength = 2;
if (isNaN(parseInt(mgrsStr.charAt(1)))){
zoneLength = 1;
}
if (mgrsStr.length-zoneLength!=13) return false;
iHiddenZone = parseInt(mgrsStr.substring(0,zoneLength));
var latZone = mgrsStr.charAt(zoneLength);
//alert(latZone);
var twoCharCode = mgrsStr.substring(zoneLength+1,zoneLength+3);
//alert(twoCharCode);
var xval = mgrsStr.substring(zoneLength+3,zoneLength+8);
var yval = mgrsStr.substring(mgrsStr.length-5);
while (xval.charAt(0)=="0" && xval.length >1 )xval = xval.substring(1);
//alert("x "+xval);
//}
while (yval.charAt(0)=="0"&& yval.length >1)yval = yval.substring(1);
// alert("y "+yval);
//}
//alert (xval+","+yval);
// get x coord
var codes = "ABCDEFGHJKLMNPQRSTUVWXYZABCDEFGHJKLMN";
var index = codes.indexOf(twoCharCode.charAt(0));
index = index%8 +1;
var x = index*100000 + parseInt(xval);
//alert ("x "+x);
// get y coord
index = codes.indexOf(twoCharCode.charAt(1));
if (iHiddenZone%2==0)index+=15;
// lat codes are same as long....
index = index%20;
var y = index*100000 + parseInt(yval);
//get y zone
codes = "CDEFGHJKLMNPQRSTUVWXYZABCDEFGHJKLMN";
var yzoneindex = codes.indexOf(latZone);
if (yzoneindex > 10)
bHiddenIsSouth = false;
else
bHiddenIsSouth = true;
//alert (bHiddenIsSouth);
yzoneindex = yzoneindex%10;
//alert (yzoneindex*900000);
var yoffset = 0;
if (index*100000 < yzoneindex*900000){
yoffset = (Math.floor(yzoneindex*900000/2000000));
//alert ("yoffset "+yoffset);
}
y += yoffset*2000000;
//alert ("y "+y);
xy[0] = x;
xy[1] = y;
//status = "x: " + xy[0] + " y: " + xy[1];
return true;
//var index = (Math.floor(y/100000))%20;
//return codes.charAt(index);
}
function getMGRSString(lat,lng){
if ((lng < -180.0) || (180.0 <= lng)) {
status = ("The longitude you entered is out of range. " +
"Please enter a number in the range [-180, 180).");
return ""
}
if ((lat < -90.0) || (90.0 < lat)) {
status = ("The latitude you entered is out of range. " +
"Please enter a number in the range [-90, 90].");
return "";
}
var zonstr="";
var utmzone = Math.floor ((lng + 180.0) / 6) + 1;
if (lat<84 && lat>-80){
// test for special zones...
if (lat >= 72 && lng >= 0 && lng<=42){
if (lng<9) zonstr = "31X";
else if(lng<21) zonstr="33X";
else if(lng<33) zonstr="35X";
else zonstr = "37X";
}else if(lat>=56 && lat<=64 && lng>=3 && lng<=12){
zonstr = "32V";
}else{
zonstr += utmzone;
var latcode = Math.floor ((lat+80.0)/8);
var possibles = "CDEFGHJKLMNPQRSTUVWXX";
zonstr+=possibles.charAt(latcode);
}
var xy = new Array();
LatLonToUTMXY(DegToRad(lat), DegToRad(lng),utmzone, xy);
zonstr+=getLong100000Zone(xy[0],utmzone);
zonstr+=getLat100000Zone(xy[1],utmzone);
zonstr+=getCoords5(xy[0]);
zonstr+=getCoords5(xy[1]);
}else{
// were in the polar regions...
}
return zonstr;
}
function getLong100000Zone(x,utmzone){
var codes = "WXYZABCDEFGHJKLMNPQRSTUVWXYZABCDEFGHJKLMN";
var index = (((utmzone-1)%3)*8)+(Math.floor(x/100000))+3;
//status = ""+x+","+utmzone+","+index;
return codes.charAt(index);
}
function getLat100000Zone(y,utmzone){
var codes = "ABCDEFGHJKLMNPQRSTUVABCDEFGHJKLMN";
var index = (Math.floor(y/100000))%20;
if (utmzone%2==0)index+=5;
return codes.charAt(index);
}
function getCoords5(coord){
var s = ""+Math.floor(coord%100000);
for (var i = s.length; i<5;i++){
s = "0" + s;
}
return s;
}
/*
Copyright 1997-1998 by Charles L. Taylor
*/
var pi = 3.14159265358979;
/* Ellipsoid model constants (actual values here are for WGS84) */
var sm_a = 6378137.0;
var sm_b = 6356752.314;
var sm_EccSquared = 6.69437999013e-03;
var UTMScaleFactor = 0.9996;
/*
* DegToRad
*
* Converts degrees to radians.
*
*/
function DegToRad (deg)
{
return (deg / 180.0 * pi)
}
/*
* RadToDeg
*
* Converts radians to degrees.
*
*/
function RadToDeg (rad)
{
return (rad / pi * 180.0)
}
/*
* ArcLengthOfMeridian
*
* Computes the ellipsoidal distance from the equator to a point at a
* given latitude.
*
* Reference: Hoffmann-Wellenhof, B., Lichtenegger, H., and Collins, J.,
* GPS: Theory and Practice, 3rd ed. New York: Springer-Verlag Wien, 1994.
*
* Inputs:
* phi - Latitude of the point, in radians.
*
* Globals:
* sm_a - Ellipsoid model major axis.
* sm_b - Ellipsoid model minor axis.
*
* Returns:
* The ellipsoidal distance of the point from the equator, in meters.
*
*/
function ArcLengthOfMeridian (phi)
{
var alpha, beta, gamma, delta, epsilon, n;
var result;
/* Precalculate n */
n = (sm_a - sm_b) / (sm_a + sm_b);
/* Precalculate alpha */
alpha = ((sm_a + sm_b) / 2.0)
* (1.0 + (Math.pow (n, 2.0) / 4.0) + (Math.pow (n, 4.0) / 64.0));
/* Precalculate beta */
beta = (-3.0 * n / 2.0) + (9.0 * Math.pow (n, 3.0) / 16.0)
+ (-3.0 * Math.pow (n, 5.0) / 32.0);
/* Precalculate gamma */
gamma = (15.0 * Math.pow (n, 2.0) / 16.0)
+ (-15.0 * Math.pow (n, 4.0) / 32.0);
/* Precalculate delta */
delta = (-35.0 * Math.pow (n, 3.0) / 48.0)
+ (105.0 * Math.pow (n, 5.0) / 256.0);
/* Precalculate epsilon */
epsilon = (315.0 * Math.pow (n, 4.0) / 512.0);
/* Now calculate the sum of the series and return */
result = alpha
* (phi + (beta * Math.sin (2.0 * phi))
+ (gamma * Math.sin (4.0 * phi))
+ (delta * Math.sin (6.0 * phi))
+ (epsilon * Math.sin (8.0 * phi)));
return result;
}
/*
* UTMCentralMeridian
*
* Determines the central meridian for the given UTM zone.
*
* Inputs:
* zone - An integer value designating the UTM zone, range [1,60].
*
* Returns:
* The central meridian for the given UTM zone, in radians, or zero
* if the UTM zone parameter is outside the range [1,60].
* Range of the central meridian is the radian equivalent of [-177,+177].
*
*/
function UTMCentralMeridian (zone)
{
var cmeridian;
cmeridian = DegToRad (-183.0 + (zone * 6.0));
return cmeridian;
}
/*
* FootpointLatitude
*
* Computes the footpoint latitude for use in converting transverse
* Mercator coordinates to ellipsoidal coordinates.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -