使用 Google Maps API V3 和 QWebView 时,地图不响应鼠标点击

发布于 2024-11-10 18:09:20 字数 1357 浏览 6 评论 0原文

编辑4

一直以来我都在想这是标记的问题,它们没有被拖动! 现在我意识到,当地图显示在 Qt 小部件中时,鼠标单击在地图上不起作用。

下面的代码,我粘贴在一个 HTML 文件中并通过 Firefox 打开,效果完美!当我点击 QtWidget 上的地图时,同样没有响应:rolleyes:

任何人都可以为我确认这一点或告诉我我做错了什么吗?

Google 地图 JavaScript API 示例

<script type="text/javascript"> 

var map;    
var latlng            = new google.maps.LatLng (34.7607233, -117.0107599);

var directionsDisplay = new google.maps.DirectionsRenderer ();;
var directionsService = new google.maps.DirectionsService ();

var pointsArray       = new Array();
var arrayToBeReturned = new Array();

function initialize ()
{
    var myOptions = 
    {
        zoom:8,
        center:latlng,
        mapTypeId:google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map (document.getElementById ("map"), myOptions);
    directionsDisplay.setMap (map); 

    google.maps.event.addListener (map, "click", function ()
                        {
                            alert("You clicked the map.");
                        });
    }

</script> 
</head>
<body onload="initialize()" topmargin="0" leftmargin="0">
<div id="map" style="width: 341px; height: 271px"></div>
<div id="directionsPanel" style="float:right;width:30%;height 100%"></div>
</body>
</html> 

EDIT 4

All this while I was thinking that it is the markers problem, that they do not get dragged!
Now I have realized, that NO mouse click works on the map when it is displayed in Qt widget.

The following code, I pasted in an HTML file and opened through Firefox, this worked flawlessly! and the same doesn't respond when I click on the map on QtWidget :rolleyes:

Can anyone confirm this for me or tell me what wrong I am doing?

Google Maps JavaScript API Example

<script type="text/javascript"> 

var map;    
var latlng            = new google.maps.LatLng (34.7607233, -117.0107599);

var directionsDisplay = new google.maps.DirectionsRenderer ();;
var directionsService = new google.maps.DirectionsService ();

var pointsArray       = new Array();
var arrayToBeReturned = new Array();

function initialize ()
{
    var myOptions = 
    {
        zoom:8,
        center:latlng,
        mapTypeId:google.maps.MapTypeId.ROADMAP
    };

    map = new google.maps.Map (document.getElementById ("map"), myOptions);
    directionsDisplay.setMap (map); 

    google.maps.event.addListener (map, "click", function ()
                        {
                            alert("You clicked the map.");
                        });
    }

</script> 
</head>
<body onload="initialize()" topmargin="0" leftmargin="0">
<div id="map" style="width: 341px; height: 271px"></div>
<div id="directionsPanel" style="float:right;width:30%;height 100%"></div>
</body>
</html> 

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

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

发布评论

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

评论(3

傲性难收 2024-11-17 18:09:20

您正在查看事件的错误位置:) 在引用中,每个对象都有自己的事件。您可以在此处查看标记:

[http://code.google.com/apis/maps/documentation/javascript/reference.html#Marker][1]

我已经多次使用了 Dragend 事件,并且工作顺利。

执行此操作的代码可能是:

    var map = new google.maps.Map(document.getElementById("map"), myOptions);

    var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        draggable: true
    });

    google.maps.event.addListener(marker, 'dragend', function () {
        document.getElementById("txtLatitude").value = marker.getPosition().lat();
        document.getElementById("txtLongitude").value = marker.getPosition().lng();
    });

[1]: http:// /code.google.com/apis/maps/documentation/javascript/reference.html#Markermap = 新

You are looking at the wrong place for the events :) In the references each object have their own events. You can see the ones for markers here:

[http://code.google.com/apis/maps/documentation/javascript/reference.html#Marker][1]

I have used the dragend event plenty of times and it works smoothly.

The code to do it could be:

    var map = new google.maps.Map(document.getElementById("map"), myOptions);

    var marker = new google.maps.Marker({
        position: latlng,
        map: map,
        draggable: true
    });

    google.maps.event.addListener(marker, 'dragend', function () {
        document.getElementById("txtLatitude").value = marker.getPosition().lat();
        document.getElementById("txtLongitude").value = marker.getPosition().lng();
    });

[1]: http://code.google.com/apis/maps/documentation/javascript/reference.html#Markermap = new

固执像三岁 2024-11-17 18:09:20

找到解决方案:

在源文件中添加此类,您将在其中“加载”Qt 小部件中的 HTML 文件(包含 Javascript API)。

class myWebPage : public QWebPage
{
    virtual QString userAgentForUrl(const QUrl& url) const {
        return "Chrome/1.0";
    }
};

然后在同一源文件中您自己的类中,添加 setPage 函数调用,如下所示,看看奇迹发生了!

MainScreen::MainScreen(QWidget *parent):QWidget(parent)
{
        ...
    ***map->setPage (new myWebPage());***
    map->load (QUrl("./index.html") ) ;
};

Found the solution:

Add this class in the source file where you are "loading" the HTML file (containing Javascript API) in the Qt widget.

class myWebPage : public QWebPage
{
    virtual QString userAgentForUrl(const QUrl& url) const {
        return "Chrome/1.0";
    }
};

Then in your own class in the same source file, add the setPage function call as shown below and see the magic happen!

MainScreen::MainScreen(QWidget *parent):QWidget(parent)
{
        ...
    ***map->setPage (new myWebPage());***
    map->load (QUrl("./index.html") ) ;
};
葬シ愛 2024-11-17 18:09:20
google.maps.event.addListener (initialPointMarker, 'click', 
                                  function () { map.closeInfoWindow(); });

点击争论周围的单引号怎么样

google.maps.event.addListener (initialPointMarker, 'click', 
                                  function () { map.closeInfoWindow(); });

How about single quotes around the click arguement

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