Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to get my accurate latitude longitude of my linux device on command line. Like Geolocation in HTML5. I dont have access to browser

I want to send email of current Lat Long from my Linux device. I tried geo location in HTML5 browsers, it works great. But i want it on command line. I tried so many options such as curl, geoip to some websites by IP, but they all show my ISP's position, not mine. I prefer using it on command line or python etc tools.

I could successfully write a python program which opens a locally saved page of HTML5 geolocation code and shows accurate lat long also. Then automatically python fetches lat long from browser and shows on terminal. File: test.py

from splinter.browser import Browser
import os.path
import time

browser = Browser()
browser.visit('file://' + os.path.realpath('geo.html'))

time.sleep(5)

elements = browser.find_by_css("#demo")

div = elements[0]

print div.value

browser.quit()

File: geo.html

   <html>
        <head>
            <title>Test</title>
        <p id="demo"></p>
            <script type="text/javascript">

    var x = document.getElementById("demo");

    function getLocation() {
        if (navigator.geolocation) {
            navigator.geolocation.getCurrentPosition(showPosition);
        } else { 
            x.innerHTML = "Geolocation is not supported by this browser.";
        }
    }

    function showPosition(position) {
        x.innerHTML = "Latitude: " + position.coords.latitude + 
        "<br>Longitude: " + position.coords.longitude;  
    }
            </script>
        </head>
        <body>
            <p>The element below will receive content</p>
            <div id="div" />
            <script type="text/javascript">getLocation()</script>
        </body>
    </html>

But a bug is there, that every time python opens the browser, i have to click "Share location: Yes" in browser. Because the page is running on local server, not on any webserver. So this solution was not applicable. Can anyone suggest me reliable solution to get my current lat long in linux in command line?

like image 865
Taral Shah Avatar asked Nov 08 '22 21:11

Taral Shah


1 Answers

On Linux you need to talk to gpsd.

  • You could talk to libgps: http://manpages.ubuntu.com/manpages/wily/en/man3/libgps.3.html

  • You could use the DBUS interface.

  • Or you could use the python interface: how to use/install gps python library

like image 138
Ben Avatar answered Nov 14 '22 21:11

Ben