如何在 bash 中获取我的地理位置
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2436648/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get my geolocation in bash
提问by icco
I am looking for a good geolocation api to use from bash. I want this call to return at the very minimum the name of the city I am in, and the state.
我正在寻找一个很好的地理定位 api 来从 bash 中使用。我希望这个电话至少返回我所在城市的名称和州名。
I imagine that there must be some site I can curl, or some scripting language that has a package that works. The machine does not have a GPS, but it does use wireless internet most of the time if that is needed.
我想一定有一些我可以卷曲的站点,或者一些具有可用包的脚本语言。这台机器没有 GPS,但如果需要,它在大部分时间都会使用无线互联网。
采纳答案by npdoty
I was looking at this problem again recently and found whereami, a geolocation bash script for Mac OS X Snow Leopard, which takes advantage of Core Location. This will tend to be much more precise than IP geolocation, and it works well in my tests.
我最近又在看这个问题,发现whereami了一个用于 Mac OS X Snow Leopard 的地理定位 bash 脚本,它利用了 Core Location。这往往比 IP 地理定位更精确,并且在我的测试中运行良好。
回答by Thomas Kekeisen
Since whereamiis not (officially) available for Swift 2 and Swift 3, I digged a bit deeper and found locatemewhat does its job on OSX 10.12.4:
由于whereami不适用于 Swift 2 和 Swift 3(官方),我深入挖掘了一下,发现locateme在 OSX 10.12.4 上的作用是什么:
Its asks for permission the first time, and then it works without any additional interaction. Also, the formatparameter is nice:
回答by fmark
geolocate-cliis still in alpha but should do the job.
geolocate-cli仍处于 alpha 阶段,但应该可以完成这项工作。
回答by appas
If your platform is a *nixlike that supports network-manager, you can use this script:
curl "https://maps.googleapis.com/maps/api/browserlocation/json?browser=firefox&key=AIzaSyDBgL8fm9bD8RLShATOLI1xKmFcZ4ieMkM&sensor=true" --data-urlencode "`nmcli -f SSID,BSSID,SIGNAL dev wifi list |perl -ne "if(s/^(.+?)\s+(..:..:..:..:..:..)\s+(.+?)\s*$/&wifi=mac:\2|ssid:\1|ss:\3/g){print;}"`"Check out How to create a script to query Google Browserlocationfor more details.
如果您的平台是支持 的 *nixlike network-manager,您可以使用此脚本:
curl "https://maps.googleapis.com/maps/api/browserlocation/json?browser=firefox&key=AIzaSyDBgL8fm9bD8RLShATOLI1xKmFcZ4ieMkM&sensor=true" --data-urlencode "`nmcli -f SSID,BSSID,SIGNAL dev wifi list |perl -ne "if(s/^(.+?)\s+(..:..:..:..:..:..)\s+(.+?)\s*$/&wifi=mac:\2|ssid:\1|ss:\3/g){print;}"`"查看如何创建脚本以查询 Google Browserlocation以获取更多详细信息。
Notice that there's some limit to this service. It was working fine, at first, but now I get "status" : "OVER_QUERY_LIMIT". I don't know what the limit is because I can only seem to find geocoding references, but if anyone knows, please tell me!
请注意,此服务有一些限制。起初它运行良好,但现在我得到了"status" : "OVER_QUERY_LIMIT". 我不知道限制是什么,因为我似乎只能找到地理编码参考,但如果有人知道,请告诉我!
回答by ephemient
Scraping the first Google hit result for "geoip" seems to work easily enough.
抓取“geoip”的第一个 Google 搜索结果似乎很容易。
$ curl http://www.maxmind.com/app/locate_my_ip |
> awk '/<table>/{RS=RS RS}/tblProduct1/'
I haven't checked their EULA to see if using their service this way is okay, but they do also provide GeoIP libraries for C, Perl, Python, etc. with a "lite" database that you can distribute (with conditions).
我还没有检查他们的 EULA 以查看以这种方式使用他们的服务是否可以,但他们也为C、Perl、Python等提供了 GeoIP 库,以及一个可以分发的“精简版”数据库(带条件)。
回答by vladr
Wireless internet meaning WiFi, CDMA2k, UMTS, EDGE... ? And your carrier? Not that it matters.
无线互联网意味着 WiFi、CDMA2k、UMTS、EDGE...?你的运营商呢?并不是说这很重要。
Really. I'd start investing in a GPS dongle.
真的。我会开始投资购买 GPS 加密狗。

