当前位置: 首页 > article >正文

天地图InfoWindow插入React自定义组件

截至2025年03月21日天地图的Marker不支持添加Label; 同时Label和Icon是不支持自定义HTMLElement只支持String;目前只有InfoWindow支持自定义HTMLElement;

效果图

在这里插入图片描述

React核心api
import ReactDOM from 'react-dom/client'
const content = document.createElement('div');
ReactDOM.createRoot(content).render((
 <CurLineInfoWindowContent optionsClick={curInfoWindowClick}/>
));
天地图InfoWindow
// 创建
const infoWin = new T.InfoWindow();
// 开启
map.openInfoWindow(infoWin, e.lnglat)
// 关闭
map.closeInfoWindow(infoWin);
// 插入自定义
infoWin.setContent(content: String | HTMLElement);

场景模拟: 点击线弹出编辑和删除

自定义组件_编辑/删除
import style from './style.module.less';
const CurLineInfoWindowContent = ({optionsClick}: any) => {
    // 你的React组件,包含编辑和删除按钮
    return (
      <div className={style.custom_line_window_wrap}>
        {/* <span className={style.custom_line_window_name}>路线: {linePathNum} (个点)</span> */}
        <button onClick={(event: any) => {
            const currentTarget = event.currentTarget;
            currentTarget.disabled = true;
            setTimeout(() => { currentTarget.disabled = false; }, 250);
            optionsClick(0);
        }}>点编辑</button>
        <button className={style.del_button}
            onClick={(event: any) => {
            const currentTarget = event.currentTarget;
            currentTarget.disabled = true;
            setTimeout(() => { currentTarget.disabled = false; }, 250);
            optionsClick(1);
        }}>删除</button>
      </div>
    );
};
天地图创建线和绑定事件
/* 创建线 */
var points = [];
points.push(new T.LngLat(116.41239, 39.97569));
points.push(new T.LngLat(116.412799, 39.9068));
points.push(new T.LngLat(116.32970, 39.92940));
points.push(new T.LngLat(116.385440, 39.90610));
var polyline = new T.Polyline(points , {
 	weight: 6,
    color: '#3366FF'
});
/* 创建infoWindow */
const infoWin = new T.InfoWindow();
// 创建一个div
const content = document.createElement('div');
// 绑定自定义组件事件
const curInfoWindowClick(action: number){
	switch(action){
		case 0:
			{ // 处理编辑逻辑
				polyline.enableEdit();
			}
			break;
		case 1:
			{ // 处理删除逻辑
				map.removeOverLay(polyline);
			}
			break;
		default:
             break;
	}
}
// 将React组件渲染到DOM 
ReactDOM.createRoot(content).render((
    <CurLineInfoWindowContent optionsClick={curInfoWindowClick}/>
));
// 插入到infoWindow
infoWin.setContent(content);
/* 绑定事件 */
// 绑定点击事件
polyline.addEventListener('click', (e: any) => {
	map.openInfoWindow(infoWin, e.lnglat)
});
// 如果线被移除了_顺手把弹窗给隐藏掉
polyline.addEventListener('remove', (e: any) => {
	map.closeInfoWindow(infoWin);
});


http://www.kler.cn/a/596303.html

相关文章:

  • UI自动化测试往往在功能测试之后进行的核心原因
  • uniapp特有生命周期钩子
  • 【零基础入门unity游戏开发——unity3D篇】3D模型 —— Rig操纵页签和Avatar化身系统
  • Camera2 与 CameraX 闲谈
  • 【蓝桥杯—单片机】IAP15F2K61S2专项 | 真题整理、解析与拓展 | 省赛题(更新ing...)
  • 效率革命与用户体验的博弈——B端界面设计的底层逻辑与创新实践
  • 常用的pdf技术有哪些?--笔记
  • WebRTC建立Description的通信的实际的原理
  • Qt按钮控件常用的API
  • 自动化测试框架详解
  • https握手过程
  • Qt动态设置样式,实现样式实时切换
  • 项目实战:基于瑞萨RA6M5构建多节点OTA升级-创建工程MCUBoot<二>
  • 【CMake指南】第10篇:复杂项目重构与优化指南(实战)
  • CUDAOpenCV 基于Hessian矩阵计算特征值
  • Rust vs. Go: 在使用最快框架时的性能测试[译]
  • 界面控件DevExpress WPF v25.1预览 - .NET开发效率提升
  • java 数据库连接基于向驱动管理器注册第三方驱动的机制介绍,包含三种注册类型和华为高斯(GaussDB)数据库的完整连接例子
  • 【Node.js入门笔记9---path 模块】
  • PHP函数与数据处理