由于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. Scala 特质(Trait)

    1 package chapter06 2 3 object Test13_Trait { 4 def main(args: Array[String]): Unit = { 5 val studen ...

  2. 高德地图和echarts结合实现地图下钻(二)

    一.学习ajax发送异步请求 1 $(function(){ 2 //请求参数 3 var list = {}; 4 // 5 $.ajax({ 6 //请求方式 7 type : "POS ...

  3. .net跨平台运行实践

    一个偶然的机会,一个朋友想做一个程序,同时支持windows和linux,本来想用go来写,奈何不太熟练,突然想到.net不是也支持跨平台了吗,还没有操作过,刚好可以试验一下. 最新的.net 6已经 ...

  4. #凸包,闵可夫斯基和#CF87E Mogohu-Rea Idol

    题目 按逆时针顺序给出三个凸包点集 \(\mathbb{A,B,C}\),每次查询给出点 \(D\), 问是否存在点 \(A\in\mathbb{A},B\in\mathbb{B},C\in\math ...

  5. #01背包,容斥,排列组合#洛谷 5615 [MtOI2019]时间跳跃

    题目 分析 不是凸多边形当且仅当边数小于2或者最长边大于等于其余边之和, 那么容斥一下,首先总权值为 \[\sum_{i=1}^nC(n,i)\times i=n\sum_{i=1}^nC(n-1,i ...

  6. Git 删除 .gitignore 生成之前上传的文件

    清除缓存 git rm -r --cached . git add . 提交记录 git commit -m "chore: 清除缓存." git push

  7. SpringBoot中bean的生命周期

    目录 概述 使用场景 代码演示bean初始化 TestSupport BeanPostProcessorImpl log 代码 概述 Bean 生命周期管理是 Spring Boot 中的关键功能之一 ...

  8. 如何在 Python 中执行 MySQL 结果限制和分页查询

    Python MySQL 限制结果 限制结果数量 示例 1: 获取您自己的 Python 服务器 选择 "customers" 表中的前 5 条记录: import mysql.c ...

  9. 关于集群节点timeline不一致的处理方式

    关于集群节点 timeline 不一致的处理方式 本文出处:https://www.modb.pro/db/400223 在 PostgreSQL/MogDB/openGauss 数据库日常维护过程中 ...

  10. openGauss/MOGDB时间消耗相关视图

    openGauss/MOGDB 时间消耗相关视图 本文出处:https://www.modb.pro/db/388212 数据库版本 openGauss/MOGDB-2.1.1 一.显示当前用户在各个 ...