-1

I am trying to create a program to find the distance between two user inputted cities in java.

I've looked at a few other posts, and I've seen the Geocoder API recommended, to get the lat and lang values for the cities. However, I've only just started learning Java, and all those solutions were slightly complicated. Is there an easier method, or will I have to go about understanding API's?

  • Your computer does not know about cities, therefore it would have to ask someone that does. Computers talk to each other using API. So either you'll have to bite the bullet and use an API, or choose a less demanding starting exercise. – Amadan May 21 '19 at 11:25
  • @Andy Turner The immediate question is "how to get lat/lon of a city without API", not "how to calculate distance between coordinates". I'd say this is not a dupe (though there is scarce benefit from reopening the question, only to close it with "nope, use API"). – Amadan May 21 '19 at 11:26

2 Answers2

0

Have you considered writing a GIS application with a library like NASA WorldWind? This way the user can click in two locations on a map and you can compute the haversine distance between them.

0

Once you know the latitudes and longitudes you can calculate.

This is fairly complex as the latitude (North to South) has a constant scale, the longitude (East to West) is not a constant scale and tends towards zero as you get nearer the poles.

This site give a very good description of the basic calculations required
https://www.movable-type.co.uk/scripts/latlong.html

James Anderson
  • 27,109
  • 7
  • 50
  • 78