Make a script read GPS geolocation

Make a script read GPS geolocation
0

#1

Hello, I want to make a script in python which will connect to the user’s GPS and will print his\her Long, Lat. Unforrunately I can not figure out the right way to do it. Can someone please tell me what I have to do?


#2

So reading a GPS location involves reading data being sent from an actual GPS device/service. As far as I know, that normally means device-specific integration for reading the data feeds. For example, on a mobile phone, one would need to request System Location access like when an app you download requests such permission. But grabbing the serialized data from a GPS-enabled Arduino is a different story. Furthermore, what if the PC the script is running on has no GPS module? Providing a general overview for all cases is way above my skill level, sorry.

However, if instead you’d settle for GeoIP lookup I might be able to help. This involves grabing the user’s public IP address and then using a database to convert that to a general location. This can be done in a number of ways, but for simplicity I’m going to do this example with no outside libraries and using only RESTful APIs. The caveat to this method is it assumes a usable internet connection. Any IP masking techniques like using proxys, tor, or VPNs will give you the location of the end node, not the actual user’s IP/location.

The Fundamentals:

import requests

# Step 1) Find the public IP of the user. This is easier said that done, look into the library Netifaces if you're
# interested in getting the public IP locally.
# The GeoIP API I'm going to use here is 'https://geojs.io/' but any service offering similar JSON data will work.

ip_request = requests.get('https://get.geojs.io/v1/ip.json')
my_ip = ip_request.json()['ip']  # ip_request.json() => {ip: 'XXX.XXX.XX.X'}
print(my_ip)
# Prints The IP string, ex: 198.975.33.4

# Step 2) Look up the GeoIP information from a database for the user's ip

geo_request_url = 'https://get.geojs.io/v1/ip/geo/' + my_ip + '.json'
geo_request = requests.get(geo_request_url)
geo_data = geo_request.json()
print(geo_data)
# {
# "area_code": "0",
# "continent_code": "NA",
# "country": "United States",
# "country_code": "US",
# "country_code3": "USA",
# "ip": "198.975.33.4",
# "latitude": "37.7510",
# "longitude": "-97.8220",
# "organization": "AS15169 Google Inc.",
# "timezone": ""
# }  This is a fake example I grabbed from the GeoJS website

Putting It All Together:

def display_ip():
    """  Function To Print GeoIP Latitude & Longitude """
    ip_request = requests.get('https://get.geojs.io/v1/ip.json')
    my_ip = ip_request.json()['ip']
    geo_request = requests.get('https://get.geojs.io/v1/ip/geo/' +my_ip + '.json')
    geo_data = geo_request.json()
    print({'latitude': geo_data['latitude'], 'longitude': geo_data['longitude']})

if __name__ == '__main__':
    display_ip()

I apologize if this isn’t what you needed. Sorry I couldn’t be more helpful on the GPS front. You could probably use some well known modules to execute some script in Firefox or Chrome to grab the geolocation from a service that already has GPS access. But to be honest, that’s sounds like it’s going down a nefarious path so I wouldn’t do that.

If you are planning on making some kind of legitimate app in Python that needs mobile GPS permission, I would highly recommend looking into Kivy. If you’re new to GUI’s it’s going to be a long road. But you can certainly build a mobile app that can be appropriately signed and can be granted permissions for integrated systems like GPS, camera, accelerometer, etc…


#3

What you commented is extremely helpful and I want to thank you for that!


#4

Glad it helped. Happy Coding