Road distance using Java

Ads

 
 

Share on Google+Share on Google+

sameer
Road distance using Java
1 Answer(s)      8 years ago
Posted in : Java Beginners
Ads
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

Ads









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
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
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
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
SMART VEHICLE ROAD SIGN IDENTIFICATION
SMART VEHICLE ROAD SIGN IDENTIFICATION  JAVA code for "SMART VEHICLE ROAD SIGN IDENTIFICATION
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
Version of com.mapzen>on-the-road dependency
List of Version of com.mapzen>on-the-road dependency
Version of com.mapzen>on-the-road dependency
List of Version of com.mapzen>on-the-road dependency
How to make a damaged road, make a damaged road, damaged road
How to make a damaged road       If you want to convert a fresh road to damaged mode come... a picture: First we have take a fresh road picture as I have taken here
Hill stations near Delhi distance
Hill stations near Delhi distance   Hi, Can any one tell the distances of the Hill stations near Delhi? Thanks
using eval in Java
using eval in Java  using eval in Java
Maven Dependency on-the-road >> 1.2.0-rc1
You should include the dependency code given in this page to add Maven Dependency of com.mapzen >> on-the-road version1.2.0-rc1 in your project
Maven Dependency on-the-road >> 1.2.0-rc2
You should include the dependency code given in this page to add Maven Dependency of com.mapzen >> on-the-road version1.2.0-rc2 in your project
Maven Dependency on-the-road >> 0.1
You should include the dependency code given in this page to add Maven Dependency of com.mapzen >> on-the-road version0.1 in your project
Maven Dependency on-the-road >> 0.8
You should include the dependency code given in this page to add Maven Dependency of com.mapzen >> on-the-road version0.8 in your project
Maven Dependency on-the-road >> 0.8.1
You should include the dependency code given in this page to add Maven Dependency of com.mapzen >> on-the-road version0.8.1 in your project
Maven Dependency on-the-road >> 0.8.2
You should include the dependency code given in this page to add Maven Dependency of com.mapzen >> on-the-road version0.8.2 in your project
Maven Dependency on-the-road >> 0.8.3
You should include the dependency code given in this page to add Maven Dependency of com.mapzen >> on-the-road version0.8.3 in your project
Maven Dependency on-the-road >> 0.8.4
You should include the dependency code given in this page to add Maven Dependency of com.mapzen >> on-the-road version0.8.4 in your project
Maven Dependency on-the-road >> 1.0.0
You should include the dependency code given in this page to add Maven Dependency of com.mapzen >> on-the-road version1.0.0 in your project
Maven Dependency on-the-road >> 1.1.0
You should include the dependency code given in this page to add Maven Dependency of com.mapzen >> on-the-road version1.1.0 in your project
Maven Dependency on-the-road >> 1.1.1
You should include the dependency code given in this page to add Maven Dependency of com.mapzen >> on-the-road version1.1.1 in your project
Maven Dependency on-the-road >> 1.2.0
You should include the dependency code given in this page to add Maven Dependency of com.mapzen >> on-the-road version1.2.0 in your project
Maven Dependency on-the-road >> 1.2.0-rc1
You should include the dependency code given in this page to add Maven Dependency of com.mapzen >> on-the-road version1.2.0-rc1 in your project
Maven Dependency on-the-road >> 1.2.0-rc2
You should include the dependency code given in this page to add Maven Dependency of com.mapzen >> on-the-road version1.2.0-rc2 in your project
Using throw in java method
Using throw in java method  using throw with method implies what
write a programm using java
write a programm using java  print the following using java programming