PyQt5接入高德地图搜索API出现Request Failed提示

@TOCPyqt5调用高德API

PyQt5接入高德地图搜索功能API

Windows版本:10
Python版本:3.6.5
高德地图API版本:1.4.15

想做一个能够实现数据搜索定位功能的GUI 但是在接入高德地图API出现小问题

问题1:已安装PyQt5但是无法导入QtWebEngineView

网上查阅好像高版本Pyqt5不自带QtWebEngineView 需要自行安装
解决方案:pip install QtWebEngineWidgets

问题2:高德地图能显示地图,但是无法正确显示搜索结果,提示Invalid User key!

按照高德地图官网提供的获取搜索数据样例,使用QtWebEngineView的sethtml()方法写入,但是提示Invalid User key,于是又到高德官网查看,发现高德在API应用新加入密钥设定
解决方案:在html文档中增加代码段:

<script type="text/javascript">
        window._AMapSecurityConfig = {
            securityJsCode:'您申请的安全密钥',
        }
</script>
<script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=您申请的key值"></script> 

问题2

问题3:问题2解决了不合理的用户key,但用sethtml()方法引入html仍然有Request Failed错误

解决方案:将html代码保存为.html文件,改为使用load函数读取html路径,代替sethtml方法,问题解决,但是具体原因不太明白,还望路过的大佬指点迷津!
成功

最后附上代码段

# -*- coding: utf-8 -*-
'''
PyQt中高德地图API接入
'''
from PyQt5.QtWidgets  import QApplication , QWidget , QVBoxLayout 
from PyQt5.QtWebEngineWidgets import QWebEngineView
from PyQt5.QtCore import *
import sys
# 创建一个 application实例
app = QApplication(sys.argv)  
win = QWidget()
win.setWindowTitle('PyQt高德地图API接入')
# 创建一个垂直布局器
layout = QVBoxLayout()
win.setLayout(layout)
# 创建一个 QWebEngineView 对象
view = QWebEngineView()
view.load(QUrl(r"file:///C:/Users/**/Desktop/amap.html"))
# view.setHtml('''
# <!doctype html>
# <html>
# <head>
#   <meta charset="utf-8">
#   <meta http-equiv="X-UA-Compatible" content="IE=edge">
#   <meta name="viewport" content="initial-scale=1.0, user-scalable=no, width=device-width">
#   <link rel="stylesheet" href="https://a.amap.com/jsapi_demos/static/demo-center/css/demo-center.css" />
#   <link rel="stylesheet" type="text/css" href="https://a.amap.com/jsapi_demos/static/demo-center/css/prety-json.css">
#   <style>
#     html,
#     body,
#     #container {
#       width: 100%;
#       height: 100%;
#     }
#   </style>
#   <title>获取搜索信息</title>
# </head>
#
# <body>
#   <div id="container"></div>
#   <div class="info">
#     <h4>搜索结果展示</h4>
#     <p><span id="input-info"></span></p>
#   </div>
#   <script type="text/javascript">
#         window._AMapSecurityConfig = {
#             securityJsCode:'**********',
#         }
#   </script>
#   <script src="https://webapi.amap.com/maps?v=1.4.15&key=************&plugin=AMap.PlaceSearch"></script>
#   <script type="text/javascript" src="https://a.amap.com/jsapi_demos/static/demo-center/js/jquery-1.11.1.min.js" ></script>
#   <script type="text/javascript" src="https://a.amap.com/jsapi_demos/static/demo-center/js/underscore-min.js" ></script>
#   <script type="text/javascript" src="https://a.amap.com/jsapi_demos/static/demo-center/js/backbone-min.js" ></script>
#   <script type="text/javascript" src='https://a.amap.com/jsapi_demos/static/demo-center/js/prety-json.js'></script>
#   <script>
#     // 初始化地图
#     var map = new AMap.Map("container", {
#         resizeEnable: true
#     });
#
#     // 获取搜索信息
#     function autoInput(){
#       var keywords = '北京大学';
#       AMap.plugin('AMap.PlaceSearch', function(){
#         var autoOptions = {
#           city: '全国'
#         }
#         var placeSearch = new AMap.PlaceSearch(autoOptions);
#         placeSearch.search(keywords, function(status, result) {
#           // 搜索成功时,result即是对应的匹配数据
#           var node = new PrettyJSON.view.Node({
#               el: document.querySelector("#input-info"),
#               data: result
#           });
#         })
#       })
#     }
#
#     autoInput();
#   </script>
# </body>
#
# </html>
# ''')

layout.addWidget(view)

# 显示窗口和运行app
win.show()
sys.exit(app.exec_())

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值