9

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.

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.

icco
  • 3,033
  • 4
  • 33
  • 47

6 Answers6

9

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.

npdoty
  • 4,566
  • 23
  • 24
6

Since whereami is not (officially) available for Swift 2 and Swift 3, I digged a bit deeper and found locateme what does its job on OSX 10.12.4:

Screenshot of LocateMe Its asks for permission the first time, and then it works without any additional interaction. Also, the format parameter is nice:

enter image description here

Thomas Kekeisen
  • 4,449
  • 4
  • 34
  • 54
2

geolocate-cli is still in alpha but should do the job.

fmark
  • 54,196
  • 25
  • 97
  • 106
1

Scraping the first Google hit result for "geoip" seems to work easily enough.

$ 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).

ephemient
  • 189,938
  • 36
  • 271
  • 385
  • Well, I'm in Cambridge MA yet maxmind puts me in Boston (close, but not the same), and hostip.info puts me in... Reston, VA, etc. :) Not surprising since probably more than half the info still comes from `whois`. – vladr Mar 13 '10 at 06:12
  • Cool. I'm trying to think now of a good way to grep out just the city, region and country. – icco Mar 14 '10 at 20:13
  • This method gave me a more accurate result than the program "whereami", but it is still far from where I really am. Plus, it requires internet connection. I am quite surprise that there seem not to be a gps locator in my laptop that tells me where I really am. – Student Jun 01 '19 at 18:05
1

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 Browserlocation for more details.

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!

Community
  • 1
  • 1
appas
  • 3,840
  • 2
  • 17
  • 17
0

Wireless internet meaning WiFi, CDMA2k, UMTS, EDGE... ? And your carrier? Not that it matters.

Really. I'd start investing in a GPS dongle.

vladr
  • 63,475
  • 18
  • 128
  • 128
  • just good ol 802.11g. I shouldn't need a gps if my browser can get my location without one. – icco Mar 13 '10 at 05:09
  • I assume this is for yourself -- a mobile computer? :) You can get *a* location without GPS, never guaranteed to be the right one (GeoIP services can place you in a completely different state just because the ISP is registered there in `whois`.) – vladr Mar 13 '10 at 06:15