uniapp中利用renderjs引入leaflet
由于uniapp中要使用地图,虽然uni-app有地图组件map,但是很难用,而且性能很差。在app中是不能操作dom,所以直接用leaflet是不可能的。最终发现了renderjs,官网提出,在app-vue环境下,视图层由webview渲染,而renderjs运行在视图层,自然可以操作dom和window。
使用注意事项:
: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的更多相关文章
- karaf中利用Bundle引入外部log4j配置文件
环境准备: 1.在karaf_home下新建 config及logs目录 2.将mylog4j.properties拷贝到config文件夹下 查看log4j-1.2.17.jar/MANIFEST. ...
- uniapp中利用uni.$emit()和uni.$on()进行页面和tabbar页面传值(页面通讯)
tabbar页面 <script> export default { data() { return { list: [] , }; }, onLoad() { // 监听事件 uni.$ ...
- uni-app 中实现 onLaunch 异步回调后执行 onLoad 最佳实践
前言 好久没写博客了,由于公司业务需要,最近接触uiapp比较多,一直想着输出一些相关的文章.正好最近时间富余,有机会来一波输出了. 问题描述 在使用 uni-app 开发项目时,会遇到需要在 onL ...
- uniapp中引入less文件
uniapp入门遇到的问题记录 在uniapp中从外部import less文件的话,首先需要在 工具>插件安装 中安装支持less语法的插件,然后在.vue文件中引入 @import url ...
- [C# 基础知识系列]专题一:深入解析委托——C#中为什么要引入委托
转自http://www.cnblogs.com/zhili/archive/2012/10/22/Delegate.html 引言: 对于一些刚接触C# 不久的朋友可能会对C#中一些基本特性理解的不 ...
- VUE2 项目 引入 leaflet.draw全过程
leaflet.draw的参考文档:http://leaflet.github.io/Leaflet.draw/docs/leaflet-draw-latest.html 这个网址不稳定,多刷新几 ...
- 在Eclipse中利用maven整合搭建ssm框架
首先说明用到的框架: spring + springMVC + mybatis 构建工具:maven 开发工具:eclipse 开发环境:win10 java版本:jdk1.8 ...
- C#深入解析委托——C#中为什么要引入委托
引言: 对于一些刚接触C# 不久的朋友可能会对C#中一些基本特性理解的不是很深,然而这些知识也是面试时面试官经常会问到的问题,所以我觉得有必要和一些接触C#不久的朋友分享下关于C#基础知识的文章,所以 ...
- vue中的js引入图片,必须require进来
需求:如何components里面的index.vue怎样能把assets里面的图片拿出来. 1.在img标签里面直接写上路径: <img src="../assets/a1.png& ...
- 二叉树系列 - 二叉搜索树 - [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 ...
随机推荐
- #期望,树的直径#51nod 1803 森林直径
题目 有一棵 \(n\) 个结点的树,按顺序给出树边 \((fa[i],i)\), \(Q\) 次询问查询如果只选取第 \([l,r]\) 条树边,问森林的直径 \(fa[i]\) 的生成方式为 \( ...
- #博弈论#Poj 2505 A multiplication game
题目 给你一个整数\(n\),你从1开始乘,乘2-9之间的任意一个数. 最先得到大于等于\(n\)的数的人胜利.Stan先手Ollie后手. 那么,请问给你一个数\(n\),Stan和Ollie都足够 ...
- C# 虚方法virtual详解(转载)
C# 虚方法virtual详解 在C++.Java等众多OOP语言里都可以看到virtual的身影,而C#作为一个完全面向对象的语言当然也不例外. 虚拟函数从C#的程序编译的角度来看,它和其它一般的函 ...
- PMF源解析技术在大气颗粒物与VOCs研究中的创新应用
目前,大气颗粒物和臭氧污染成为我国亟待解决的环境问题.颗粒物和臭氧污染不仅对气候和环境有重要影响,而且对人体健康有严重损害.而臭氧的前体物之一为挥发性有机物(VOCs).为了高效.精准地治理区域大气颗 ...
- HarmonyOS分布式文件系统开发指导
分布式文件系统概述 分布式文件系统(hmdfs,HarmonyOS Distributed File System)提供跨设备的文件访问能力,适用于如下场景: ● 两台设备组网,用户可以利用一台设 ...
- 基于eTS高效开发HarmonyOS课程类应用
原文:https://mp.weixin.qq.com/s/kU76kB6T1JSqapAfGPGRHQ,点击链接查看更多技术内容. 随着HarmonyOS 3.0 Beta版的发布,API Vers ...
- Spring Boot 版本 GA、RC、beta等含义
GA General Availability,正式发布的版本,官方开始推荐广泛使用,国外有的用GA来表示release版本. RELEASE 正式发布版,官方推荐使用的版本,有的用GA来表示.比如s ...
- 重新整理 .net core 实践篇————防跨站脚本攻击[四十]
前言 简单整理一下跨站脚本攻击. 正文 攻击原理是这样子的: 这种攻击被攻击的面挺多的,比如说只要有一个可以让用户输入的注入脚本就都是一个问题. 给网站注入脚本 然后用户访问给网站注入的脚本 脚本里面 ...
- android 找不到设备
前言 当我们安装android studio的时候,测试的时候,你可能找不到设备. 我遇到的有两种情况,一种是本身就需要安装插件,如一些低端机或者有些小米机. 还有一种情况需要去触发一下,有些华为手机 ...
- c# MCV 实现跨域
前言 core跨域严格来说是要分为两步的,因为分为简单跨域和复杂跨域,第一种为直接允许跨域,第二种因为存在某些框架本身不允许put,delete这两个,那么这就是一个问题了.对的,那么mvc这种重量级 ...