Re: Latitude to decimal degrees.



Roedy,

This ship above was a combined (latitude or longitude) calculation. I
needed this to deal with data that was shown such as Los Angeles County

34:04:45N 118:24:05W to the another form 34.07917 -118.40139

Here is the complete code below (at this time). While not shown here
will be features: name the location, radio buttons to detail whether it
is a latitude or a longitude, and the combining of the statements,
then a button to save as an append to a file.

This is output is used in another program to show the global (planet
Earth) distances between locations as the crow flies (ugh!), but which
lacks the MapQuest accuracy.

Bob

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.text.DecimalFormat;

public class Frame3 extends JFrame {

private JButton calcBtn = new JButton();
private JButton ClearDataBtn = new JButton();

String locdir1;
double flocdegree , flocmin, flocsec;
String Dir ;

Label lble1 = new Label ("Convert Degrs, Mins, Secs and Dir " );
Label lble6 = new Label ("Degrees as a Integer" );
Label lble7 = new Label ("Minutes as a Integer");
Label lble8 = new Label ("Seconds as a Integer");
Label lble9 = new Label (" Direction (N/S or E/W)");
Label lble10 = new Label ("Converted Value");


TextField tf1 = new TextField(10);//Degrees
TextField tf2 = new TextField(10);//Minutes
TextField tf3 = new TextField(10);//Seconds
TextField tf4 = new TextField(10);//Direction N/S or E/W
TextField tf5 = new TextField(20);//Resultant Value

static void main() {
Frame3 fr3= new Frame3();
fr3.setVisible(true); // replaces depreciated version 1.1 was
fr2.show();
}

Frame3() {
setSize(300, 300);
add(lble1);

setLayout(new FlowLayout());
add(lble6);
add(tf1); // degrees

add(lble7);
add(tf2); // minutes

add(lble8);
add(tf3); // seconds

add(lble9);
add(tf4);// a Direction letter

calcBtn.setText(" Do Calculations ");
add(calcBtn);
add(lble10);
add(tf5);

calcBtn.addActionListener(new CalcData());
add(tf5);


ClearDataBtn.setText("Clear");
ClearDataBtn.addActionListener(new ClearData());
add(ClearDataBtn);
}

class CalcData implements ActionListener {
public void actionPerformed(ActionEvent event) {
//calculations below
flocdegree = Double.parseDouble(tf1.getText());
flocmin = Double.parseDouble(tf2.getText())/60;
flocsec = Double.parseDouble(tf3.getText())/3600;
flocdegree = flocdegree + flocmin + flocsec;
locdir1= tf4.getText();
System.out.println(locdir1);
if (locdir1.equals("S") )
flocdegree = -1* flocdegree;
if (locdir1.equals("W") )
flocdegree = -1* flocdegree;
DecimalFormat df = new DecimalFormat("#.#####");
tf5.setText(df.format(flocdegree));
}
}
class ClearData implements ActionListener {
public void actionPerformed(ActionEvent event) {
tf1.setText("");
tf2.setText("");
tf3.setText("");
tf4.setText("");
tf5.setText("");
}
}
//Overridden so we can exit when window is closed
protected void processWindowEvent(WindowEvent e) {
super.processWindowEvent(e);
if (e.getID() == WindowEvent.WINDOW_CLOSING) {
System.exit(0);
}
}
}

.


Quantcast