简体   繁体   English

在Java中获取用户当前的经度和经度

[英]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. 我在这里看到了很多关于Java的问题,但我看到的每一个问题似乎都是以Android为中心的:当他们打电话来自计算机而不是移动设备时,我需要一种方法来获取用户当前的经纬度。 I know that this is doable in C#, but I'm not sure how exactly to do it in Java. 我知道这在C#中是可行的,但我不确定如何在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. 我之前使用过Google Maps的restful API:但是,它想要一个地址作为输入,我只想获得用户当前的lat / lon,我觉得这比调用Google更简单。 Any help would be greatly appreciated. 任何帮助将不胜感激。

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. 一般而言,计算机没有内置GPS设备。 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. 另一种方法是尝试将IP地址映射到物理位置。 That is unreliable because IP addresses are not fundamentally tied to physical locations. 这是不可靠的,因为IP地址并没有从根本上与物理位置相关联。 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. 通过IP地址进行地理定位只会使用户的网络服务提供商能够(并且愿意)向第三方提供位置信息。 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. 它不是标准Java平台的一部分。

  • There is another approach which is based on geolocating WiFi hotspots 1 . 还有另一种方法是基于地理定位WiFi热点1 This link goes into more detail: http://gps.about.com/od/glossary/g/wifi_position.htm . 此链接更详细: 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. 然而,这取决于用户的WiFi提供商注册他们的lat / long,以及用户打开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. 1 - 我记得有一位同事在进行iPhone开发时提到,当WiFi打开时,他在iPhone(带GPS)上获得了更准确的地理定位。

Java doesn't currently provide a general platform-independent API to get location. Java目前不提供通用的独立于平台的API来获取位置。 However, there may be platform-specific ways. 但是,可能存在特定于平台的方式。

Most implementations on a desktop machine will geolocate via IP address; 台式机上的大多数实现都将通过IP地址进行地理定位; you can use these services from Java ( Best way to get geo-location in Java ). 您可以使用Java中的这些服务( 在Java中获取地理位置的最佳方式 )。 However, if operating systems provide a more general API, it can take advantage of any GPS hardware. 但是,如果操作系统提供更通用的API,则可以利用任何GPS硬件。

For Mac OS, https://superuser.com/questions/346302/is-there-a-way-to-access-os-x-location-services-from-the-command-line 对于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 . 这里有Gnome提供的库的详细信息,以及KDE支持计划的编写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM