How to convert UTM coordinate to Lat/Long coordinate

Hi everyone,
In my Glide app I want to users enter UTM coordinate and app convert this to latitude and longitude coordinate.

Can you verify the format of how you would receive the utm coordinates, so I can make sure I’m giving you the correct javascript?

User enter a 15 digits number for example 619298421586038
app will separate 6 digits,7 digits and 2 digits from left hand
619298
4215860
38
there are some website like https://www.geoplaner.com that do this
but I can’t connect Glide to those website

Try this in a javascript column and point the p1 parameter at the column that contains the user input.

function Utm2LatLng( X,Y,zone,sn) {
    if (sn=='S')
    {
        Y = Y - 10000000;
    }
    X = X - 500000;
    var sa = 6378137.000000;
    var sb = 6356752.314245;

   var e = Math.pow( Math.pow(sa , 2) - Math.pow(sb , 2) , 0.5 ) / sa;
   var e2 = Math.pow( Math.pow( sa , 2 ) - Math.pow( sb , 2 ) , 0.5 ) / sb;
   var e2cuadrada = Math.pow(e2 , 2);
   var c = Math.pow(sa , 2 ) / sb;



   var S = ( ( zone * 6 ) - 183 ); 
   var lat =  Y / ( 6366197.724 * 0.9996 );                         
   var v =  (c * 0.9996)/ Math.pow( 1 + ( e2cuadrada * Math.pow( Math.cos(lat), 2 ))  , 0.5 ) ;
   var a = X / v;
   var a1 = Math.sin( 2 * lat );
   var a2 = a1 * Math.pow( Math.cos(lat), 2);
   var j2 = lat + ( a1 / 2 );
   var j4 = ( ( 3 * j2 ) + a2 ) / 4;
   var j6 = ( ( 5 * j4 ) + ( a2 * Math.pow( Math.cos(lat) , 2) ) ) / 3;
   var alfa = ( 3 / 4 ) * e2cuadrada;
   var beta = ( 5 / 3 ) * Math.pow(alfa , 2);
   var gama = ( 35 / 27 ) * Math.pow(alfa , 3);
   var Bm = 0.9996 * c * ( lat - alfa * j2 + beta * j4 - gama * j6 );
   var b = ( Y - Bm ) / v;
   var Epsi = ( ( e2cuadrada * Math.pow(a , 2)) / 2 ) * Math.pow( Math.cos(lat) , 2);
   var Eps = a * ( 1 - ( Epsi / 3 ) );
   var nab = ( b * ( 1 - Epsi ) ) + lat;
   var senoheps = ( Math.exp(Eps) - Math.exp(-Eps) ) / 2;
   var Delt = Math.atan(senoheps / Math.cos(nab) );
   var TaO = Math.atan(Math.cos(Delt) * Math.tan(nab));
   var longitude = (Delt *(180 / Math.PI ) ) + S;
   var latitude = ( lat + ( 1 + e2cuadrada* Math.pow(Math.cos(lat), 2) - ( 3 / 2 ) * e2cuadrada * Math.sin(lat) * Math.cos(lat) * ( TaO - lat ) ) * ( TaO - lat ) ) * (180 / Math.PI);
    return latitude.toString().concat(", " + longitude.toString()) ;
}

const number = p1;
const zone = parseInt(number.toString().substring(13)); // Extract the last 2 digits as zone
const easting = parseInt(number.toString().substring(0, 6)); // Extract the first 6 digits as easting
const northing = parseInt(number.toString().substring(6, 13)); // Extract the next 7 digits as northing
const northernHemisphere = 'N'; // You should have a way to determine this based on your input

return Utm2LatLng(easting, northing, zone, northernHemisphere);
1 Like

Thank you so much


I run but there is a problem

1 Like

I think the error is just related to your empty inputs (for below rows). It shouldn’t affect what you’re trying to do.

1 Like

Yeah, like @ThinhDinh mentioned, the error doesn’t affect anything. Its just a warning, but it if bothers you, here is a modified version that just returns nothing if p1 is empty.

function Utm2LatLng( X,Y,zone,sn) {
    if (sn=='S')
    {
        Y = Y - 10000000;
    }
    X = X - 500000;
    var sa = 6378137.000000;
    var sb = 6356752.314245;

   var e = Math.pow( Math.pow(sa , 2) - Math.pow(sb , 2) , 0.5 ) / sa;
   var e2 = Math.pow( Math.pow( sa , 2 ) - Math.pow( sb , 2 ) , 0.5 ) / sb;
   var e2cuadrada = Math.pow(e2 , 2);
   var c = Math.pow(sa , 2 ) / sb;



   var S = ( ( zone * 6 ) - 183 ); 
   var lat =  Y / ( 6366197.724 * 0.9996 );                         
   var v =  (c * 0.9996)/ Math.pow( 1 + ( e2cuadrada * Math.pow( Math.cos(lat), 2 ))  , 0.5 ) ;
   var a = X / v;
   var a1 = Math.sin( 2 * lat );
   var a2 = a1 * Math.pow( Math.cos(lat), 2);
   var j2 = lat + ( a1 / 2 );
   var j4 = ( ( 3 * j2 ) + a2 ) / 4;
   var j6 = ( ( 5 * j4 ) + ( a2 * Math.pow( Math.cos(lat) , 2) ) ) / 3;
   var alfa = ( 3 / 4 ) * e2cuadrada;
   var beta = ( 5 / 3 ) * Math.pow(alfa , 2);
   var gama = ( 35 / 27 ) * Math.pow(alfa , 3);
   var Bm = 0.9996 * c * ( lat - alfa * j2 + beta * j4 - gama * j6 );
   var b = ( Y - Bm ) / v;
   var Epsi = ( ( e2cuadrada * Math.pow(a , 2)) / 2 ) * Math.pow( Math.cos(lat) , 2);
   var Eps = a * ( 1 - ( Epsi / 3 ) );
   var nab = ( b * ( 1 - Epsi ) ) + lat;
   var senoheps = ( Math.exp(Eps) - Math.exp(-Eps) ) / 2;
   var Delt = Math.atan(senoheps / Math.cos(nab) );
   var TaO = Math.atan(Math.cos(Delt) * Math.tan(nab));
   var longitude = (Delt *(180 / Math.PI ) ) + S;
   var latitude = ( lat + ( 1 + e2cuadrada* Math.pow(Math.cos(lat), 2) - ( 3 / 2 ) * e2cuadrada * Math.sin(lat) * Math.cos(lat) * ( TaO - lat ) ) * ( TaO - lat ) ) * (180 / Math.PI);
    return latitude.toString().concat(", " + longitude.toString()) ;
}


if (!p1) {
  return '';
}
const number = p1;
const zone = parseInt(number.toString().substring(13)); // Extract the last 2 digits as zone
const easting = parseInt(number.toString().substring(0, 6)); // Extract the first 6 digits as easting
const northing = parseInt(number.toString().substring(6, 13)); // Extract the next 7 digits as northing
const northernHemisphere = 'N'; // You should have a way to determine this based on your input

return Utm2LatLng(easting, northing, zone, northernHemisphere);
1 Like

Thank you very much
It ran correctly :wave: :wave:

2 Likes