9

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...

7
  • 1
    Excellent question and I'm just sorry no one answered you. Were you able to figure anything out that you could share? Commented Apr 29, 2014 at 9:03
  • Just to clarify: on what machine is Python running, and is that the same machine for which you want the geolocation? Commented Jun 1, 2015 at 17:00
  • Can you access the navigator.geolocation without using a browser? Commented Jun 1, 2015 at 17:37
  • Also jsfiddle puts my location as being 100+ miles away from where I actually am Commented Jun 1, 2015 at 18:45
  • I dont usually ask 'why?', as people have all sorts of reasons for wanting code, but in this case I am lost a bit. Surely you want the client side lat/long, which your snippet gives you. Why invoke python at all? You can very easily store the current clients lat/long in the DB if you wanted, just using a tiny bit of Jquery.... Commented Jun 1, 2015 at 20:42

1 Answer 1

2
+50

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

1
  • This is useful stuff, but doesn't quite answer OP's question, of performing the JS method, from Python. However if no better answers appear within the bounty period, I will aware you with the bounty just for shear useful information. Cheers!
    – Aphire
    Commented Jun 2, 2015 at 8:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.