Get your location via python

In any case, I can get the location of my device through python. Currently, I have to use selenium and open a browser, use the location service website and set the result of this for variables for lat / long.

But is there an easier way to do this?

UPDATE: I use a 3G dongle on my RaspberryPi, so I'm looking for a way to get a specific lat / for a long time - I can successfully do this through a web service, just wondering if there is a faster way built into python for these requests?

+19
python geolocation
source share
11 answers

Update : This API endpoint is deprecated and will stop working on July 1, 2018. For more information, please visit: https://github.com/apilayer/freegeoip#readme "

Assumptions:

The device for which the location is being searched is the device on which Python is running. You have access to the Internet (it seems fair as you mention the location service site)

In this case, there are services similar to the one indicated in the comment, where the IP address of the request is used to generate the location. For example, http://freegeoip.net/ .

import requests import json send_url = 'http://freegeoip.net/json' r = requests.get(send_url) j = json.loads(r.text) lat = j['latitude'] lon = j['longitude'] 

disadvantages

Only IP is used to create a location.

+27
source share

or, just like that

 import geocoder g = geocoder.ip('me') print(g.latlng) 
+25
source share

Others mentioned several services, but another is mine, https://ipinfo.io , which will provide you with latitude, longitude, and tons of other information:

Uses for Bash:

 $ curl ipinfo.io { "ip": "24.6.61.239", "hostname": "c-24-6-61-239.hsd1.ca.comcast.net", "city": "Mountain View", "region": "California", "country": "US", "loc": "37.3845,-122.0881", "org": "AS7922 Comcast Cable Communications, LLC", "postal": "94040" } 

If you only need coordinate data, you can get it by calling /loc :

 $ curl ipinfo.io/loc 37.3845,-122.0881 

See https://ipinfo.io/developers for more details.

+7
source share

Since http://freegeoip.net/json the API endpoint is deprecated and stops working on July 1, 2018. So, they are releasing the new API http://api.ipstack.com .

So you can try this with the new API:

 import requests import json send_url = "http://api.ipstack.com/check?access_key=YOUR_ACCESS_KEY" geo_req = requests.get(send_url) geo_json = json.loads(geo_req.text) latitude = geo_json['latitude'] longitude = geo_json['longitude'] city = geo_json['city'] 

To get your own ACCESS_KEY , you first need to create an account on ipstack.com which is free at https://ipstack.com/signup/free .

Along with latitude , longitude and city ; You can also get zip , continent_code , continent_name , country_code , country_name , region_code , region_name .

Limitation : A free account allows you only 10,000 requests / month. If your requirement is greater, you can upgrade your account.

For more information about this new API, you can visit at https://github.com/apilayer/freegeoip

Link: @JishnuM answer

+2
source share

Here is the Python geocoding module with GeoFreeIP

Example: http://geocoder.readthedocs.io/

 $ pip install geocoder 

Using CLI

 $ geocode '99.240.181.199' --provider freegeoip --pretty --json 

Using Python

 >>> import geocoder >>> g = geocoder.freegeoip('99.240.181.199') <[OK] Freegeoip - Geocode [Ottawa, Ontario Canada]> >>> g.json 
+1
source share

played with this, so thanks to all the useful answers in this thread (and SO in general!) I thought that I would share my short program in case someone wants to see it combined with a big circle of calc

 import geocoder import requests freegeoip = "http://freegeoip.net/json" geo_r = requests.get(freegeoip) geo_json = geo_r.json() address=input('enter an address: ') g= geocoder.google(address) lat_ad=g.latlng[0] lon_ad=g.latlng[1] user_postition = [geo_json["latitude"], geo_json["longitude"]] lat_ip=user_postition[0] lon_ip=user_postition[1] #Calculate the great circle distance between two points on the earth (specified in decimal degrees) from math import radians, cos, sin, asin, sqrt # convert decimal degrees to radians lon_ad, lat_ad, lon_ip, lat_ip = map(radians, [lon_ad, lat_ad, lon_ip, lat_ip]) # haversine formula dlon = lon_ip - lon_ad dlat = lat_ip - lat_ad a = sin(dlat/2)**2 + cos(lat_ad) * cos(lat_ip) * sin(dlon/2)**2 c = 2 * asin(sqrt(a)) km = 6367 * c #end of calculation #limit decimals km = ('%.0f'%km) print(address+' is about '+str(km)+' km away from you') 
+1
source share

Using the ipdata.co API

This answer uses a β€œtest” API key, which is very limited and designed only to test multiple calls. Register to receive your free API key and receive up to 1,500 development requests daily.

 import requests r = requests.get('https://api.ipdata.co?api-key=test').json() r['country_name'] # United States 
+1
source share

@JishnuM answered the questions amazingly.

2 cents from me. You do not need to import the json library.

You can do the following:

 freegeoip = "http://freegeoip.net/json" geo_r = requests.get(freegeoip) geo_json = geo_r.json() user_postition = [geo_json["latitude"], geo_json["longitude"]] print(user_postition) 
0
source share

The location based on the IP address gives only the location for your server. To find a location based on your current location, you need to give the browser access to the location. This can be done using selenium.

 from selenium import webdriver from selenium.webdriver.chrome.options import Options from selenium.webdriver.support.ui import WebDriverWait def getLocation(): chrome_options = Options() chrome_options.add_argument("--use-fake-ui-for-media-stream") timeout = 20 driver = webdriver.Chrome(chrome_options=chrome_options) driver.get("https://mycurrentlocation.net/") wait = WebDriverWait(driver, timeout) longitude = driver.find_elements_by_xpath('//*[@id="longitude"]') longitude = [x.text for x in longitude] longitude = str(longitude[0]) latitude = driver.find_elements_by_xpath('//*[@id="latitude"]') latitude = [x.text for x in latitude] latitude = str(latitude[0]) driver.quit() return (latitude,longitude) print getLocation() 

A tutorial on how to do this can be found here or find the GitHub repository here.

0
source share

The endpoints of freegoip.net and api.ipstack.com seem to use the location of the Internet service provider (ISP), not the location of my device. I tried to curl http://api.ipstack.com/check?access_key=YOUR_KEY and I got

 {"ip":"106.51.151.31","type":"ipv4","continent_code":"AS","continent_name":"Asia","country_code":"IN","country_name":"India","region_code":"KA","region_name":"Karnataka","city":"Bengaluru","zip":"560008","latitude":12.9833,"longitude":77.5833,"location":{"geoname_id":1277333,"capital":"New Delhi","languages":[{"code":"hi","name":"Hindi","native":"\u0939\u093f\u0928\u094d\u0926\u0940"},{"code":"en","name":"English","native":"English"}],"country_flag":"http:\/\/assets.ipstack.com\/flags\/in.svg","country_flag_emoji":"\ud83c\uddee\ud83c\uddf3","country_flag_emoji_unicode":"U+1F1EE U+1F1F3","calling_code":"91","is_eu":false}} 

Checking the lat, long location on google maps indicates that this is the location of the service provider, not the location of the server / device.

This solution is still applicable, but probably only for the granularity of the city or country, depending on how the provider organizes and finds its gateways.

0
source share

Ipregistry returns location data along with currency, time zone, etc. For your current UP address or specified (disclaimer, I start the service). There is also an official Python client available on PyPI:

 $ pip install ipregistry 
 from ipregistry import IpregistryClient client = IpregistryClient("tryout") ipInfo = client.lookup() print(ipInfo) 
0
source share

All Articles