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

天地图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);
});

相关文章:

  • OpenHarmony子系统开发 - 电源管理(一)
  • 可视化图解算法:链表相加( 两数相加)
  • LabVIEW软件长时间运行导致蓝屏问题排查与优化
  • SpringCache小记
  • CentOS 7 设置 Nginx 开机自启(使用 Systemd)
  • 【Hbase】列族版本问题
  • sqli-labs学习记录6
  • 使用 Fetch API 实现流式读取
  • 单片机学习笔记——入门51单片机
  • 在windows下通过wsl工具管理Linux子系统
  • UDS诊断、ECU刷写、自动化测试、车联网测试、DTC故障注入测试、坏境测试、可靠性测试、压力测试、性能测试等
  • 计算机四级 - 数据库原理(操作系统部分)- 第5章「内存管理」
  • QT Quick(C++)跨平台应用程序项目实战教程 3 — 项目基本设置(窗体尺寸、中文标题、窗体图标、可执行程序图标)
  • C++:类和对象(含各编译器对编译过程的优化解释)详解[后篇]
  • Neo Gamma 机器人在 GTC 2025 上的突破性进展与表现分析
  • JVM运行时数据区内部结构难记?一个例子优化记忆
  • 避坑指南 | 阿里云服务器centos7上MySQL部署优化指南
  • C++之模板进阶
  • 亮相AWE2025,MOVA以科技重塑生活,以美学沟通世界
  • 标贝科技入选2025年市级数据要素市场化配置改革“揭榜挂帅”名单
  • 商务部新闻发言人就中美日内瓦经贸会谈联合声明发表谈话
  • 第1现场 | 印巴停火次日:当地民众逐渐恢复正常生活
  • 《淮水竹亭》:一手好牌,为何打成这样
  • 海航回应“男团粉丝为追星堵住机舱通道”:已紧急阻止
  • 2025世界数字教育大会将于5月14日至16日在武汉举办
  • 一季度全国消协组织为消费者挽回经济损失23723万元