Graham Carling
Graham Carling

Reputation: 563

Get user's current latitude and longitude in Java

I see this question asked a lot here for Java, but every single one I see seems to be Android-centered: I need a way of getting the user's current latitude and longitude when they call is coming from a computer, not a mobile device. I know that this is doable in C#, but I'm not sure how exactly to do it in Java. I've used Google Maps' restful API before: however, it wants an address as input, and I just want to get the user's current lat/lon, which I feel is simpler than making a call to Google. Any help would be greatly appreciated.

Upvotes: 5

Views: 10025

Answers (2)

Stephen C
Stephen C

Reputation: 718758

The reasons you can't do this for computers in general is that computers in general are pretty obvious when you think about it:

  • Computers in general don't have builtin GPS devices. So the computer can't tell you where is based on that.

  • The other approach is to try to map the IP address to a physical location. That is unreliable because IP addresses are not fundamentally tied to physical locations. Geolocation by IP address is only going to work to that the user's network service provider is able (and willing) to provide location information to 3rd parties. In practice, this information is very "patchy". The other problem is that you need to rely on an external "geolocation service" to get the information. It is not part of the standard Java platform.

  • There is another approach which is based on geolocating WiFi hotspots1. This link goes into more detail: http://gps.about.com/od/glossary/g/wifi_position.htm. However it depends on the user's WiFi provider registering their lat/long, and the user turning on WiFi.


1 - I remember a colleague doing iPhone development mentioning that he got more accurate geolocation on an iPhone (with GPS) when the WiFi was turned on.

Upvotes: 4

Joe
Joe

Reputation: 31067

Java doesn't currently provide a general platform-independent API to get location. However, there may be platform-specific ways.

Most implementations on a desktop machine will geolocate via IP address; you can use these services from Java (Best way to get geo-location in Java). However, if operating systems provide a more general API, it can take advantage of any GPS hardware.

For Mac OS, https://superuser.com/questions/346302/is-there-a-way-to-access-os-x-location-services-from-the-command-line

There's detail here of a Gnome-provided library, and also a write-up of plans for support in KDE.

Upvotes: 5

Related Questions