Access android GPS Location using Python

Abhishek Mishra
4 min readMay 20, 2021

In my previous technical story, we learned about how we access the android camera using Python using OpenCV. If you missed them, please read them because that is very important in this story. The link is here.

This tutorial is on how to access Android GPS (Global Positioning System) without using any sensor, and send this location via WhatsApp and SMS messages to a particular number.

This is helpful in accident-controlling systems, alert systems, notification systems, and real-time object positioning systems. I’ll use Python 3.6 on a window, but don’t worry about other OS, the process will be the same too. Without waiting any longer, let’s begin the tutorial.

Before you get started, install the following libraries:

  • OpenCV-contrib-python
  • NumPy
  • PIL
  • Pytesseract
  • Webbrowser
  • Requests
  • Selenium

To install these libraries using pip, just type the following command on the command prompt (cmd):

pip install pillow
pip install numpy
pip install opencv-contrib-python
pip install pytesseract
pip install webbrowser
pip install requests
pip install selenium

The next step is to open the IP Webcam app on your cell phone. This app will be used to communicate between your Android phone and the computer system using an URL. After that, give the GPS permission to this application. Run the app on your phone and click on “Start server”.

After this configuration, the camera on your Android device will open with an IP address at the bottom. Like this:-

Use any one of two links that are given with an IP address. The link may differ from network to network. Please use the same which is pop on the IP Webcam screen.

Now it turns to start coding to access the GPS location using python:-

import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import webbrowser
import requests
from PIL import Image
from pytesseract import pytesseract
options = webdriver.ChromeOptions()
options.headless = True
driver = webdriver.Chrome(options=options)
URL = 'http://192.168.43.1:8080/sensors.html'
driver.get(URL)
time.sleep(3)
S=lambda X:driver.execute_script('return document.body.parentNode.scroll'+X)
driver.set_window_size(S('Width'),S('Height'))
# May need manual adjustment
driver.find_element_by_tag_name('body').screenshot('web_screenshot1.png')
driver.quit()
# Defining paths to tesseract.exe
# and the image we would be using
path_to_tesseract = r"C:\Program Files\Tesseract-OCR\tesseract.exe"
image_path = r"web_screenshot1.png"
# Opening the image & storing it in an image object
img = Image.open(image_path)
# Providing the tesseract
# executable location to pytesseract library
pytesseract.tesseract_cmd = path_to_tesseract
# Passing the image object to image_to_string() function
# This function will extract the text from the image
text = pytesseract.image_to_string(img)
#print(text)
str1=[]
akmstr=[]
if("Get location" in text[:-1]):
akm=text.index("Get location")
for i in (text[(akm):]):
if(i.isdigit() or i=="."):
str1.append(i)
for i in range(len(str1)):
if(str1[i]=="."):
akmstr.append(i)
#print(akmstr)
url1=str(str1[akmstr[0]-2])+str(str1[akmstr[0]-1])+'.'+str("".join(str1[(akmstr[0]+1):(akmstr[0]+6)]))
url2=str(str1[akmstr[1]-2])+str(str1[akmstr[1]-1])+'.'+str("".join(str1[(akmstr[1]+1):(akmstr[1]+6)]))
url=(f"https://www.google.com/maps/search/?api=1&query={url1},{url2}")
print(url)

Let us understand the code:-

The main concept of this code is to localize the GPS without using external sensors (like GPS and GSM sensors). This method provides mobile localization and greater precision. So first we open the URL on our system, that is “http://192.168.43.1:8080/sensors.html” (maybe another one in your case), which looks like this:

Notice that “Get location” is checked, but if it is not, then do it. Once it is checked, then wait for some time to load the accurate location of your phone. Make sure that the GPS permission is given and set for high accuracy on the phone.

After some time, it will show some values of latitude and longitude like:

Once you got the values, then there are two methods by which you can extract these values. One is “web scraping” and another has extracted the data with the help of a Python library named “pytesseract”.

Here I’m using the second one. So for that, take a screenshot of that web page with the help of the selenium library, and after that, extract the data from that particular screenshot.

So, pytesseract extracts the data and converts it into a string format. Extracting the data may be different because pytesseract works with the probabilities so there is a need to create a checkpoint. The given code is used for creating a checkpoint.

if("Get location" in text[:-1]):
akm=text.index("Get location")
for i in (text[(akm):]):
if(i.isdigit() or i=="."):
str1.append(i)
for i in range(len(str1)):
if(str1[i]=="."):
akmstr.append(i)

The full code of this tutorial is at the given link. That is “https://github.com/Abhishekmishra-17/Mobile-GPS-Location-using-Python

This is essentially how to access the GPS location of phones to your Python application. The next steps are in your hands. This is used in real-time location sharing, accident control systems, and alerts systems.

--

--

Abhishek Mishra

System Engineer-TCS | COMPUTER SCIENCE ENGINEER | I love Python 🐍🐍😀