0

I am using PyQt5 to create a small app. At some point I need to let the user select latitude and longitude from a map. After some research, the easiest way to implement a map search seems to be using google maps through some Javascript lines. I found this code that does pretty much what I need:

https://gis.stackexchange.com/questions/33238/how-do-you-get-the-coordinates-from-a-click-or-drag-event-in-the-google-maps-api/33246#33246?newreg=eeb7c8c7ce344710838fba3d7b111d12

However, I need to use in my python function the coordinates selected by the user. How can I pass to python the latitude and longitude selected in the Javaascript code ("latLng")? I read a bunch of similar questions on many forums but I can't figure out how to solve my problem. I would expect to find a very simple solution. My code is something like:

def latlong_function():

        html='''
 <html>  <head> ....  ....
'''

print(coordinates)

return html
CDspace
  • 2,639
  • 18
  • 30
  • 36
Carlo Bianchi
  • 115
  • 4
  • 15
  • I would suggest sending an AJAX request from Javascript to your Python code. Then the python resource can return the values you need. – JacobIRR Jul 19 '17 at 23:40
  • Quite simple, really: just load google.maps into a [QWebEngineView](https://doc.qt.io/qt-5/qwebengineview.html), then use [runJavaScript](https://doc.qt.io/qt-5/qwebenginepage.html#runJavaScript-2) with a callback to get the values. – ekhumoro Jul 20 '17 at 17:45
  • That's very close to what I am doing, but how do I get a callback? Do you have any example code to post? – Carlo Bianchi Jul 20 '17 at 17:48
  • @CarloBianchi. Just [pass in a python function with one argument](https://en.wikipedia.org/wiki/Callback_(computer_programming)#Python), which can then receive the return value of the javascript function. If the return value is a complex object, you could stringify it using json. – ekhumoro Jul 21 '17 at 17:52
  • Well, I know what a callback is, but how do I "receive the return value of the javascript function". I mean, the question is: how can I pass info from Javascript to Python? At this point the AJAX option or similar seem the only one. – Carlo Bianchi Jul 21 '17 at 23:45

1 Answers1

0

I am not an expert when it comes to doing python backends for web apps, but I do have experience with PHP and perl in a web environment.

Unless python has a specific utility/library for doing this (and based on your research it sounds like it doesn't) your best bet is to have your JavaScript submit the data via an HTTP POST request using something like Ajax and JQuery.

Something like this might work:

// Function called when an HTML button with id of "Search_Button" is clicked
$('#Search_Button').click(function () {

   // Get the values of your lat/long HTML inputs
   var lat = $('#Latitude_Input').val();
   var long = $('#Longitude_Input').val();

   // Submit the AJAX request
   $.ajax({
     url: "path/to/your_script.py", // Path of the python script to do the processing
     method: "POST", // Method to submit the HTTP request as, usually either POST or GET
     data: { // These are the POST parameters passed to the backend script
      latitude: lat,
      longitude: long 
     },
     success: function (returned_data) { // Function to run if successful, with the function parameter being the output of the url script
       alert('Here is the output: ' + returned_data);
     },
     error: function () { // Function to run if unsuccessful
       alert('An error occured');
     }
   });
});

This should let you take the data from the HTML page and let your python script process it.

Check out this answer for handling POST/GET requests in python.

enpaul
  • 246
  • 4
  • 13