aspose file tools*
The moose likes Java in General and the fly likes Measure distance between two cities ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Measure distance between two cities ?" Watch "Measure distance between two cities ?" New topic
Author

Measure distance between two cities ?

Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

HI ranchers,

I get stuck in the middle of my project which is a java application , where i need to calculate a approximate distance between two cities . But i am not getting anything by googling . I explore the Google Map API, but they provide it with JavaScript , There are some classes like GRoute which gives me distance , but not able to proceed further with it !!

Is any other Java API i can use or any programming logic or data structure i can use to solve tis problem ?

Thanks in advance . Waiting for Help !


[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41574
    
  54
Googling for "distance between cities" finds a number of online applications. If nothing better comes along, you should be able to drive one of those using the HttpUrlConnection class, and then parsing the resulting HTML.


Ping & DNS - my free Android networking tools app
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Hi
So you means i need to hit a site which offer to calculate distence between two cities and parse the repsonse html file for desired oupput distance !! But how can i set that parameter of 'From City' to 'To city' on that site ?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41574
    
  54
Here's an example of how to post a form with parameters and read the response that is being sent.
Prakash Subramanian
Ranch Hand

Joined: Feb 03, 2005
Posts: 32
Hello,

I found a web service which claims to have support for the operation you are looking for. The link is http://codebump.com/services/placelookup.asmx

Hope it helps.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Thanks for the help !
Hello,

I found a web service which claims to have support for the operation you are looking for. The link is http://codebump.com/services/placelookup.asmx

This site gives distance between cities only whic comes under US and Canada territories . Plus it is not a free service .

Still thanks for the help , I am already found a site which gives not Distance , but Longitude and latitude , which help to calculate the approximate distance
Vikas Kapoor
Ranch Hand

Joined: Aug 16, 2007
Posts: 1374
Hi Sagar,

This may help you.It gives many information like distance in kilometer,miles.
It provides longitude,latitude and much more.
And it's free.
[ May 15, 2008: Message edited by: Vishal Pandya ]
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
There will be classes and methods in java.awt that convert rectangular screen co-ordiantes to polar co-ordinates. You can provide the appropriate class with (int x, int y, int delta-x, int delta-y) using city first(x,y) for the first two then city second(x,y) to the second pair of ints then call rectangular to polar and get magnitude on the result.

To do surface of Earth ( Great Circle Distance ) is complex, but one does not get asked to do Great Circle Distance unless your instructor is a Pilot or Astronomer.


"The differential equations that describe dynamic interactions of power generators are similar to that of the gravitational interplay among celestial bodies, which is chaotic in nature."
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
Two methods which do what Nicholas Jordan suggests are Math#atan2() and Math#hypot. But they do assume you are working on a flat surface.
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Thanks for the new suggestion and help !

But i already started working on what 'Ulf Dittmer' was suggest .
I hit the URL to get XML file as a response and extract longitude and latitude , which gives me a approximate distance (i.e a straight nautical distance )

Thanks once again !
Ahmed Taha
Greenhorn

Joined: Jul 10, 2009
Posts: 2
You can use the LatLongLib , from this post

MyBlog
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

ahmd taha wrote:You can use the LatLongLib , from this post

MyBlog

Hey thanks ahmd and welocme to JavaRanch
I already implemented my requirement, (which is a year old) and its working fine.

I hadn't explore the library yet, but one question for you ?

Can I calculate the distance of any city of the word to any city ? And a short description about how it calculate the longitude and latitude of a city, I mean is it request this data from internet or do you have you own logic/data structure inside.
Ahmed Taha
Greenhorn

Joined: Jul 10, 2009
Posts: 2
it is just calculate the Distance between Latitude and longitude coordinates
i assume you know the Latitude and longitude coordinates of the city
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

Ahmed Taha wrote:it is just calculate the Distance between Latitude and longitude coordinates
i assume you know the Latitude and longitude coordinates of the city

Ohh, Ok. Got it.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38412
    
  23
Ahmed Taha wrote:You can use the LatLongLib , from this post
Please don't post that sort of answer to a year-old thread. Read this.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Measure distance between two cities ?