Share on Google+Share on Google+

Road distance using Java
1 Answer(s)      7 years ago
Posted in : Java Beginners


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(;
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);



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
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
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
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
Distance learning business management
Distance learning business management courses are on high demand today.... But those students should not worry as many institutions have started distance... for the future and skilled workforce. Distance learning business management courses
Hill Stations near Delhi distance
Distance of Major Hill Stations From Delhi  In this page we have listed all the Hill Stations near Delhi with their distance from central Delhi... and northeastern India. List of Hill Stations near Delhi with distance Sr
VoIP Long Distance
VoIP Long Distance          VoIP Designing a Long-Distance Network The long-distance Voice over IP (VoIP) network solution is a set of network design
How can i display the distance of a line drawing randomly?
How can i display the distance of a line drawing randomly?  How can i display the distance of a line drawing randomly
loop to input multiple points to calculate final distance and speed
loop to input multiple points to calculate final distance and speed  ...; private double distance; public Travel() { time=0; distance=0... getDistance() { return distance; } public static void main (String[] args
using eval in Java
using eval in Java  using eval in Java
write a programm using java
write a programm using java  print the following using java programming
Using throw in java method
Using throw in java method  using throw with method implies what
A Java Program by using JSP
A Java Program by using JSP  how to draw lines by using JSP plz show me the solution by using program
survey poll using java
survey poll using java  how to make a survey poll using java? i am using netbeans and glassfish
RARP program using java
RARP program using java   hai, how to implement the RARP concept using java
java code using swings
java code using swings  code that should be able to enter data of student details using all swings into the access database using jdbc connectivity
java using Stack - Java Beginners
java using Stack  How convert decimal to binary using stack in java
Image using Java coding
Image using Java coding  Hai, Display image in pdf file using Java coding through Xsl file.. Please help me.. xsl file generate the pdf file
covert the following using java
covert the following using java  how to convert (for eg : 2.89) . this decimal to binary in java
accessing xml using java
accessing xml using java  I need to retrieve some elements in xml file using java   Hi Friend, Please visit the following links:
uSING JAVA API  How to extract Quickbooks online data using Quickbooks api( Rest ApI) write in java code ? 1.register company account 2..register application 3.get apptoken,consumer key,consumer screte can i
SMS using Java
/viewqa/Java-Beginners/15745-Sms-to-mobile-using-java-application.html ) How...SMS using Java  I am having a concern , where is the main class in, if this seems silly please let me know How to execute this java
Read XML using Java
of all i need to read xml using java . i did good research in google and came to know.../xmlParsing/java-xml-parsing-using-sax.shtml XML using Java  Hi All, Good Morning, I have been working
Java Spring Hibernate Struts Training Retrieve database from the table dynamically in jsp from oracle using servlet What does core Java include? java What are some way to learn Java quickly? Is it required to learn Java before learning Java Script? Is it necessary to learn java script before learning PHP? Are HTML5 and Java Script boosts Java career? Is Java object oriented? Fingerprint application with Java Uninstall Oracle Virtual Box JSON to HashMap Free Java online Training I want example of Control Statement in Java ANSI Color Codes with Python Create a Program that Calculates Input What is difference between JDK,JRE and JVM? How to see ubuntu version on server? How to get Page Source in Selenium (WebDriver) using Java? The path to the driver executable must be set by the webdriver.gecko.driver system property parse data from a link in java Java Program Qns using BlueJ How to fix HAX Kernel Module Is Not Installed error? Installing Audacity Looking for code Logic to check track changes & Coments in MSWord, MSWordx, MSExcel, MSExcelx is ON/OFF Installing JDK on Mac SAX Parser exception ERROR 601 (42P00): Syntax error. Encountered Thread java.lang.NoClassDefFoundError: org/apache/commons/fileupload/FileItemFactory How to install Ubuntu 16.04 LTS? HttpServletRequest cannot be resolved to a type in eclipse - Solved Unhandled event loop exception GC overhead limit exceeded Spring Data jpa with apache phoenix Caused by: java.lang.IllegalArgumentException: Not a host:port pair: PBUF o.a.h.h.z.RecoverableZooKeeper - Possibly transient ZooKeeper, quorum= com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager class not found com.thinkaurelius.titan.diskstorage.hbase.HBaseStoreManager not found How to download and install Java 8 on Windows? How to uninstall JDK 7? How to install gtk-doc-tools package in Ubuntu? How to install Oracle JDK 8 on Ubuntu? ejabberd_ctl.beam not found - Solved How to convert date to UTC format in Java? How to install autoconf, automake and libtool in Ubuntu 15.10? How to convert current date to mm dd yyyy format in Java? How to convert current date to dd mm yyyy format in Java? How to stop window closing in "internalFrameClosing" event. How to find list of all index in Neo4j? file location SASLError using PLAIN: not-authorized


Advertisement null