1

I need to be able to map out in coordinates real world cities and then have a method that calculated the distance between two cities that I give it... What would be the best way to do this? just citie object that has coordinates and then ill figure out the fomula for calculating the distance my self or a hash map or something... another thing is how am i gonna figure out the coordinates of the cities? Im talking about real world cities so yeah... i had an idea of getting the latitude and longitude of the cities and then somehow when calculating distance turn it into kilo meters... If anyone can help my if this I would be glad!

Thank you very much! If you need more info just tell me and ill add it.

What I mainly need: A suggestion to based on what map the cities and a lead on implementing distance calculations.. I would not like to check out the distance between each city i make to all the rest online then put it in because it seams to be a lot of work and if i manage to figure out a algorithm then it would be much faster and easier to add cities to the game...

Trixmix
  • 81
  • 9

2 Answers2

4

First you need to translate city name to geographical coordinates. GeoNames is a big database of geographical locations including coordinates out of many others available.

When having geographical coordinates of two cities, you can use simple equation to calculate the distance, see How to find distance from the latitude and longitude of two locations? and Great-circle distance.

Community
  • 1
  • 1
Tomasz Nurkiewicz
  • 334,321
  • 69
  • 703
  • 674
  • Thanks! Can you tell me one last thing looking at this: N 40° 42' 51' W 74° 0' 21' the N means northern hemosphere 40 means that its 40 degrees but what is the 42 and 51? – Trixmix Jun 17 '12 at 14:57
  • @Trixmix: http://en.wikipedia.org/wiki/Minute_of_arc and http://en.wikipedia.org/wiki/Second_of_arc#Symbols_and_abbreviations – Tomasz Nurkiewicz Jun 17 '12 at 15:04
  • If i use the Haversine formula how would the formula know if the latitude and longitude are in the northern or eastern of w/e hemispheres? How can i tell it? or will it work just by the degrees..? – Trixmix Jun 17 '12 at 15:27
  • Im using this formula: R = earth’s radius (mean radius = 6,371km) Δlat = lat2− lat1 Δlong = long2− long1 a = sin²(Δlat/2) + cos(lat1).cos(lat2).sin²(Δlong/2) c = 2.atan2(√a, √(1−a)) d = R.c found here: http://www.movable-type.co.uk/scripts/latlong.html – Trixmix Jun 17 '12 at 15:27
  • @Trixmix: most likely north and east are positive while south and west are negative values. Please post another question and leave follow-up in comments. – Tomasz Nurkiewicz Jun 17 '12 at 15:35
1

one part is GREAT CIRCLE DISTANCE - there are lots of examples.

finding a data set of city coordinates may be trickier.

Randy
  • 16,480
  • 1
  • 37
  • 55