【发布时间】:2018-01-15 11:57:51
【问题描述】:
我一直在试验 googlemaps javascript API。
我的目标是在地图上绘制折线。
我找到了以下教程-> https://developers.google.com/maps/documentation/javascript/examples/polyline-simple
当我在网络浏览器中实现它时,它可以工作,但是,尝试在 QT QWebView 小部件中做同样的事情并没有奏效。地图显示正常,但没有绘制折线。
我想知道您是否必须登录到您的 google 帐户才能使您的 googlemaps API 密钥被视为有效?即它想将 API 与用户配对?
这可能是浏览器工作但 QWebView 不工作的原因吗?
在python方面:
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
def __init__(self, parent=None):
super(g3ui, self).__init__(parent)
self.setupUi(self)
self.initMap()
...
def initMap(self):
self.webView.settings().setAttribute(QWebSettings.JavascriptEnabled, True)
self.webView.load(QUrl('polyline.html'))
在qt设计器中创建的webView,相关sn-p:
class Ui_MainWindow(object):
def setupUi(self, MainWindow):
...
self.webView = QtWebKit.QWebView(self.tab_10)
self.webView.setGeometry(QtCore.QRect(30, 20, 1121, 401))
self.webView.setUrl(QtCore.QUrl(_fromUtf8("about:blank")))
self.webView.setObjectName(_fromUtf8("webView"))
在html端(polyline.html):
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no">
<meta charset="utf-8">
<title>Simple Polylines</title>
<style>
/* Always set the map height explicitly to define the size of the div
* element that contains the map. */
#map {
height: 100%;
}
/* Optional: Makes the sample page fill the window. */
html, body {
height: 100%;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
// This example creates a 2-pixel-wide red polyline showing the path of
// the first trans-Pacific flight between Oakland, CA, and Brisbane,
// Australia which was made by Charles Kingsford Smith.
function initMap() {
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 3,
<!-- zoom: 10, -->
<!-- center: {lat: 50.8548, lng: 1.1866}, -->
center: {lat: 0, lng: -180},
mapTypeId: 'terrain'
});
var flightPlanCoordinates = [
{lat: 37.772, lng: -122.214},
{lat: 21.291, lng: -157.821},
{lat: -18.142, lng: 178.431},
{lat: -27.467, lng: 153.027}
];
var flightPath = new google.maps.Polyline({
path: flightPlanCoordinates,
geodesic: true,
strokeColor: '#FF0000',
strokeOpacity: 1.0,
strokeWeight: 2
});
flightPath.setMap(map);
}
</script>
<script async defer
src="https://maps.googleapis.com/maps/api/js?key=AIzaSyA6Hzn6vaW5MOn-rh9c4kenP8jBpL7rzbQ&callback=initMap">
</script>
</body>
</html>
这是取自 googlemaps javascript API 教程网站的简单折线教程。
更新以响应 eyllanesc 答案中的代码:
这是我在运行简单的 main.py 代码时看到的:
【问题讨论】:
-
显示您的代码。 Qt4 还是 Qt5?
-
我已经尝试了html中的链接代码,它可以工作,但是你显示的html只画了一个标记,你没有画折线
-
在你的测试中,地图绘制了吗?
-
您的密钥有效吗?
-
试试我的答案
标签: javascript qt google-maps pyqt4 pyqt5