Road distance using Java

Road distance using Java

View Answers

August 19, 2009 at 12:32 PM

Hi Friend,

We are providing you a code that will prompt the user to enter the latitude and longitude coordinates of the source and destination and accordingly return the distance between them.

import java.util.*;

public class CalculateDistance{
public static void main(String args[]){
System.out.println("Enter the Longitude and Latitude of Source location:");
Scanner input=new Scanner(System.in);
double lat1=input.nextDouble();
double long1=input.nextDouble();
System.out.println("Enter the Longitude and Latitude of destination location:");
double lat2=input.nextDouble();
double long2=input.nextDouble();
CalculateDistance cal=new CalculateDistance();
double dist=cal.distance( lat1, -long1 ,lat2 , -long2, 'K');
System.out.println(dist + " Kilometers\n");
}

public static double distance(double lat1, double lon1, double lat2, double lon2, char unit) {
double theta = lon1 - lon2;
double dist = Math.sin(degreeToRadian(lat1)) * Math.sin(degreeToRadian(lat2)) + Math.cos(degreeToRadian(lat1)) * Math.cos(degreeToRadian(lat2)) * Math.cos(degreeToRadian(theta));
dist = Math.acos(dist);
dist = radianToDegree(dist);
dist = dist * 60 * 1.1515;
if (unit == 'K') {
dist = dist * 1.609344;
}
return (dist);
}
private static double degreeToRadian(double deg) {

return (deg * Math.PI / 180.0);
}
private static double radianToDegree(double rad) {
return (rad * 180.0 / Math.PI);
}

}

Thanks









Related Tutorials/Questions & Answers:
Road distance using Java - Java Beginners
Road distance using Java  Dear All i am calculating the straight line distance between two latlong points using the simple mathematical formula but i need the Real Road Driving Distance // protected double distance(double pX
Delhi to Udaipur by road distance
Delhi to Udaipur by road distance  delhi to udaipur by road distance
Advertisements
Distance in meters - Java Beginners
Distance in meters  Write a program that asks the user to enter a distance in meters. The program will then present the following menu of selections... the program The program will convert the distance to kilometers, inches
Distance conversion - Java Beginners
Distance conversion  Write an application that reads a distance... the distance to kilometres, feet and inches, according to the user?s selection...() for the user selection. ? Write a method to read a distance in metres from
Another distance conversion - Java Beginners
Another distance conversion  Write an application that reads a distance in metres. The program will then convert the distance to kilometres, feet...[]){ System.out.print("Enter distance in meters : "); BufferedReader br = new BufferedReader(new
Edit Distance
Edit Distance  I want java programming ask from user input two string and the program find the edit distance between two strings and find table and optimal solution using GUI
Maven dependency for com.mapzen - on-the-road version 0.1 is released. Learn to use on-the-road version 0.1 in Maven based Java projects
( com.mapzen - on-the-road version 0.1 ) in their Java project if it is based... in project? Steps to use  com.mapzen - on-the-road version 0.1 in Java projects... - on-the-road version 0.1 java library in your project. ADS_TO_REPLACE_2 Now you can
Maven dependency for com.mapzen - on-the-road version 1.3.2 is released. Learn to use on-the-road version 1.3.2 in Maven based Java projects
this version ( com.mapzen - on-the-road version 1.3.2 ) in their Java project... in project? Steps to use  com.mapzen - on-the-road version 1.3.2 in Java...; com.mapzen - on-the-road version 1.3.2 java library in your project. ADS
Maven dependency for com.mapzen - on-the-road version 0.5 is released. Learn to use on-the-road version 0.5 in Maven based Java projects
( com.mapzen - on-the-road version 0.5 ) in their Java project if it is based... in project? Steps to use  com.mapzen - on-the-road version 0.5 in Java projects... - on-the-road version 0.5 java library in your project. ADS_TO_REPLACE_2 Now you can
GUI and how to convert a distance - Java Beginners
application that can be is used to convert a distance unit in miles into its...=new JTextField(20); t5=new JTextField(20); l1=new JLabel("Enter distance...){ String value=t1.getText(); double distance=Double.parseDouble(value
class distance
class distance  class distance-integer data member km1,km2,km3,m1,m2,m3. run()-to input the two distance and add them in the third. display()-to display the total distance. display the constructor if required
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 1.14.0 is released. Learn to use road-traffic-generators version 1.14.0 in Maven based Java projects
( uk.gov.gchq.gaffer - road-traffic-generators version 1.14.0 ) in their Java project... tutorial for using the latest version of uk.gov.gchq.gaffer - road-traffic... and includes  uk.gov.gchq.gaffer - road-traffic-generators version 1.14.0 java
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 1.15.0 is released. Learn to use road-traffic-generators version 1.15.0 in Maven based Java projects
( uk.gov.gchq.gaffer - road-traffic-generators version 1.15.0 ) in their Java project... tutorial for using the latest version of uk.gov.gchq.gaffer - road-traffic... and includes  uk.gov.gchq.gaffer - road-traffic-generators version 1.15.0 java
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 1.16.0 is released. Learn to use road-traffic-generators version 1.16.0 in Maven based Java projects
( uk.gov.gchq.gaffer - road-traffic-generators version 1.16.0 ) in their Java project... tutorial for using the latest version of uk.gov.gchq.gaffer - road-traffic... and includes  uk.gov.gchq.gaffer - road-traffic-generators version 1.16.0 java
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 1.16.1 is released. Learn to use road-traffic-generators version 1.16.1 in Maven based Java projects
( uk.gov.gchq.gaffer - road-traffic-generators version 1.16.1 ) in their Java project... tutorial for using the latest version of uk.gov.gchq.gaffer - road-traffic... and includes  uk.gov.gchq.gaffer - road-traffic-generators version 1.16.1 java
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 1.17.0 is released. Learn to use road-traffic-generators version 1.17.0 in Maven based Java projects
( uk.gov.gchq.gaffer - road-traffic-generators version 1.17.0 ) in their Java project... tutorial for using the latest version of uk.gov.gchq.gaffer - road-traffic... and includes  uk.gov.gchq.gaffer - road-traffic-generators version 1.17.0 java
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 1.17.1 is released. Learn to use road-traffic-generators version 1.17.1 in Maven based Java projects
( uk.gov.gchq.gaffer - road-traffic-generators version 1.17.1 ) in their Java project... tutorial for using the latest version of uk.gov.gchq.gaffer - road-traffic... and includes  uk.gov.gchq.gaffer - road-traffic-generators version 1.17.1 java
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 2.0.0-alpha-0.2 is released. Learn to use road-traffic-generators version 2.0.0-alpha-0.2 in Maven based Java projects
in Java projects. Follow the step by step tutorial for using the latest version... of road-traffic-generators released The developers of   uk.gov.gchq.gaffer - road-traffic-generators project have released the latest version
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 1.22.0 is released. Learn to use road-traffic-generators version 1.22.0 in Maven based Java projects
( uk.gov.gchq.gaffer - road-traffic-generators version 1.22.0 ) in their Java project... tutorial for using the latest version of uk.gov.gchq.gaffer - road-traffic...Maven dependency for  uk.gov.gchq.gaffer  - Version 1.22.0 of road
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 2.0.0-alpha-0.1 is released. Learn to use road-traffic-generators version 2.0.0-alpha-0.1 in Maven based Java projects
in Java projects. Follow the step by step tutorial for using the latest version... of road-traffic-generators released The developers of   uk.gov.gchq.gaffer - road-traffic-generators project have released the latest version
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 1.21.1 is released. Learn to use road-traffic-generators version 1.21.1 in Maven based Java projects
( uk.gov.gchq.gaffer - road-traffic-generators version 1.21.1 ) in their Java project... tutorial for using the latest version of uk.gov.gchq.gaffer - road-traffic...Maven dependency for  uk.gov.gchq.gaffer  - Version 1.21.1 of road
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 1.21.0 is released. Learn to use road-traffic-generators version 1.21.0 in Maven based Java projects
( uk.gov.gchq.gaffer - road-traffic-generators version 1.21.0 ) in their Java project... tutorial for using the latest version of uk.gov.gchq.gaffer - road-traffic...Maven dependency for  uk.gov.gchq.gaffer  - Version 1.21.0 of road
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 1.2.0-RC1 is released. Learn to use road-traffic-generators version 1.2.0-RC1 in Maven based Java projects
( uk.gov.gchq.gaffer - road-traffic-generators version 1.2.0-RC1 ) in their Java project...; uk.gov.gchq.gaffer - road-traffic-generators version 1.2.0-RC1 in Java projects. Follow the step by step tutorial for using the latest version of uk.gov.gchq.gaffer - road
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 1.0.2 is released. Learn to use road-traffic-generators version 1.0.2 in Maven based Java projects
( uk.gov.gchq.gaffer - road-traffic-generators version 1.0.2 ) in their Java project... tutorial for using the latest version of uk.gov.gchq.gaffer - road-traffic-generators...Maven dependency for  uk.gov.gchq.gaffer  - Version 1.0.2 of road
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 2.0.0-alpha-0.3.1 is released. Learn to use road-traffic-generators version 2.0.0-alpha-0.3.1 in Maven based Java projects
2.0.0-alpha-0.3.1 in Java projects. Follow the step by step tutorial for using... of road-traffic-generators released The developers of   uk.gov.gchq.gaffer - road-traffic-generators project have released the latest version
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 1.20.0 is released. Learn to use road-traffic-generators version 1.20.0 in Maven based Java projects
( uk.gov.gchq.gaffer - road-traffic-generators version 1.20.0 ) in their Java project... tutorial for using the latest version of uk.gov.gchq.gaffer - road-traffic...Maven dependency for  uk.gov.gchq.gaffer  - Version 1.20.0 of road
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 1.19.0 is released. Learn to use road-traffic-generators version 1.19.0 in Maven based Java projects
( uk.gov.gchq.gaffer - road-traffic-generators version 1.19.0 ) in their Java project... tutorial for using the latest version of uk.gov.gchq.gaffer - road-traffic...Maven dependency for  uk.gov.gchq.gaffer  - Version 1.19.0 of road
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 2.0.0-alpha-0.3 is released. Learn to use road-traffic-generators version 2.0.0-alpha-0.3 in Maven based Java projects
in Java projects. Follow the step by step tutorial for using the latest version... of road-traffic-generators released The developers of   uk.gov.gchq.gaffer - road-traffic-generators project have released the latest version
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 1.13.4 is released. Learn to use road-traffic-generators version 1.13.4 in Maven based Java projects
( uk.gov.gchq.gaffer - road-traffic-generators version 1.13.4 ) in their Java project... tutorial for using the latest version of uk.gov.gchq.gaffer - road-traffic...Maven dependency for  uk.gov.gchq.gaffer  - Version 1.13.4 of road
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 1.13.3 is released. Learn to use road-traffic-generators version 1.13.3 in Maven based Java projects
( uk.gov.gchq.gaffer - road-traffic-generators version 1.13.3 ) in their Java project... tutorial for using the latest version of uk.gov.gchq.gaffer - road-traffic...Maven dependency for  uk.gov.gchq.gaffer  - Version 1.13.3 of road
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 1.13.2 is released. Learn to use road-traffic-generators version 1.13.2 in Maven based Java projects
( uk.gov.gchq.gaffer - road-traffic-generators version 1.13.2 ) in their Java project... tutorial for using the latest version of uk.gov.gchq.gaffer - road-traffic...Maven dependency for  uk.gov.gchq.gaffer  - Version 1.13.2 of road
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 1.13.1 is released. Learn to use road-traffic-generators version 1.13.1 in Maven based Java projects
( uk.gov.gchq.gaffer - road-traffic-generators version 1.13.1 ) in their Java project... tutorial for using the latest version of uk.gov.gchq.gaffer - road-traffic...Maven dependency for  uk.gov.gchq.gaffer  - Version 1.13.1 of road
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 1.13.0 is released. Learn to use road-traffic-generators version 1.13.0 in Maven based Java projects
( uk.gov.gchq.gaffer - road-traffic-generators version 1.13.0 ) in their Java project... tutorial for using the latest version of uk.gov.gchq.gaffer - road-traffic...Maven dependency for  uk.gov.gchq.gaffer  - Version 1.13.0 of road
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 1.12.0 is released. Learn to use road-traffic-generators version 1.12.0 in Maven based Java projects
( uk.gov.gchq.gaffer - road-traffic-generators version 1.12.0 ) in their Java project... tutorial for using the latest version of uk.gov.gchq.gaffer - road-traffic...Maven dependency for  uk.gov.gchq.gaffer  - Version 1.12.0 of road
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 1.11.0 is released. Learn to use road-traffic-generators version 1.11.0 in Maven based Java projects
( uk.gov.gchq.gaffer - road-traffic-generators version 1.11.0 ) in their Java project... tutorial for using the latest version of uk.gov.gchq.gaffer - road-traffic...Maven dependency for  uk.gov.gchq.gaffer  - Version 1.11.0 of road
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 1.10.5 is released. Learn to use road-traffic-generators version 1.10.5 in Maven based Java projects
( uk.gov.gchq.gaffer - road-traffic-generators version 1.10.5 ) in their Java project... tutorial for using the latest version of uk.gov.gchq.gaffer - road-traffic...Maven dependency for  uk.gov.gchq.gaffer  - Version 1.10.5 of road
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 1.10.4 is released. Learn to use road-traffic-generators version 1.10.4 in Maven based Java projects
( uk.gov.gchq.gaffer - road-traffic-generators version 1.10.4 ) in their Java project... tutorial for using the latest version of uk.gov.gchq.gaffer - road-traffic...Maven dependency for  uk.gov.gchq.gaffer  - Version 1.10.4 of road
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 1.18.0 is released. Learn to use road-traffic-generators version 1.18.0 in Maven based Java projects
( uk.gov.gchq.gaffer - road-traffic-generators version 1.18.0 ) in their Java project... tutorial for using the latest version of uk.gov.gchq.gaffer - road-traffic...Maven dependency for  uk.gov.gchq.gaffer  - Version 1.18.0 of road
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 1.18.0 is released. Learn to use road-traffic-generators version 1.18.0 in Maven based Java projects
( uk.gov.gchq.gaffer - road-traffic-generators version 1.18.0 ) in their Java project... tutorial for using the latest version of uk.gov.gchq.gaffer - road-traffic...Maven dependency for  uk.gov.gchq.gaffer  - Version 1.18.0 of road
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 1.6.0-RC2 is released. Learn to use road-traffic-generators version 1.6.0-RC2 in Maven based Java projects
( uk.gov.gchq.gaffer - road-traffic-generators version 1.6.0-RC2 ) in their Java project...; uk.gov.gchq.gaffer - road-traffic-generators version 1.6.0-RC2 in Java projects. Follow the step by step tutorial for using the latest version of uk.gov.gchq.gaffer - road
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 2.0.0-alpha-0.4 is released. Learn to use road-traffic-generators version 2.0.0-alpha-0.4 in Maven based Java projects
in Java projects. Follow the step by step tutorial for using the latest version... of road-traffic-generators released The developers of   uk.gov.gchq.gaffer - road-traffic-generators project have released the latest version
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 1.6.0-RC1 is released. Learn to use road-traffic-generators version 1.6.0-RC1 in Maven based Java projects
( uk.gov.gchq.gaffer - road-traffic-generators version 1.6.0-RC1 ) in their Java...; uk.gov.gchq.gaffer - road-traffic-generators version 1.6.0-RC1 in Java projects...; uk.gov.gchq.gaffer - road-traffic-generators version 1.6.0-RC1 java
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 1.3.0 is released. Learn to use road-traffic-generators version 1.3.0 in Maven based Java projects
( uk.gov.gchq.gaffer - road-traffic-generators version 1.3.0 ) in their Java project... - road-traffic-generators version 1.3.0 in Java projects. Follow the step by step tutorial for using the latest version of uk.gov.gchq.gaffer - road-traffic
Maven dependency for uk.gov.gchq.gaffer - road-traffic-generators version 1.1.0 is released. Learn to use road-traffic-generators version 1.1.0 in Maven based Java projects
( uk.gov.gchq.gaffer - road-traffic-generators version 1.1.0 ) in their Java project... - road-traffic-generators version 1.1.0 in Java projects. Follow the step by step tutorial for using the latest version of uk.gov.gchq.gaffer - road-traffic
SMART VEHICLE ROAD SIGN IDENTIFICATION
SMART VEHICLE ROAD SIGN IDENTIFICATION  JAVA code for "SMART VEHICLE ROAD SIGN IDENTIFICATION
ModuleNotFoundError: No module named 'Distance'
ModuleNotFoundError: No module named 'Distance'  Hi, My Python... 'Distance' How to remove the ModuleNotFoundError: No module named 'Distance... to install padas library. You can install Distance python with following
Version of com.mapzen>on-the-road dependency
List of Version of com.mapzen>on-the-road dependency
Delhi to Udaipur by road
Delhi to Udaipur by road  Please guide me an easy and the shortest road route from Delhi to Udaipur. Thanks
data science distance learning
data science distance learning  Hi, I am beginner in Data Science... science distance learning Try to provide me good examples or tutorials links so that I can learn the topic "data science distance learning". Also tell
artificial intelligence distance learning
artificial intelligence distance learning  Hi, I am beginner in Data...: artificial intelligence distance learning Try to provide me good examples... distance learning". Also tell me which is the good training courses

Ads