0

I know that Android has a way to get location, and W3C has a location API for web browsers, but is there a way to get my location progromatically on my own machine?

I'm running Ubuntu 14.10

Edit: Removed ruby tag. Opening up to any language.

NullVoxPopuli
  • 61,906
  • 73
  • 206
  • 352
  • 1
    you need GPS for that – hek2mgl Mar 10 '15 at 22:50
  • this doesn't need gps, and it is really accurate: http://www.w3schools.com/html/html5_geolocation.asp – NullVoxPopuli Mar 10 '15 at 22:52
  • @NullVoxPopuli Hmm, it gives me a location 5 km away. If I would live in a small village I expect the accuracy even worse. – hek2mgl Mar 10 '15 at 22:56
  • Did your browser ask you to allow w3schools to use your location? – NullVoxPopuli Mar 10 '15 at 22:57
  • @NullVoxPopuli Yes, and I confirmed – hek2mgl Mar 10 '15 at 22:57
  • Also think about what happens if you are using a VPN together with geolocation. Then the location of the router behind the VPN gateway would being used – hek2mgl Mar 10 '15 at 23:00
  • On OS X there's [whereami](http://victor.github.io/whereami/) using Apple's CoreLocation (quite accurate, just 10 meters off in my case). Not sure if there is an equivalent library / service for Linux. – Stefan Mar 10 '15 at 23:34
  • @Stefan 10 meters accuracy means that your Mac has GPS integrated and is using that – hek2mgl Mar 11 '15 at 12:30
  • @hek2mgl no, my Mac does not have GPS. Apple determines my location based on the WiFi I'm connected to. The GPS data is collected from iPhones and iPads on the same network. – Stefan Mar 11 '15 at 12:52
  • @Stefan Oh yes, hadn't the WIFI in mind (mentioned in the docs, you are right).. However, the location detection itself is based on GPS. – hek2mgl Mar 11 '15 at 13:24
  • If there is GPS data tied to WiFi networks, would it be possible to lookup a GPS location based on the WiFi network? – NullVoxPopuli Mar 11 '15 at 14:17

1 Answers1

1

Short answer for your question: with just Ruby - you can't decode your location when working on localhost.

You might try using gem trifle for that, but what it does is a GeoIP country lookup. Because you want to try to decode your localhost - it can't just work.

How it works is - it has a database of IP's and associated cities. This will work when you deploy the application, and it will try to decode your visitor's locations.

Example in their github is quite easy. Once your trifle gem is set up properly (it requires redis server, and loading provided file with predefined IP's and associated cities):

trifle = Trifle.new(Redis.new)
trifle.find "223.255.128.0"
# => ["HK", "Hong Kong"]

# or you can try to play with it in controller
trifle.find request.remote_ip

What you can do, to decode your location when working in locahost:

There is a way to obtain location of computer accessing your application using browser's capabilities. There is a great article in tutsplus - http://code.tutsplus.com/tutorials/an-introduction-to-the-geolocation-api--cms-20071. This will work with localhost

There is quite long code presented, so I wont copy it here, but explanation is very good. You can easily implement your solution based on the article, and the location is very accurate. The limitation for this is - browser asks if it can obtain location of computer, and user might not allow that operation. There is no way for forcing it.

Hope that helps!

Good luck!

Paweł Dawczak
  • 9,519
  • 2
  • 24
  • 37
  • the intention behind my question is to have some cron job post my laptop's location somewhere every 10 or so seconds, so that if it gets stolen, I'll know where it is, provided there is a network connection. – NullVoxPopuli Mar 11 '15 at 12:44
  • Yeah, I found prey last night. And am using it. They have a free tier which is exactly what I need. For the take of curiosity though, I still wonder if my question in possible. Maybe not with ruby, but possibly node and a headless browser to use the HTML5 location api? Idk – NullVoxPopuli Mar 11 '15 at 14:15