views:

493

answers:

2

I'm building a Qt app with Python where you can point and click at a (google) map and get the coordinates of the location. The map is shown through a QWebView loading a simple HTML page and the user can create markers by clicking. Screenshot of the widget after clicking on the map.

However, I'm having trouble to retrieve the just-clicked location coordinates back to Qt (so that I can use them as variables -- and, for example, show them in the QLineEdits on the topleft corner above, as current location of the marker).

This is the relevant part of the HTML file:

<script type="text/javascript">
var map;
function initialize() { 
    var local = new google.maps.LatLng(-23.4,-40.3);
    var myOptions = {
        zoom: 5,
        center: local,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    } 
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);

    google.maps.event.addListener(map, 'rightclick', function(event) {
        placeMarker(event.latLng);
    });
}

function placeMarker(location) {
    var clickedLocation = new google.maps.LatLng(location);
    var marker = new google.maps.Marker({
        position: location,
        map: map
    });

    map.setCenter(location);
}

function dummyTxt() {
    return 'This works.';
}
</script>

I've been trying with evaluateJavaScript, but was not able to retrieve the coordinates. I tried to created a function to access the position with marker.getPosition(), but with no luck. The dummy below works though..

newplace = QWebView.page().mainFrame().evaluateJavaScript(QString('dummyTxt()'))

>>> print newplace.toString()
This works.

Any suggestions on how to get the coordinates back to Qt?

--

Here is the code that worked for me:

def update_geo(self):
    # Capture coordinates of the last marker on the map.
    mark = self.map.page().mainFrame().evaluateJavaScript('document.getElementById("markerlocation").value').toString()
    # Convert string to list of floats, stripping parentheses.
    marker = str(mark).strip('()').split(', ')
    decimals = [float(c) for c in marker]

Full source is in veliger.py at http://github.com/nelas/veliger

A: 

Hi, I found a work around to make it work but I'm not pretty sure that it will be the right approach. Anyway, this is what I did:

  1. Create a hidden input in the body section of your html document to save the position data of the marker:

    <body>
        (...)
        <input id="locationData" type="hidden">
        (...)
    </body>
    
  2. In the javascript code, save the position of the marker in the hidden input every time it's created:

    function placeMarker(location) {
        (...)
        document.getElementById("locationData").value = marker.position;
        (...)
    }
    
  3. In your Qt code, read the value of the hidden input with the instruction:

    webView->page()->mainFrame()->findFirstElement("#locationData").evaluateJavaScript("this.value").toString();
    

I hope it helps!

Source: http://opendocs.net/qt/4.6/webkit-formextractor.html

Mkfnx
Thank you, Mkfnx. It helped indeed! I only changed the Qtcode to find the element directly via js: webview.page().mainFrame().evaluateJavaScript('document.getElementById("markerlocation").value').toString()
nelas
I hadn't noticed the javascript alternative. I'm glad to see it helped!
Mkfnx
A: 

Hi nelas,

i wonder if you could provide anywhere the complete source code of your solution?

thanks in advance,

Lukas

bibo
Hi Lukas, a simplified version:def update_geo(self): # Capture coordinates of the last marker. mark = self.map.page().mainFrame().evaluateJavaScript('document.getElementById("markerlocation").value').toString() # String to list of floats. marker = str(mark).strip('()').split(', ') decimals = [float(c) for c in marker]The complete source is at veliger.py at http://github.com/nelas/veliger
nelas
that worked for me, thanks a lot!
bibo