由于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. 10 JavaScrit定时器

    10 JavaScrit定时器 在JS中, 有两种设置定时器的方案: // 语法规则 t = setTimeout(函数, 时间) // 经过xxx时间后, 执行xxx函数 // 5秒后打印我爱你 t ...

  2. #floyd,斜率#洛谷 1354 房间最短路问题

    题目 在一个长宽均为10,入口出口分别为(0,5),(10,5)的房间里有几堵墙, 每堵墙上有两个缺口,求入口到出口的最短路径. 分析 暴力建图,判断两个点是否有墙堵住, 可以求斜率然后判断所交点不能 ...

  3. PriorityQueue和PriorityBlockingQueue

    目录 简介 PriorityQueue PriorityBlockingQueue PriorityQueue和PriorityBlockingQueue 简介 Queue一般来说都是FIFO的,当然 ...

  4. 震撼!这个Python模块竟然能自动修复代码!

    说到Python的强大的地方,那真的是太多了,优雅.简洁.丰富且强大的第三方库.开发速度快,社区活跃度高等,所以才使得Python才会如此的受欢迎. 今天给大家介绍一个特别暴力的Python库: Fu ...

  5. Windows下Net6开源akstream项目vs2022调试GB28181协议对接摄像头全流程

    一.背景介绍 笔者经历多个项目对接摄像头需求,不同项目具体要求又有所不同,碰到的摄像头对接开发问题,整理记录.此篇主要用于记录备用及给有缘人提供解决思路等. 1.   同一局域网对接(海康摄像头),如 ...

  6. HDD与你相约深圳,一起探讨创新开发与运营增长

    12月14日,HUAWEI Developer Day(以下简称HDD)将在深圳与广大开发者见面.本次HDD共设有主论坛.两个分论坛及两个闭门会议,期待各位开发者前来参加. 精彩预告 01·主论坛 在 ...

  7. Python将依赖包导出到requirements.txt文件

    代码 # 查询环境中已经安装的库 pip list # 将所有依赖库导出到 requirements.txt 文件 pip freeze > requirements.txt

  8. 元素类型 “item” 相关联的 “name” 属性值不能包含 ‘<’ 字符

    Android构建时报错: app:lintVitalRelease[Fatal Error] :3:214: 与元素类型 "item" 相关联的 "name" ...

  9. web常见的攻击方式有哪些?如何防御?

    一.是什么 Web攻击(WebAttack)是针对用户上网行为或网站服务器等设备进行攻击的行为 如植入恶意代码,修改网站权限,获取网站用户隐私信息等等 Web应用程序的安全性是任何基于Web业务的重要 ...

  10. python实现快排算法,传统快排算法,数据结构

    def quick_sort(lists,i,j): if i >= j: return list pivot = lists[i] low = i high = j while i < ...