qq_41981438 2024-06-26 10:20 采纳率: 62.5%
浏览 69

腾讯地图Geolocation报错

今天打开一个老项目后,腾讯地图的定位服务在

let geolocation = new window.qq.maps.Geolocation('USER-KEY', 'APP-NAME')

后报错
Cannot read properties of null (reading 'id')
at e.receive (gljs?v=1.exp&libraries=service&key=USER-KEY:40:90870),
Cannot read properties of null (reading 'key')
at detector.js:1:84,
Cannot read properties of null (reading 'type')
at f (content.js:2:1002707)

img


但是后面的getLocation有正常的返回
请问是什么原因(key和id做了隐藏,代码里没问题)


let geolocation = new window.qq.maps.Geolocation('USER-KEY', 'APP-NAME')
        await new Promise(function (resolve, reject) {
          geolocation.getLocation(showPosition, showErr)
          function showPosition(position) {
            resolve(position)
          }
          function showErr(error) {
            reject(error)
          }
        })
          .then(res => {
            console.log('getLocationSuccess', res)
            this.userPosition = res
          })
          .catch(err => {
            console.log('getLocationError', err)
            this.userPosition = undefined
          })
  • 写回答

4条回答 默认 最新

  • 专家-赤兔[在线] 优质创作者: 编程框架技术领域 2024-06-26 10:20
    关注

    引自免费微信小程序:皆我百晓生

    这个问题是由于window.qq.maps.Geolocation对象在创建时被赋值为null导致的。这可能是因为你在创建这个对象之前没有初始化它。

    要解决这个问题,你需要确保在调用new操作符时,首先将window.qq.maps.Geolocation对象设置为非空。

    let geolocation = new window.qq.maps.Geolocation('USER-KEY', 'APP-NAME');
    if (geolocation === null) {
      geolocation = new window.qq.maps.Geolocation('USER-KEY', 'APP-NAME');
    }
    

    这样就可以避免TypeError错误了。

    评论 编辑记录

报告相同问题?

问题事件

  • 修改了问题 6月26日
  • 创建了问题 6月26日