开发者

Retrieve position of a google maps v3 marker to Qt in a desktop app with QtWebKit

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().evaluateJ开发者_StackOverflow社区avaScript(QString('dummyTxt()'))

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

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

Edit:

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 in https://github.com/nelas/veliger/blob/master/veliger.py#L2374


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜