Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: GPS Coordinates Calculator

  1. #1
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default GPS Coordinates Calculator

    Hey guys im dealing with a bit of an issue in my code and i would really appreciate some help.So i need to write a code that will read in some coordinates from a txt file and store them in an array (one 2d array or two 1d arrays),and then i need with a help from a function to calculate the maximum distance between all the coordinates that i have read in from the txt file.( exactly 30 pairs). Now i have read in those coordinates and i have made a function that does the math but in order for that function to work i need 4 arguments,and i have only 2. I'll put my code here so maybe some of u guys have an idea what im doing wrong or how i could get the extra 2 arguments i need. Thanks!

    public class gps {
    public static double calculateDistance(double latitude1, double longitude1,
    			double latitude2, double longitude2) {
    		double deltaLatitude = Math.toRadians(latitude2 - latitude1);
    		double deltaLongitude = Math.toRadians(longitude2 - longitude1);
     
    		double a = Math.sin(deltaLatitude / 2) * Math.sin(deltaLatitude / 2)
    				+ Math.cos(Math.toRadians(latitude1))
    				* Math.cos(Math.toRadians(latitude2)) 
    				* Math.sin(deltaLongitude / 2) 
    				* Math.sin(deltaLongitude / 2);
     
    		double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
     
    		return 6371000 * c;
    	}	
     
    public static void main(String [] args){
    In.open("coordinates.txt");
            String [] zeilen = new String [30];
            String line;
            int i = 0;
            while(i < zeilen.length && (line =In.readLine ()) != null){
    			zeilen[i] = line;        
                i++;
            }
            double [] x = new double [zeilen.length];
            double [] y = new double [zeilen.length];
            for( i = 0; i<zeilen.length; i++){
                String s = zeilen[i];
                String [] coords = s.split ("\\s+");
                String number1 = coords [0];
                String number2 = coords [1];
     
                x[i] = Double.parseDouble(number1);
                y[i] = Double.parseDouble(number2);
                Out.println(x[i]);
            }
            In.close ();
        }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: GPS Coordinates Calculator

    i need 4 arguments,and i have only 2.
    Please explain.
    What 2 args do you have,
    what 4 args do you need?

    Note: the variable names x and y do not describe the values that they contain. Can you change the names to something more descriptive?
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Apr 2020
    Posts
    147
    Thanks
    8
    Thanked 0 Times in 0 Posts

    Default Re: GPS Coordinates Calculator

    Hey, thanks for the feedback. I just wanna say that i have found the solution to the problem i had and i am gonna post it here. And by arguments i meant acutally Longitude and Latitude (the x and y).

    public class gps {
    public static double calculateDistance(double latitude1, double longitude1,
    			double latitude2, double longitude2) {
    		double deltaLatitude = Math.toRadians(latitude2 - latitude1);
    		double deltaLongitude = Math.toRadians(longitude2 - longitude1);
     
    		double a = Math.sin(deltaLatitude / 2) * Math.sin(deltaLatitude / 2)
    				+ Math.cos(Math.toRadians(latitude1))
    				* Math.cos(Math.toRadians(latitude2)) 
    				* Math.sin(deltaLongitude / 2) 
    				* Math.sin(deltaLongitude / 2);
     
    		double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
     
    		return 6371000 * c;
    	}	
    static final int size = 30;	
    public static void main(String [] args){
    In.open ("coordinates.txt");
    double [] x = new double[size];
    double [] y = new double[size];
    for (int i = 0; i< size; i++){
    	 x[i] = In.readDouble ();
    	 y[i] = In.readDouble ();
    }
    double maxdistance = 0;
    	for(int i = 0; i<size; i++){
    		for (int j = 0; j<size; j++){			
    		double distance = calculateDistance( x[i], y[i], x[j], y[j]);
    			if(maxdistance < distance){
    				maxdistance = distance;
    		}	    
        }
    	}
    	Out.formatln("The biggest distance is: %.3f m", maxdistance);
    	In.close ();
    }}

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,165
    Thanks
    65
    Thanked 2,725 Times in 2,675 Posts

    Default Re: GPS Coordinates Calculator

    ok, glad you got it working.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Model won't go to the coordinates I specified
    By defiledx1 in forum What's Wrong With My Code?
    Replies: 17
    Last Post: February 17th, 2019, 03:52 PM
  2. problem with coordinates (GUI)
    By Django in forum What's Wrong With My Code?
    Replies: 14
    Last Post: March 8th, 2013, 12:36 PM
  3. coordinates (GUI)
    By Django in forum What's Wrong With My Code?
    Replies: 0
    Last Post: March 8th, 2013, 10:24 AM
  4. Java Coordinates
    By qspec in forum Java Theory & Questions
    Replies: 1
    Last Post: September 13th, 2012, 10:15 PM
  5. Help with plotting x-y coordinates.
    By mixmagz in forum Object Oriented Programming
    Replies: 5
    Last Post: January 23rd, 2012, 09:41 AM