1 import Java. math. bigdecimal; 2 3 Public class convertlatlng {4 5 // converts longitude and latitude degrees to decimal 6 Public double converttodecimal (double du, double Fen, double Miao) {7 if (du <0) 8 return-(math. ABS (DU) + (math. ABS (FEN) + (math. ABS (Miao)/60)/60); 9 10 return math. ABS (DU) + (math. ABS (FEN) + (math. ABS (Miao)/60)/60; 11 12} 13 // string-type conversion of latitude and longitude 14 public double converttodecimalbystring (string latlng) {15 16 Double du = double. parsedouble (latlng. substring (0, latlng. indexof ("°"); 17 double Fen = double. parsedouble (latlng. substring (latlng. indexof ("°") + 1, latlng. indexof ("'"); 18 double Miao = double. parsedouble (latlng. substring (latlng. indexof ("'") + 1, latlng. indexof ("); 19 if (du <0) 20 return-(math. ABS (DU) + (Fen + (Miao/60)/60); 21 return du + (Fen + (Miao/60)/60; 22 23} 24 25 // convert decimal point to degree minute second 26 Public String converttosexagesimal (double num) {27 28 int du = (INT) math. floor (math. ABS (Num); // obtain the integer part (degree) 29 double temp = getdpoint (math. ABS (Num) * 60; 30 int Fen = (INT) math. floor (temp); // obtain the integer part (points) 31 double Miao = getdpoint (temp) * 60; 32 If (Num <0) 33 return "-" + du + "°" + Fen + "'" + Miao + "″"; 34 35 return du + "°" + Fen + "'" + Miao + ""; 36 37} 38 // obtain the fractional part 39 public double getdpoint (double num) {40 double D = num; 41 int fint = (INT) D; 42 bigdecimal b1 = new bigdecimal (double. tostring (d); 43 bigdecimal b2 = new bigdecimal (integer. tostring (fint); 44 double dpoint = b1.subtract (B2 ). floatvalue (); 45 return dpoint; 46} 47 48 public static void main (string [] ARGs) {49 50 convertlatlng convert = new convertlatlng (); 51 double latlng1 = convert. converttodecimal (37, 25, 19.222); 52 double latlng2 = convert. converttodecimalbystring ("-37 ° 25' 19.222" "); 53 string latlng3 = convert. converttosexagesimal (121.084095); 54 string latlng4 = convert. converttosexagesimal (-121.084095); 55 56 system. out. println ("Convert decimals (Number Parameter)" + latlng1); 57 system. out. println ("Convert decimals (string parameter)" + latlng2); 58 system. out. println ("conversion degree in seconds:" + latlng3); 59 system. out. println ("conversion degree in seconds:" + latlng4); 60 61} 62 63} 64
Mutual conversion between latitude and longitude and decimal places