This is how you change it to UTM,
First you have yo change it to wgs, and then call the function to do the rest
function showCoordinates(evt) {
var wgs = webMercatorUtils.webMercatorToGeographic(evt.mapPoint);
var mp = evt.mapPoint;
// Converting The WGS to UTM
document.getElementById("utmCoordinates").innerHTML = myFunction(wgs.x, wgs.y);
function myFunction(lan1, fi)
{
var a = 6378137.000;
var b = 6356752.314;
var f = (a - b) / a;
var e2 = Math.sqrt((Math.pow(a, 2) - Math.pow(b, 2)) / Math.pow(b, 2));
var e = Math.sqrt((Math.pow(a, 2) - Math.pow(b, 2)) / Math.pow(a, 2));
var zone;
var lan0;
if (lan1 > 0) {
var zone = 30 + Math.ceil(lan1 / 6);
lan0 = Math.floor(lan1 / 6) * 6 + 3;
}
else {
var zone = 30 - Math.floor(Math.abs(lan1) / 6);
lan0 = -Math.floor(Math.abs(lan1) / 6) * 6 - 3;
}
//-----------------------------------------------
var lan = lan1 - lan0;
lan = lan * Math.PI / 180;
fi = fi * Math.PI / 180;
var N = a / Math.pow(1 - Math.pow(e, 2) * Math.pow(Math.sin(fi), 2), 0.5);
var M = a * (1 - Math.pow(e, 2)) / Math.pow((1 - (Math.pow(e, 2) * Math.pow(Math.sin(fi), 2))), (3 / 2));
var t = Math.tan(fi);
var p = N / M;
//----------------------------------------------
var k0 = 0.9996;
var term1 = Math.pow(lan, 2) * p * Math.pow(Math.cos(fi), 2) / 2;
var term2 = Math.pow(lan, 4) * Math.pow(Math.cos(fi), 4) * (4 * Math.pow(p, 3) * (1 - 6 * Math.pow(t, 2)) + Math.pow(p, 2) * (1 + 24 * Math.pow(t, 2)) - 4 * p * Math.pow(t, 2)) / 24;
var term3 = Math.pow(lan, 6) * Math.pow(Math.cos(fi), 6) * (61 - 148 * Math.pow(t, 2) + 16 * Math.pow(t, 4)) / 720;
var Kutm = k0 * (term1 + term2 + term3);
//----------------------------------------------
term1 = Math.pow(lan, 2) * p * Math.pow(Math.cos(fi), 2) * (p - Math.pow(t, 2)) / 6;
term2 = Math.pow(lan, 4) * Math.pow(Math.cos(fi), 4) * (4 * Math.pow(p, 3) * (1 - 6 * Math.pow(t, 2)) + Math.pow(p, 2) * (1 + 8 * Math.pow(t, 2)) - Math.pow(p, 2) * Math.pow(t, 2) + Math.pow(t, 4)) / 120;
term3 = Math.pow(lan, 6) * Math.pow(Math.cos(fi), 6) * (61 - 479 * Math.pow(t, 2) + 179 * Math.pow(t, 4) - Math.pow(t, 6)) / 5040;
var Xutm = 500000 + k0 * lan * N * Math.cos(fi) * (1 + term1 + term2 + term3);
//----------------------------------------------
var A0 = 1 - 0.25 * Math.pow(e, 2) - 3 / 64 * Math.pow(e, 4) - 5 / 256 * Math.pow(e, 6);
var A2 = 3 / 8 * (Math.pow(e, 2) + 0.25 * Math.pow(e, 4) + 15 / 128 * Math.pow(e, 6));
var A4 = 15 / 256 * (Math.pow(e, 4) + 0.75 * Math.pow(e, 6));
var A6 = 35 / 3072 * Math.pow(e, 6);
var sfi = a * (A0 * fi - A2 * Math.sin(2 * fi) + A4 * Math.sin(4 * fi) - A6 * Math.sin(6 * fi));
//----------------------------------------------
term1 = Math.pow(lan, 2) * N * Math.sin(fi) * Math.cos(fi) / 2;
term2 = Math.pow(lan, 4) * N * Math.sin(fi) * Math.pow(Math.cos(fi), 3) * (4 * Math.pow(p, 2) + p - Math.pow(t, 2)) / 24;
term3 = Math.pow(lan, 6) * N * Math.sin(fi) * Math.pow(Math.cos(fi), 5) * (8 * Math.pow(p, 4) * (11 - 24 * Math.pow(t, 2)) - 28 * Math.pow(p, 3) * (1 - 6 * Math.pow(t, 2)) + Math.pow(p, 2) * (1 - 32 * Math.pow(t, 2)) - p * 2 * Math.pow(t, 2) + Math.pow(t, 4));
var term4 = Math.pow(lan, 8) * N * Math.sin(fi) * Math.pow(Math.cos(fi), 7) * (1385 - 3111 * Math.pow(t, 2) + 543 * Math.pow(t, 4) - Math.pow(t, 6));
var Yutm = k0 * (sfi + term1 + term2 + term3 + term4);
UTMx = Xutm.toFixed(5);
UTMy = Yutm.toFixed(5);
return (Xutm.toFixed(5).toString() + "," + Yutm.toFixed(5).toString());
//return Xutm.toString().concat(" ; " + Yutm.toString() + " " + zone.toString());
}
Best Answer
I haven't looked at your code, but in the URL you provided, your X and Y are flipped around. For Faroe Islands, X should be -6.7866 (longitude) while Y should be 62.01387 (latitude). Here's a working URL of what you're trying to do:
That correctly returns:
As for 500s, I seem to get those quite frequently with sampleserver3 as well tonight. Try using sampleserver1 instead, that one seems to be more stable.