由于uniapp中要使用地图,虽然uni-app有地图组件map,但是很难用,而且性能很差。在app中是不能操作dom,所以直接用leaflet是不可能的。最终发现了renderjs,官网提出,在app-vue环境下,视图层由webview渲染,而renderjs运行在视图层,自然可以操作dom和window。

官网renderjs的说明链接

使用注意事项:

:prop 传值 :change:prop 监听prop改变

调用改变的方法内有四个参数

newValue:跟新后的数据

oldValue:原先的数据

ownerInstance:通过该函数可以调用内部函数,可以传输数据

instance:当前service层实例

通过 this.$ownerInstance.$vm 获取当前组件的 ComponentDescriptor 实例

完整示例代码:

<template>
<view class="leafletMap">
<view
class="mapBox"
id="mapId"
:prop="psArr"
:change:prop="leaflet.updatePsArr"
></view>
</view>
</template> <script>
export default {
data() {
return {
psArr: [], //企业数组
};
},
onLoad() {
this.$nextTick(() => {
this.queryPsGis();
});
},
methods: {
//请求企业gis数据
async queryPsGis() {
this.psArr = []; //重置企业的站点信息
const { data = {} } = await this.$http(
'/smoke/smokeData/getPsRealityDataGis',
'get'
);
if (data.code !== 200) {
return uni.showToast({
title: '请求数据异常',
icon: 'error',
mask: true,
});
}
if (data.result.length > 0) {
this.psArr = data.result.filter(
(item) =>
item.pscode !== null &&
item.pscode !== undefined &&
item.pscode !== ''
); //排除第一个无意义内容
}
},
//renderjs 传递给视图层
getMessage(option) {
uni.showToast({
title: option.text,
icon: 'success',
mask: true,
});
},
},
};
</script>
<script module="leaflet" lang="renderjs">
export default {
data() {
return {
map: null, //地图容器
centerpoint: [37.6211, 114.9304676], //默认中心位置
zoomlevel: 14, //初始化放大倍数
baseLayer: null, //矢量底图
markers:null,
ownerInstance:null,//接收视图层dom
}
},
mounted() {
// 动态引入较大类库避免影响页面展示
const link = document.createElement('link');
link.rel = "stylesheet"
link.href = "https://unpkg.com/leaflet@1.9.3/dist/leaflet.css";
document.head.appendChild(link)
const script = document.createElement('script')
script.src = "https://unpkg.com/leaflet@1.9.3/dist/leaflet.js"
script.type = "text/javascript"
script.onload = this.initMap.bind(this)
document.head.appendChild(script)
},
methods: {
initMap() {
this.map = L.map('mapId', {
minZoom: 5,
maxZoom: 18,
crs: L.CRS.EPSG3857,
center: this.centerpoint,
zoom: this.zoomlevel,
fullscreenControl: false,
zoomControl: false,
attributionControl: false,
})
//添加基础图层
this.baseLayer = L.tileLayer(
'http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineCommunity/MapServer/tile/{z}/{y}/{x}', {
minZoom: 5,
maxZoom: 18,
pane: 'overlayPane',
}
)
this.map.addLayer(this.baseLayer)
},
//属性psArr变化监控
updatePsArr(newValue, oldValue, ownerInstance, instance) {
if(newValue.length>0){
this.ownerInstance = ownerInstance
if(this.markers){
this.map.removeLayer(this.markers)
this.markers = null
}
this.addMarkerCluster(newValue)
}
},
//处理整合geoJSON所需要的marker数据
handlePsGeoJson(arr) {
let coorsField = {
type: 'FeatureCollection',
features: [],
}
arr.forEach((item) => {
let lon = item.lon
let lat = item.lat
if (lon && lat) {
coorsField.features.push({
type: 'Feature',
properties: {},
geometry: {
type: 'Point', // 配合 pointToLayer 一起使用
coordinates: [lon, lat],
},
})
}
})
return coorsField
},
//添加marker标记
addMarkerCluster(arr) {
// 添加站点marker标记
this.markers = L.geoJSON(this.handlePsGeoJson(arr), {
pointToLayer: (feature, latlng) => {
return L.marker(latlng, {
icon: this.getMarkerIcon(),
zIndexOffset: 1000
}) // 添加标记
},
})
this.map.fitBounds(this.markers.getBounds())
this.markers.addTo(this.map)
//renderjs传递给视图层
// this.ownerInstance.callMethod('getMessage', {
// text: '成功'
// })
},
getMarkerIcon() {
let htmlContent = '<div style="width:24px;height:24px;border-radius:50%;background-color:#5ed323"></div>'
let icon = L.divIcon({
html: htmlContent,
className: 'ss',
iconAnchor: [13, 4],
})
return icon } }
}
</script> <style lang="scss" scoped>
.leafletMap {
width: 100%;
height: 100%; .mapBox {
box-sizing: border-box;
width: 100%;
height: 100vh;
background-color: #042046;
overflow: hidden;
}
}
</style>

uniapp中利用renderjs引入leaflet的更多相关文章

  1. karaf中利用Bundle引入外部log4j配置文件

    环境准备: 1.在karaf_home下新建 config及logs目录 2.将mylog4j.properties拷贝到config文件夹下 查看log4j-1.2.17.jar/MANIFEST. ...

  2. uniapp中利用uni.$emit()和uni.$on()进行页面和tabbar页面传值(页面通讯)

    tabbar页面 <script> export default { data() { return { list: [] , }; }, onLoad() { // 监听事件 uni.$ ...

  3. uni-app 中实现 onLaunch 异步回调后执行 onLoad 最佳实践

    前言 好久没写博客了,由于公司业务需要,最近接触uiapp比较多,一直想着输出一些相关的文章.正好最近时间富余,有机会来一波输出了. 问题描述 在使用 uni-app 开发项目时,会遇到需要在 onL ...

  4. uniapp中引入less文件

    uniapp入门遇到的问题记录 在uniapp中从外部import less文件的话,首先需要在 工具>插件安装 中安装支持less语法的插件,然后在.vue文件中引入  @import url ...

  5. [C# 基础知识系列]专题一:深入解析委托——C#中为什么要引入委托

    转自http://www.cnblogs.com/zhili/archive/2012/10/22/Delegate.html 引言: 对于一些刚接触C# 不久的朋友可能会对C#中一些基本特性理解的不 ...

  6. VUE2 项目 引入 leaflet.draw全过程

    leaflet.draw的参考文档:http://leaflet.github.io/Leaflet.draw/docs/leaflet-draw-latest.html   这个网址不稳定,多刷新几 ...

  7. 在Eclipse中利用maven整合搭建ssm框架

    首先说明用到的框架: spring  +  springMVC  +  mybatis 构建工具:maven 开发工具:eclipse 开发环境:win10      java版本:jdk1.8    ...

  8. C#深入解析委托——C#中为什么要引入委托

    引言: 对于一些刚接触C# 不久的朋友可能会对C#中一些基本特性理解的不是很深,然而这些知识也是面试时面试官经常会问到的问题,所以我觉得有必要和一些接触C#不久的朋友分享下关于C#基础知识的文章,所以 ...

  9. vue中的js引入图片,必须require进来

    需求:如何components里面的index.vue怎样能把assets里面的图片拿出来. 1.在img标签里面直接写上路径: <img src="../assets/a1.png& ...

  10. 二叉树系列 - 二叉搜索树 - [LeetCode] 中序遍历中利用 pre节点避免额外空间。题:Recover Binary Search Tree,Validate Binary Search Tree

    二叉搜索树是常用的概念,它的定义如下: The left subtree of a node contains only nodes with keys less than the node's ke ...

随机推荐

  1. 国产Linux发行版 Deepin 评价与主观展望 我与Deepin的故事

    全文皆属于up自己的主观臆断,有不到之处可以进行批评,共同成长. P1 编辑时间(2021-2-20) 昨天,我刷b站的时候见到一则旧闻,关于我国操作系统的.原本只是想写个1000字左右的评论的,没想 ...

  2. 初学STM32 SDIO (二)

    ​ SD命令由主机发出,以广播命令和寻址命令为例,广播命令是针对与SD主机总线连接的所有从设备发送的,寻址命令是指定某个地址设备进行命令传输. 1.SD命令格式 SD命令格式固定为48bit,都是通过 ...

  3. 探索生成式AI的未来:Chat与Agent的较量与融合

    近年来,生成式人工智能(AI)不仅在技术界引起了广泛关注,更成为了推动多个行业革新的关键力量.这种技术之所以备受瞩目,不仅在于其独特的创造性和高效性,还在于它对未来商业模式和社会结构可能产生的深远影响 ...

  4. IE8页面失去焦点,动态删除element

    当页面失去焦点(切换到其他标签页 / 切换到其他软件),触发失焦事件,然后动态删除element 兼容IE8 if(window.ActiveXObject){ window.attachEvent( ...

  5. 第二十一篇:信号、缓存、中间件、Form操作

    一.CSRF 二.中间件 三.缓存 四.信号 五.Form操作

  6. spring cloud 学习笔记 基础工程的构建(一)

    前言 学习一下spring cloud,只是过一遍微服务的一些现代化工具,微服务其实一直都存在,去公司的时候发现一个问题,即使有些项目没有用到现代这些什么docker.k8s,其实也是微服务,微服务一 ...

  7. Node.js 中的事件循环机制

    一.是什么 在浏览器事件循环中,我们了解到javascript在浏览器中的事件循环机制,其是根据HTML5定义的规范来实现 而在NodeJS中,事件循环是基于libuv实现,libuv是一个多平台的专 ...

  8. 鸿蒙HarmonyOS实战-ArkUI组件(Canvas)

    一.Canvas Canvas组件是一种图形渲染组件,它提供了一个画布(canvas),开发者可以在上面绘制各种图形.文本等.Canvas组件通常用于创建游戏.数据可视化等需要动态绘制图形的应用程序. ...

  9. 【GDKOI 2024 TG Day2】不休陀螺(top) 题解

    考虑一个卡牌区间怎样才不是"陀螺无限". 一个是费用在打到一半时费用就不够了.考虑构造一个卡牌序列使其尽量能够在打到一半时费用就不够,如何构造呢? 把 \(a_i > b_i ...

  10. RocketMQ 之 IoT 消息解析:物联网需要什么样的消息技术?

    前言: 从初代开源消息队列崛起,到 PC 互联网.移动互联网爆发式发展,再到如今 IoT.云计算.云原生引领了新的技术趋势,消息中间件的发展已经走过了 30 多个年头. 目前,消息中间件在国内许多行业 ...