uni-app map地图组件报错
uni-app map地图组件报错
| 开发环境 | 版本号 | 项目创建方式 |
|---|---|---|
| Windows | 19043.1237 | HBuilderX |
### 操作步骤:
```javascript
<map style="width: 100%; height: 260px;" scale="17" :latitude="act.latitude" :longitude="act.longitude" :markers="markers"></map>
that.markers = [{
latitude: list.act.latitude,
longitude: list.act.longitude,
iconPath:'../../static/img/btn.png',
callout:{
content:list.act.biz_name,
borderRadius:20,
display:'ALWAYS',
padding:10,
}
}];
预期结果:
不报错正常显示
实际结果:
报错
bug描述:
设置iconPath后报错 Uncaught TypeError: this.setStyle is not a function 不设置iconPath后报错并且不显示默认标记点 Error in event handler for “mapready”: “TypeError: Cannot read property ‘indexOf’ of undefined” HBuilder切换回 3.2.3.20210825后正常
更多关于uni-app map地图组件报错的实战教程也可以访问 https://www.itying.com/category-93-b0.html
3 回复
测试 HBuilderX-alpha 版本出现同样错误
更多关于uni-app map地图组件报错的实战教程也可以访问 https://www.itying.com/category-93-b0.html
根据你提供的信息,这是一个在特定HBuilderX版本中出现的兼容性问题。核心问题在于iconPath路径处理异常,触发了地图组件的内部错误。
原因分析:
- 主要问题:当
iconPath设置为相对路径('../../static/img/btn.png')时,在HBuilderX 3.2.3.20210825之后的某个版本中,地图组件底层可能未能正确解析此路径,导致调用setStyle方法失败(Uncaught TypeError: this.setStyle is not a function)。 - 连带问题:当你注释掉
iconPath后,地图组件尝试使用默认标记点图标,但默认图标的路径变量可能为undefined,进而导致indexOf方法调用错误(Cannot read property 'indexOf' of undefined)。
解决方案:
-
临时方案(推荐):如你所述,暂时切换回可正常工作的HBuilderX版本(3.2.3.20210825)。这是最直接的解决方式。
-
路径处理方案:尝试对
iconPath使用绝对路径。在uni-app中,静态资源建议放在static目录下,并使用绝对路径/static/进行引用。将你的代码修改为:iconPath: '/static/img/btn.png'

