• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Measure distance between two cities ?

 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 !
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here's an example of how to post a form with parameters and read the response that is being sent.
 
Prakash Subramanian
Ranch Hand
Posts: 32
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1374
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Posts: 51332
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can use the LatLongLib , from this post

MyBlog
 
Sagar Rohankar
Ranch Hand
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2907
1
Java Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 51332
86
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic