Python + Google Map JavaScript API:this.D.bind将Marker添加到Map时出错

时间:2018-03-01 13:50:12

标签: python google-maps marker

我需要你的帮助。 从Python IDLE控制台执行此代码,我有一个错误:

"TypeError: 'undefined' is not a function (evaluating 'this.D.bind(this)')"

并且标记在地图上不可见。

我在浏览器中执行html代码,但我没有错误。地图和标记是完美的。

我发现错误发生在marker.js文件中:http://maps.google.com/maps-api-v3/api/js/32/2/intl/es_419/marker.js

# -*- coding: utf-8 -*-
import sys

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import *
html= \
"""
<!DOCTYPE html>
<html>
  <head>
    <title>Simple Map</title>
    <meta name="viewport" content="initial-scale=1.0">
    <meta charset="utf-8">
    <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>
      var map;
      function initMap() {
        map = new google.maps.Map(document.getElementById('map'), {
          center: {lat: -34.397, lng: 150.644},
          zoom: 8
        });
      }
    </script>
    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyDfBy5lpFyQzq7pRpKEBeg7jaXH5hnHul8&callback=initMap"
    async defer></script>
  </body>
</html>
"""   
class Window(QWidget):
    def __init__(self):
        super(Window, self).__init__()
        self.view = QWebView(self)

        self.setupInspector()

        self.splitter = QSplitter(self)
        self.splitter.setOrientation(Qt.Vertical)

        layout = QVBoxLayout(self)
        layout.setMargin(0)
        layout.addWidget(self.splitter)

        self.splitter.addWidget(self.view)
        self.splitter.addWidget(self.webInspector)

    def setupInspector(self):
        page = self.view.page()
        page.settings().setAttribute(QWebSettings.DeveloperExtrasEnabled, True)
        self.webInspector = QWebInspector(self)
        self.webInspector.setPage(page)

        shortcut = QShortcut(self)
        shortcut.setKey(Qt.Key_F12)
        shortcut.activated.connect(self.toggleInspector)
        self.webInspector.setVisible(False)

    def toggleInspector(self):
        self.webInspector.setVisible(not self.webInspector.isVisible())

def main():
    app = QApplication(sys.argv)
    window = Window()
    window.show()
    window.view.setHtml(html)
    app.exec_()

if __name__ == "__main__":
    main()

1 个答案:

答案 0 :(得分:0)

我了解您的应用程序正在使用PyQt4中的Web浏览器控件。您在使用Google地图JavaScript API版本3.32时遇到问题,建议使用marker.js文件的路径。

版本3.32目前是Maps JavaScript API的实验版本,它于2018年2月13日推出。请注意,Google团队正在积极使用新版API中的EcmaScript 6新功能,这可能会破坏旧浏览器或者没有完全支持EcmaScript 6功能的网络浏览器控件。

您可以在此处查看支持的浏览器列表和嵌入控件:

https://developers.google.com/maps/documentation/javascript/browsersupport

不幸的是,PyQt4不在列表中,因此Google不保证API在此环境中正常运行。

作为一种解决方法,您可以尝试在

中添加v = 3参数

<script src="https://maps.googleapis.com/maps/api/js?v=3&key=YOUR_API_KEY&callback=initMap" async defer></script>

加载发布版本或v = 3.30加载冻结版本,但这可能只有3个月或6个月,因为Google每3个月推出一个新版本的API,如文档中所述:

https://developers.google.com/maps/documentation/javascript/versions

您可以考虑使用其他Web浏览器嵌入式控件。例如,您可以查看Chromium Embedded Framework。

https://en.wikipedia.org/wiki/Chromium_Embedded_Framework

我相信它应该可以很好地运作。根据文档,该框架嵌入了一个Chromium浏览器(与谷歌Chrome浏览器密切相关的开源Web浏览器),支持HTML5,EcmaScript6并使用V8 JavaScript引擎。

相关问题