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.
2 Answers
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.

- 698,415
- 94
- 811
- 1,216
-
I'm having trouble finding the code, but I'm almost certain that I wrote a webpage about a year ago in asp.net and C# that would open up and tell you your current lat/lon. I specifically remembering noticing that I was able to get a more accurate picture of where I was once I connected to WiFi - I definitely was getting my position from a computer and, once WiFi was on, it was pinpoint accurate. Do you have any idea how this might have been done? – Graham Carling Apr 06 '13 at 05:31
-
Additionally to the Stephen C description, I want to add that Google Devices which shoots the location, adds to their DataBase near Wi-Fi devices MAC Address (Physical address) to identify location. But actually best way to identify user location is GPS.With other way you can get only Country, Enterprise location if this is public information. – Musa Dec 05 '16 at 16:31
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.
-
Thanks, this seems to be what I'm looking for. I'l take a look at Skyhook, it seems like a solid choice. – Graham Carling Apr 08 '13 at 19:09