使用 QtWebKit 在桌面应用程序中检索谷歌地图 v3 标记到 Qt 的位置

发布于 2024-09-02 21:48:40 字数 2050 浏览 8 评论 0 原文

我正在使用 Python 构建一个 Qt 应用程序,您可以在其中指向并单击(谷歌)地图并获取位置的坐标。地图通过加载简单 HTML 页面的 QWebView 显示,用户可以通过单击创建标记。单击地图后小部件的屏幕截图

但是,我无法将刚刚单击的位置坐标检索回 Qt(以便我可以将它们用作变量 - 例如,在左上角的 QLineEdit 中显示它们)上面的角,作为标记的当前位置)。

这是 HTML 文件的相关部分:

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

我一直在尝试使用 evaluateJavaScript,但无法检索坐标。我尝试创建一个函数来使用 marker.getPosition() 访问该位置,但没有成功。不过下面的虚拟可以工作。

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

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

关于如何将坐标返回到 Qt 有什么建议吗?

编辑:

这是对我有用的代码:

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]

https://github.com/nelas/veliger/blob/master/veliger.py#L2374

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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

岛歌少女 2024-09-09 21:48:40

我找到了一种解决方法来使其发挥作用,但我不太确定这是否是正确的方法。无论如何,这就是我所做的:

  1. 在 html 文档的正文部分创建一个隐藏输入以保存标记的位置数据:

    <前><代码><正文>
    (...)
    <输入 id="locationData" type="隐藏">
    (...)

  2. 在 javascript 代码中,每次标记的位置都保存在隐藏输入中创建:

    函数 placeMarker(位置) {
        (...)
        document.getElementById("locationData").value = benchmark.position;
        (...)
    }
    
  3. 在 Qt 代码中,使用指令读取隐藏输入的值:

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

希望有帮助!

来源:http://opendocs.net/qt/4.6/webkit-formextractor.html< /a>

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文