arcgis_js 自定义弹窗:vue3+ts+jquery+mitt
·
目录列表

创建一个 ArcgisPopup.ts
注意 $("#map-container").append(infoDiv); map-container 是地图容器,根据当前渲染容器id来设置
import $ from 'jquery'
import { createApp } from 'vue'
import PopupContent from './PopupContent.vue'
import { events } from "@/utils/mitt.js";
interface ArcgisPopupOption {
clear: true, //渲染新数据前,是否清除之前数据
component: string, // 组件路径 Default
minScale: number,
maxScale: number
}
interface PopupData {
x: number, //经度
y: number, //纬度
attributes: any,
popupId: string
}
export class ArcgisPopup {
view: any
popupInfos: Array<any>
option: ArcgisPopupOption
$extentEvent: any
$rotationEvent: any
constructor({ view, option }: { view: any, option: ArcgisPopupOption }) {
this.view = view
this.option = option
this.popupInfos = []
events.off("closeArcgisPopup")
this.addEvent()
events.on("closeArcgisPopup", (obj: any) => {
console.log('关闭弹窗')
this.removeSinglePopup(obj.popupId)
this.popupInfos = this.popupInfos.filter(item => {
return item.popupId !== obj.popupId
})
});
}
removeSinglePopup(popupId: string) {
$("#trackPopUp" + popupId).remove();
}
// 删除监听
removeEvent() {
this.$extentEvent.remove()
this.$rotationEvent.remove()
}
addEvent() {
this.$extentEvent = this.view.watch("extent", () => {
this.relocatePopup();
});
this.$rotationEvent = this.view.watch("rotation", () => {
this.relocatePopup();
});
}
popupRemove() {
$(".leaflet-popup").remove()
this.popupInfos = [];
}
//写入popup的数据
popupRender(response: Array<PopupData>, option?: ArcgisPopupOption) {
console.log('popupRender数据', response)
// localStorage.setItem('popupRenderData', JSON.stringify(response))
if (response.length === 0) {
response = JSON.parse(localStorage.getItem('popupRenderData') || '[]') as Array<PopupData>
}
if (!option) {
option = this.option
}
//popup初始化
console.log('弹窗')
if (option && option.clear === true) {
this.popupRemove()
}
let popupData = []
for (let i = 0; i < response.length; i++) {
let popupId = this.generateRandomString(8)
let obj: PopupData = {
attributes: {
...response[i]
},
x: response[i].x,
y: response[i].y,
popupId: popupId
}
this.popupInfos.push(obj);
popupData.push(obj)
}
for (var i = 0; i < popupData.length; i++) {
var popupInfo: PopupData = popupData[i];
//坐标转换
var mapPoint = {
x: popupInfo.x,
y: popupInfo.y,
spatialReference: this.view.spatialReference
};
var screenPoint = this.view.toScreen(mapPoint);
var obj = {
...popupInfo
};
obj.x = screenPoint.x;
obj.y = screenPoint.y;
obj.popupId = popupInfo.popupId;
this.loadinfoWindow(obj, option);
}
// $(".leaflet-popup").show()
this.relocatePopup()
// if (this.view.scale <= 4200) {
// $(".leaflet-popup").show()
// } else {
// console.log('hide')
// $(".leaflet-popup").hide()
// }
return popupData
}
loadinfoWindow(obj: any, option: ArcgisPopupOption) {
//动态添加气泡窗口DIV
var infoDiv = `
<div id="trackPopUp${obj.popupId}" class="my-popup leaflet-popup">
</div>
`;
$("#map-container").append(infoDiv);
//填充内容
// $('#' + obj.popupId).append(obj.content);
//刷新气泡窗口位置
this.positionPopUp('#trackPopUp' + obj.popupId, obj);
//气泡窗口关闭事件
$('#leaflet-popup-close' + obj.popupId).click(function () {
$('#trackPopUp' + obj.popupId).hide();
});
// ==========================
const app = createApp(PopupContent, { attributes: { ...obj.attributes }, component: option.component, popupId: obj.popupId })
app.mount(`#trackPopUp${obj.popupId}`)
}
//显示指定气泡窗口
showinfoWindow(id: any) {
$('#trackPopUp' + id).show();
}
//隐藏指定气泡窗口
hideinfoWindow(id: any) {
$('#trackPopUp' + id).hide();
}
//刷新气泡窗口位置
positionPopUp(id: any, obj: any) {
// $('#trackPopUp' + obj.popupId).css('transform', 'translate3d(' + obj.x + 'px, ' + obj.y + 'px, 0)');
// $('#vacancyPopUp' + obj.popupId).css('transform', 'translate3d(' + obj.x + 'px, ' + obj.y + 'px, 0)');
$(id).css('transform', 'translate3d(' + obj.x + 'px, ' + obj.y + 'px, 0)');
}
//移动泡窗口位置
relocatePopup() {
let scale = this.view.scale
console.log('弹窗监听 relocatePopup')
// 基础
for (var i = 0; i < this.popupInfos.length; i++) {
var popupInfo = this.popupInfos[i];
//坐标转换
var mapPoint = {
x: popupInfo.x,
y: popupInfo.y,
spatialReference: this.view.spatialReference
};
var screenPoint = this.view.toScreen(mapPoint);
var obj: any = {};
obj.x = screenPoint.x;
obj.y = screenPoint.y;
obj.content = popupInfo.content;
obj.popupId = popupInfo.popupId;
//刷新气泡窗口位置
this.positionPopUp('#trackPopUp' + obj.popupId, obj);
if (scale <= this.option.minScale && scale >= this.option.maxScale) {
$('#trackPopUp' + obj.popupId).show();
} else {
$('#trackPopUp' + obj.popupId).hide();
}
}
}
generateRandomString(length: number) {
let result = '';
const characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
for (let i = 0; i < length; i++) {
result += characters.charAt(Math.floor(Math.random() * characters.length));
}
return result;
}
}
弹窗样式
.my-popup {
position: absolute;
width: 0;
top: 0;
}
.leaflet-popup {
text-align: center;
display: none;
}
.leaflet-popup:hover {
z-index: 2;
}
.lfpopup-content {
transform: translateX(-50%);
background-color: #fff;
color: rgba(52, 111, 229);
width: max-content;
padding: 5px 10px;
font-weight: 600;
border-radius: 4px;
transform: translate(-50%, -140%);
cursor: pointer;
}
创建一个 PopupContent.vue
<template>
<div class="popup-content" @click="handle()">
<component
:is="AsyncComponent"
:attributes="props.attributes"
:popupId="props.popupId"
></component>
</div>
</template>
<script setup>
import { events } from "@/utils/mitt.js";
import { defineAsyncComponent, onMounted, ref } from "vue";
import Default from "./components/Default.vue";
import Test from "./components/Test.vue";
let AsyncComponent = ref(null);
const COMPONENTSMAP = {
Default: Default,
Test: Test,
};
const props = defineProps({
attributes: {
type: Object,
default: {},
},
component: {
type: String,
default: "",
},
popupId: {
type: String,
default: "",
},
});
onMounted(() => {
AsyncComponent.value = COMPONENTSMAP[props.component || "Default"];
});
function handle() {
console.log("handle");
}
</script>
<style lang="scss">
.popup-content {
color: #fff;
width: max-content;
position: absolute;
bottom: 0;
left: 0;
transform: translate(-50%, 0);
}
</style>
在当前目录下,创建 components/Default.vue
<template>
<div class="popup-default">
{{props.attributes}}
</div>
</template>
<script setup>
import { ref } from 'vue'
const value = ref(3)
const props = defineProps({
attributes: {
type: Object,
default: {},
},
popupId: {
type: String,
default: "",
},
});
</script>
<style lang="scss">
.popup-default {
color: #fff;
width: max-content;
margin-bottom: 15px;
}
</style>
使用方式
let arcgisPopup = new ArcgisPopup({
view: this.view, option: {
minScale: 20000000000,
maxScale: 100,
}
} as any)
arcgisPopup.popupRender(
[
{
x: 107.39766855307928,//坐标经度
y: 30.396020227065498,//这边纬度
title: "测试点位",
props: [ //props 这里自定义就行,参数会传到 Default 组件内
{ name: "地块ID", value: 12234, unit: "m2" },
{ name: "地块号", value: 12234, unit: "" },
{ name: "片区", value: 12234, unit: "" },
{ name: "总面积", value: 12234, unit: "" },
],
},
],
{
clear: false, // 渲染是否清除之前弹窗 true:清除 false:不清除
component: "Default", //组件名称
}
);
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)