Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Definitive "Find My Geolocation" Solution for Python

Tags:

python

django

So I've been poking around the web for awhile in search of a Python library that returns your exact current location and find that there is nothing available unless you go about the cheapish and inaccurate method of finding your location via your IP Address which I've tested on my mobile hotspot and it is off by 100 miles.

I have another solution to this but it will require mixing JavaScript with Python, and I need help, I've only been on Python/Django for a few days.

Since Python is server-side, I haven't been able to find any libraries that can pinpoint your precise longitude and latitude. However with client-side JavaScript, this is extremely easy and requires no libraries whatsoever:

http://jsfiddle.net/3bERp/1/

So I am wondering if there are any Python programmers out there with a little more experience under their belt that could explain here how you can run the javascript code above, and return the javascript output as a Python string:

    python
    >>>javascriptOutput = runJavaScript("<script> function showPosition(position) </script>")
    >>>print javascriptOutput
    <p id="long">-122.15563439999998</p> <p id="lat">37.4458668</p>
    >>>

So my idea here is to run the entire HTML block in the JSfiddle URL I provided in a single python function that'll return the same result the JSfiddle provides.

With that result I can store it in a python variable and do some string concatination to it to finally store my accurate client side longitude and latitude coordinates.

So is it possible to run some JavaScript in a Python function?

OR

Can you use Python/Django to somehow read the user's HTML source that the user sees after running the javascript/HTML5 geolocation?

The more I think about this now the more I'm starting to think that the Javascript is going to have to AJAX something over to the Django server...

like image 983
user1504605 Avatar asked Jan 18 '14 01:01

user1504605


1 Answers

There are 4 ways in my head right now. I don't know how to implement all of them. However, if you search online you moght find a way to implement them.

JavaScript Method (Client Side)

You call geolocation in javascript (like the one you implemented), then search a database with the nearest location to that coordinates.

Database of zipcodes

MySQL query based on location

Another MySQL Query: latitude/longitude find nearest latitude/longitude - complex sql or complex calculation

This, however, might be off a bit. You can send the information back to the server via ajax. Keep in your mind, the user might reject to share his/her location.

If you need a sample code, tell me and I will write one for you.

IP Address look up (Server Side)

Get IP address of that user, and lookup city of the remote user. This doesn't always give you a good results. Sometimes it's off by hundreds of miles.

You can use whois information to lookup the city of the user if you don't want to use a database.

Triangulating WiFi MAC address (Smart phone required)

If you are building a web service that will be a back end for Android or iPhone app, it's possible to use their (Google, or Apple) services to get the exact location of the user by using GPS or wireless information that's nearby the user. Android and iPhones upload MAC addresses of public WiFi, and their corresponding coordinates. This is very accured, but requires smartphone

It's possible to use the same method on a PC, but you will have to get a control of the wireless card, and scan nearby SSID, then query Google service with the MAC addresses (This is a paid service from Google)

Triangulating computer on the internet using network latency

There is a method that's patented by the NSA. I can give you a hint about how it works, I might be wrong about it.

simplfied version:

Have 3 servers within geographical location of the user. Then try to open a connection from each server and measure how fast would it take to connect to the client. After it, convert that time to distance by using the time the light cross per distance. Create a circle with radius of that distance, and triangulate user location from intersection of 3 circles.

You will need to calculate propgeation deley (time for router to process and send network packet once reciving it) from each router your TCP connection take, and subtract that from the number you get from the connection. Consider that the user might have a slow internet. I included a link for the patent

Link for the patent

like image 102
Ahmed Avatar answered Nov 11 '22 06:11

Ahmed