活动介绍

uniapp的map怎么防止点重叠

时间: 2025-06-03 15:10:54 浏览: 52
### 解决 UniApp 中地图标记点重叠问题 在处理 UniApp 的 `map` 组件时,当多个标记点位置接近或完全相同的情况下会出现视觉上的重叠现象。为了改善用户体验并有效展示所有标记点的信息,可以采取以下几种最佳实践方法: #### 1. 调整 Marker 图标大小与透明度 通过调整 marker 图标的尺寸以及设置一定的透明度来减少视觉上遮挡的可能性。 ```css /* 设置较小图标 */ .marker-small { width: 20px; height: 30px; } /* 增加透明效果 */ .opacity-marker { opacity: 0.8; } ``` 对于每个 marker 应用样式类[^1]。 #### 2. 使用 Cluster 集群插件 集群化是一种常见的解决办法,在高密度区域自动聚合附近的 markers 成簇状显示,并提供点击展开查看具体位置的功能。这不仅解决了重叠问题还提高了性能表现。 安装第三方库如 `vue-clusterize` 或者寻找适合 uni-app 场景下的开源实现方案。 #### 3. 实现自定义逻辑控制 编写 JavaScript 函数判断两个坐标之间的距离是否小于设定阈值,如果满足条件则改变其中一个 marker 的偏移量(offset),使其稍微偏离原始位置从而达到错开的效果。 ```javascript function adjustMarkerPosition(markers, threshold = 50) { // 单位米 const adjustedMarkers = []; for (let i = 0; i < markers.length; ++i) { let hasOverlap = false; for (let j = 0; !hasOverlap && j < adjustedMarkers.length; ++j) { const distanceBetweenTwoPoints = calculateDistance( markers[i].latitude, markers[i].longitude, adjustedMarkers[j].latitude, adjustedMarkers[j].longitude); if (distanceBetweenTwoPoints <= threshold) { // Apply offset to avoid overlap markers[i].offsetX += Math.random() * 20 - 10; markers[i].offsetY += Math.random() * 20 - 10; hasOverlap = true; } } adjustedMarkers.push({...markers[i]}); } return adjustedMarkers; } ``` 此段代码实现了对输入的 markers 数组进行遍历检查相邻两点间距离,并随机给予轻微的位置偏移以防止覆盖[^2]。 #### 4. 提供交互提示信息 即使经过上述优化措施仍可能存在部分难以避免的小范围重合情况,此时可以在用户操作(比如缩放、拖拽)过程中动态更新提示框内容,告知当前所选中的确切地点名称或其他关联数据。 ---
阅读全文

相关推荐

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <title>高德地图</title> <script src="https://cdn.bootcss.com/vue/2.6.11/vue.js"></script> <style> #map-container { width: 100%; height: 584px; border-radius: 22px; z-index: 4; } /* 新增:隐藏高德默认标记图标 */ #map-container .amap-marker img[src*="mark_bs.png"] { display: none !important; } </style> </head> <body> <script type="text/javascript" src="./js/uni.webview.0.1.52.js"> </script> <script> window._AMapSecurityConfig = { securityJsCode: 'cf331d54f67e306ca5c0b17f7122d559' }; </script> <script src="https://webapi.amap.com/maps?v=2.0&key=f3323c24769b10828db07f07c0fcec0d&plugin=AMap.MarkerCluster"> </script> <script src="https://webapi.amap.com/ui/1.1/main.js"></script> <script> // 确保UniApp通信桥接准备就绪 function setupUniBridge() { return new Promise(resolve => { if (typeof uni.postMessage === 'function') { return resolve(); } document.addEventListener('UniAppJSBridgeReady', resolve); }); } // 向UniApp发送消息(兼容H5和App环境) async function sendMessageToUniApp(data) { // 1. 确保桥接就绪(仅对App环境有效,H5环境会直接resolve) await setupUniBridge(); // 2. 区分环境发送消息 if (isH5Environment()) { // H5环境:使用浏览器原生postMessage console.log('H5环境,使用window.parent.postMessage发送消息'); window.parent.postMessage({ type: 'webviewMessage', // 自定义类型,方便UniApp识别 data: JSON.stringify(data) }, '*'); // 第三个参数为目标域名,*表示允许所有(开发环境可用,生产环境需指定具体域名) } else if (window.uni && typeof uni.postMessage === 'function') { // App环境:使用uni.postMessage console.log('App环境,使用uni.postMessage发送消息'); uni.postMessage({ data: JSON.stringify(data) }); } else { console.error('未支持的环境,无法发送消息'); } } // 辅助函数:判断是否为H5环境(浏览器环境) function isH5Environment() { const protocol = window.location.protocol; // H5环境的协议通常是http:或https: return protocol === 'http:' || protocol === 'https:'; } // 解析URL参数 const params = new URLSearchParams(location.search); const points = JSON.parse(params.get('points') || '[]'); const markers = JSON.parse(params.get('markers') || '[]'); // 初始化地图 const map = new AMap.Map('map-container', { zoom: 10, center: [points[0].lng, points[0].lat], showBuildingBlock: true }); // 自定义单个标记内容(保持原有样式,修复图片路径) function createMarkerContent(markerData) { // 优先使用本地图片,失败则用备用图(确保是自定义图标) const imageUrl = './img/flag.png'; return ${markerData.title} ${markerData.title} ; } // 聚合点渲染函数 function _renderClusterMarker(context) { const count = context.count; console.log('渲染聚合点,数量:', count); // 通过JS直接设置样式(优先级更高) const div = document.createElement('div'); div.innerHTML = count; // 强制设置关键样式(覆盖默认) div.style.width = '40px'; div.style.height = '40px'; div.style.backgroundColor = '#FF9A44'; div.style.borderRadius = '50%'; div.style.display = 'flex'; div.style.alignItems = 'center'; div.style.justifyContent = 'center'; div.style.color = 'white'; div.style.fontWeight = 'bold'; // 设置聚合点偏移(确保居中) context.marker.setOffset(new AMap.Pixel(-20, -20)); context.marker.setContent(div); // 通过context.marker设置内容 } // 单个标记渲染函数 function _renderMarker(context) { const markerData = context.data[0].originalData; // 1. 生成标记内容 const content = createMarkerContent(markerData); // 2. 设置标记内容 context.marker.setContent(content); // 3. 校准偏移量(根据内容尺寸计算,确保标记中心与经纬度对齐) // 内容总高度:图片30px + 标题区域20px = 50px,向上偏移50px使底部对齐定位点 context.marker.setOffset(new AMap.Pixel(-15, -50)); // 水平偏移:30px宽/2=15px // 4. 绑定点击事件 context.marker.on('click', async () => { const message = { type: 'markerClick', data: markerData }; console.log('H5发送消息:', message); // 优先使用uni.postMessag await sendMessageToUniApp({ type: 'markerClick', data: markerData }); }); } // 绘制路线(保持原有功能) function drawRouteAndPoints() { if (points.length === 0) return; const path = points.map(p => [p.lng, p.lat]); new AMap.Polyline({ path: path, strokeColor: "#32CD32", strokeWeight: 5, zIndex: 500, map: map }); if (points.length >= 2) { AMap.plugin('AMap.Driving', () => { const driving = new AMap.Driving({ map: map, policy: AMap.DrivingPolicy.LEAST_TIME, zIndex: 550 }); driving.search( [points[0].lng, points[0].lat], [points[points.length - 1].lng, points[points.length - 1].lat] ); }); } } // 初始化聚合标记 function initClusterMarkers() { if (!markers.length) return; // 转换数据格式为官方示例要求的结构(包含lnglat字段) const clusterPoints = markers.map(marker => ({ lnglat: new AMap.LngLat(marker.lng, marker.lat), // 官方要求的经纬度格式 originalData: marker // 保留原始数据供渲染使用 })); // 参考官方示例:插件加载后初始化聚合 AMap.plugin('AMap.MarkerCluster', function() { if (typeof AMap.MarkerCluster === 'undefined') { console.error('聚合插件加载失败'); return; } // 配置聚合参数 const cluster = new AMap.MarkerCluster(map, clusterPoints, { gridSize: 80, // 网格大小(官方示例默认60,可调整) maxZoom: 15, // 最大聚合级别 minClusterSize: 2, // 最小聚合数量(测试时可改为1) zoomOnClick: true, averageCenter: true, zIndex: 2000, // 聚合点参数名 renderClusterMarker: _renderClusterMarker, // 单个标记参数名 renderMarker: _renderMarker }); // 调整视野 const bounds = new AMap.Bounds(); clusterPoints.forEach(point => bounds.extend(point.lnglat)); map.setBounds(bounds, [50, 50, 50, 50]); }); } // 地图加载完成后初始化 map.on('complete', function() { console.log('地图加载完成,初始化组件'); initClusterMarkers(); if (points.length > 0) drawRouteAndPoints(); }); map.on('error', (err) => console.error('地图错误:', err)); // 构建带标题的HTML内容 // markers.forEach(marker => { // // 构建HTML内容(包含图片和标题) // const content = // // 闽宁新貌展示中心 // // 闽宁新貌展示中心,生动呈现闽宁协作丰硕成果,见证昔日“干沙滩”蜕变为今日“金沙滩”的壮丽历程。 // // // ; // const markerObj = new AMap.Marker({ // position: [marker.lng, marker.lat], // content: content, // 使用HTML内容 // anchor: 'bottom-center', // 锚点在底部中心 // offset: new AMap.Pixel(0, -25) // 向上偏移 // }); // markerObj.on('click', () => { // console.log('window--uni', uni, window); // // console.log(marker, '000'); // // 将数据转为字符串 // const message = JSON.stringify({ // type: 'markerClick', // title: marker.title // }); // window.parent.postMessage(message, '*'); // // 通过uni.postMessage发送数据 // // uni.postMessage({ // // data: JSON.stringify(message) // 发送字符串 // // }); // }); // map.add(markerObj); // }); // 2. 在map加载完成后初始化聚合点 // map.on('complete', function() { // // 创建聚合实例 // const cluster = new AMap.MarkerClusterer(map, markers.map(m => { // return new AMap.Marker({ // position: [m.lng, m.lat], // content: createMarkerContent(m) // 自定义标记 // }); // }), { // gridSize: 80, // maxZoom: 18, // renderClusterMarker: renderCluster // }); // // // 转换数据格式 // // const clusterPoints = markers.map(marker => ({ // // lnglat: [marker.lng, marker.lat], // // title: marker.title, // // originalData: marker // 保留原始数据 // // })); // // console.log('在map加载完成后初始化聚合点', clusterPoints); // // // 确保有聚合点数据 // // if (clusterPoints.length === 0) return; // // 使用MarkerCluster插件 // // AMap.plugin('AMap.MarkerCluster', function() { // // console.log("聚合插件加载完成"); // // // 创建聚合点 // // const cluster = new AMap.MarkerCluster(map, clusterPoints, { // // gridSize: 80, // 聚合网格像素大小 // // maxZoom: 18, // 最大聚合级别 // // minClusterSize: 2, // 最小聚合数量 // // zoomOnClick: true, // 点击聚合点是否放大地图 // // averageCenter: true, // 聚合点是否使用平均值中心 // // // 定义聚合点样式 // // renderClusterMarker: function(context) { // // console.log('定义聚合点样式-context', context); // // // const count = context.count; // // // const div = document.createElement('div'); // // // div.innerHTML = // // // // // // ${count} // // // // // // ; // // const count = context.count; // // const div = document.createElement('div'); // // div.style.background = '#FF9A44'; // // div.style.borderRadius = '50%'; // // div.style.width = '40px'; // // div.style.height = '40px'; // // div.style.display = 'flex'; // // div.style.alignItems = 'center'; // // div.style.justifyContent = 'center'; // // div.style.color = 'white'; // // div.style.fontWeight = 'bold'; // // div.style.zIndex = '99'; // // div.innerHTML = count; // // // context.marker.setContent(div); // // return div; // // }, // // // 自定义标记点样式 // // renderMarker: function(context) { // // const markerData = context.data[0].originalData; // // // console.log('自定义标记点样式-context', context); // // // const marker = new AMap.Marker({ // // // position: [markerData.lng, markerData.lat], // // // content: // // // // // // // // // // // // ${markerData.title} // // // // // // // // // , // // // offset: new AMap.Pixel(0, 0) // // // }); // // // 创建自定义内容 // // const content = // // // // // // // // ${markerData.title} ; // // const marker = new AMap.Marker({ // // position: [markerData.lng, markerData.lat], // // content: content, // // offset: new AMap.Pixel(-2, -4) // 根据实际图片调整偏移 // // }); // // // 绑定聚合点点击事件 // // // marker.on('click', async function(e) { // // // console.log(e, 'bbbbb'); // // // sendMessageToUniApp({ // // // type: 'markerClick', // // // center: e.lnglat // // // }); // // // }); // // // // console.log('聚合点点', marker); // // // map.add(marker); // // // 绑定标记点击事件 // // marker.on('click', async function(e) { // // 发送消息给UniApp // // await sendMessageToUniApp({ // // type: 'markerClick', // // data: markerData // // }); // // }); // // return marker; // // }, // // }); // // // 绑定聚合点点击事件 // // cluster.on('click', async function(e) { // // console.log(e, 'bbbbb'); // // // 发送聚合点点击事件 // // await sendMessageToUniApp({ // // type: 'clusterClick', // // data: { // // count: e.clusterData.count, // // center: e.clusterData.center, // // markers: e.clusterData.markers // // } // // }); // // }); // // }); // }); // 2. 绘制路线 // const path = points.map(p => [p.lng, p.lat]); // new AMap.Polyline({ // path: path, // strokeColor: "#32CD32", // strokeWeight: 5, // map: map // }); // 3. 添加起点终点标记 // points.forEach((point, i) => { // new AMap.Marker({ // position: [point.lng, point.lat], // // content: ${i === 0 ? '起' : '终'}, // offset: new AMap.Pixel(-10, -10), // map: map // }); // }); // 4. 绘制驾车路径(如果需要,可以注释掉直线路径,保留驾车路径) // 注意:这里同时绘制了直线和驾车路径,可能会重叠 // if (points.length >= 2) { // AMap.plugin('AMap.Driving', () => { // const driving = new AMap.Driving({ // map: map, // 这样驾车路线会直接显示在地图上 // policy: AMap.DrivingPolicy.LEAST_TIME // }); // driving.search( // [points[0].lng, points[0].lat], // [points[points.length - 1].lng, points[points.length - 1].lat], // (status, result) => { // // 可以根据结果处理 // } // ); // }); // } // 1. 绘制路线 // if (points.length > 0) { // const path = points.map(p => [p.lng, p.lat]); // new AMap.Polyline({ // path: path, // strokeColor: "#32CD32", // strokeWeight: 5, // map: map // }); // // 添加起点终点标记 // points.forEach((point, i) => { // new AMap.Marker({ // position: [point.lng, point.lat], // offset: new AMap.Pixel(-10, -10), // map: map // }); // }); // // 绘制驾车路径(如果需要) // if (points.length >= 2) { // AMap.plugin('AMap.Driving', () => { // const driving = new AMap.Driving({ // map: map, // policy: AMap.DrivingPolicy.LEAST_TIME // }); // driving.search( // [points[0].lng, points[0].lat], // [points[points.length - 1].lng, points[points.length - 1].lat] // ); // }); // } // } </script> </body> </html>点击点标记只有console.log('H5发送消息:', message);这个打印,其他的没有反应

地图上除了聚合点和起点终点,什么都没有显示 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <title>高德地图</title> <script src="https://cdn.bootcss.com/vue/2.6.11/vue.js"></script> <style> #map-container { width: 100%; height: 584px; border-radius: 22px; z-index: 4; } /* 聚合点样式(参考官方示例的内联样式+CSS结合) */ .cluster-marker { background: #FF9A44 !important; border-radius: 50% !important; width: 40px !important; height: 40px !important; display: flex !important; align-items: center !important; justify-content: center !important; color: white !important; font-weight: bold !important; z-index: 1000 !important; box-shadow: 0 2px 6px rgba(0, 0, 0, 0.5) !important; border: 2px solid white !important; } /* 新增:隐藏高德默认标记图标 */ #map-container .amap-marker img[src*="mark_bs.png"] { display: none !important; } #map-container .amap-marker-content { overflow: visible !important; z-index: 1000 !important; } </style> </head> <body> <script type="text/javascript" src="./js/uni.webview.0.1.52.js"> </script> <script> window._AMapSecurityConfig = { securityJsCode: 'cf331d54f67e306ca5c0b17f7122d559' }; </script> <script src="https://webapi.amap.com/maps?v=2.0&key=f3323c24769b10828db07f07c0fcec0d&plugin=AMap.MarkerCluster"> </script> <script src="https://webapi.amap.com/ui/1.1/main.js"></script> <script> // 确保UniApp通信桥接准备就绪 function setupUniBridge() { console.log('window--uni', uni); return new Promise(resolve => { if (typeof uni.postMessage === 'function') { console.log('window--uni', window.uni); return resolve(); } document.addEventListener('UniAppJSBridgeReady', resolve); }); } // 向UniApp发送消息 async function sendMessageToUniApp(data) { await setupUniBridge(); if (window.uni && typeof uni.postMessage === 'function') { console.log('window--uni--00', window.uni); uni.postMessage({ data: JSON.stringify(data) // 发送字符串 }); } else if (window.parent) { console.log('window--uni--11', window.uni); window.parent.postMessage({ type: 'webviewMessage', data: JSON.stringify(data) }, '*'); } } // 解析URL参数 const params = new URLSearchParams(location.search); const points = JSON.parse(params.get('points') || '[]'); const markers = JSON.parse(params.get('markers') || '[]'); // 初始化地图 const map = new AMap.Map('map-container', { zoom: 10, center: [points[0].lng, points[0].lat], showBuildingBlock: true }); // 自定义单个标记内容(保持原有样式,修复图片路径) function createMarkerContent(markerData) { // 优先使用本地图片,失败则用备用图(确保是自定义图标) const imageUrl = './img/flag.png'; const fallbackUrl = 'https://picsum.photos/40/50'; // 替换为你的自定义备用图 // 生成唯一的类名,避免样式冲突 const uniqueClass = marker-${Date.now()}-${Math.random().toString(36).substr(2, 5)}; return <style> /* 隐藏高德默认图标 */ #map-container .amap-marker img[src*="mark_bs.png"] { display: none !important; } /* 只显示成功加载的图片(本地图或备用图) */ .${uniqueClass} img { display: block !important; width:40px !important; height:50px !important; } </style> ${markerData.title} ${markerData.title} ; } // 聚合点渲染函数(参考官方示例的context处理) function _renderClusterMarker(context) { const count = context.count; console.log('渲染聚合点,数量:', count); // 参考官方示例:通过JS直接设置样式(优先级更高) const div = document.createElement('div'); div.className = 'cluster-marker'; div.innerHTML = count; // 强制设置关键样式(覆盖默认) div.style.width = '40px'; div.style.height = '40px'; div.style.backgroundColor = '#FF9A44'; div.style.borderRadius = '50%'; // 官方示例:设置聚合点偏移(确保居中) context.marker.setOffset(new AMap.Pixel(-20, -20)); context.marker.setContent(div); // 关键:通过context.marker设置内容 } // 单个标记渲染函数(参考官方示例的_renderMarker命名和参数) function _renderMarker(context) { const markerData = context.data[0].originalData; console.log('渲染单个标记:', markerData); const marker = new AMap.Marker({ position: [markerData.lng, markerData.lat], content: createMarkerContent(markerData), offset: new AMap.Pixel(-20, -50), // 精确偏移 anchor: 'bottom-center', zIndex: 1000 }); // 绑定点击事件 marker.on('click', async () => { await sendMessageToUniApp({ type: 'markerClick', data: markerData }); }); return marker; } // 绘制路线(保持原有功能) function drawRouteAndPoints() { if (points.length === 0) return; const path = points.map(p => [p.lng, p.lat]); new AMap.Polyline({ path: path, strokeColor: "#32CD32", strokeWeight: 5, zIndex: 500, map: map }); if (points.length >= 2) { AMap.plugin('AMap.Driving', () => { const driving = new AMap.Driving({ map: map, policy: AMap.DrivingPolicy.LEAST_TIME, zIndex: 550 }); driving.search( [points[0].lng, points[0].lat], [points[points.length - 1].lng, points[points.length - 1].lat] ); }); } } // 初始化聚合标记(核心:按官方示例配置参数) function initClusterMarkers() { if (!markers.length) return; // 转换数据格式为官方示例要求的结构(包含lnglat字段) const clusterPoints = markers.map(marker => ({ lnglat: new AMap.LngLat(marker.lng, marker.lat), // 官方要求的经纬度格式 originalData: marker // 保留原始数据供渲染使用 })); // 参考官方示例:插件加载后初始化聚合 AMap.plugin('AMap.MarkerCluster', function() { if (typeof AMap.MarkerCluster === 'undefined') { console.error('聚合插件加载失败'); return; } // 核心:按官方示例配置聚合参数 const cluster = new AMap.MarkerCluster(map, clusterPoints, { gridSize: 80, // 网格大小(官方示例默认60,可调整) maxZoom: 18, // 最大聚合级别 minClusterSize: 2, // 最小聚合数量(测试时可改为1) zoomOnClick: true, averageCenter: true, zIndex: 1000, // 官方示例:聚合点渲染函数参数名 renderClusterMarker: _renderClusterMarker, // 官方示例:单个标记渲染函数参数名 renderMarker: _renderMarker }); // 调整视野(保持原有功能) const bounds = new AMap.Bounds(); clusterPoints.forEach(point => bounds.extend(point.lnglat)); map.setBounds(bounds, [50, 50, 50, 50]); if (clusterPoints.length === 1) { map.setZoom(15); } }); } // 地图加载完成后初始化(保持原有功能) map.on('complete', function() { console.log('地图加载完成,初始化组件'); initClusterMarkers(); if (points.length > 0) drawRouteAndPoints(); }); map.on('error', (err) => console.error('地图错误:', err)); // 构建带标题的HTML内容 // markers.forEach(marker => { // // 构建HTML内容(包含图片和标题) // const content = // // 闽宁新貌展示中心 // // 闽宁新貌展示中心,生动呈现闽宁协作丰硕成果,见证昔日“干沙滩”蜕变为今日“金沙滩”的壮丽历程。 // // // ; // const markerObj = new AMap.Marker({ // position: [marker.lng, marker.lat], // content: content, // 使用HTML内容 // anchor: 'bottom-center', // 锚点在底部中心 // offset: new AMap.Pixel(0, -25) // 向上偏移 // }); // markerObj.on('click', () => { // console.log('window--uni', uni, window); // // console.log(marker, '000'); // // 将数据转为字符串 // const message = JSON.stringify({ // type: 'markerClick', // title: marker.title // }); // window.parent.postMessage(message, '*'); // // 通过uni.postMessage发送数据 // // uni.postMessage({ // // data: JSON.stringify(message) // 发送字符串 // // }); // }); // map.add(markerObj); // }); // 2. 在map加载完成后初始化聚合点 // map.on('complete', function() { // // 创建聚合实例 // const cluster = new AMap.MarkerClusterer(map, markers.map(m => { // return new AMap.Marker({ // position: [m.lng, m.lat], // content: createMarkerContent(m) // 自定义标记 // }); // }), { // gridSize: 80, // maxZoom: 18, // renderClusterMarker: renderCluster // }); // // // 转换数据格式 // // const clusterPoints = markers.map(marker => ({ // // lnglat: [marker.lng, marker.lat], // // title: marker.title, // // originalData: marker // 保留原始数据 // // })); // // console.log('在map加载完成后初始化聚合点', clusterPoints); // // // 确保有聚合点数据 // // if (clusterPoints.length === 0) return; // // 使用MarkerCluster插件 // // AMap.plugin('AMap.MarkerCluster', function() { // // console.log("聚合插件加载完成"); // // // 创建聚合点 // // const cluster = new AMap.MarkerCluster(map, clusterPoints, { // // gridSize: 80, // 聚合网格像素大小 // // maxZoom: 18, // 最大聚合级别 // // minClusterSize: 2, // 最小聚合数量 // // zoomOnClick: true, // 点击聚合点是否放大地图 // // averageCenter: true, // 聚合点是否使用平均值中心 // // // 定义聚合点样式 // // renderClusterMarker: function(context) { // // console.log('定义聚合点样式-context', context); // // // const count = context.count; // // // const div = document.createElement('div'); // // // div.innerHTML = // // // // // // ${count} // // // // // // ; // // const count = context.count; // // const div = document.createElement('div'); // // div.style.background = '#FF9A44'; // // div.style.borderRadius = '50%'; // // div.style.width = '40px'; // // div.style.height = '40px'; // // div.style.display = 'flex'; // // div.style.alignItems = 'center'; // // div.style.justifyContent = 'center'; // // div.style.color = 'white'; // // div.style.fontWeight = 'bold'; // // div.style.zIndex = '99'; // // div.innerHTML = count; // // // context.marker.setContent(div); // // return div; // // }, // // // 自定义标记点样式 // // renderMarker: function(context) { // // const markerData = context.data[0].originalData; // // // console.log('自定义标记点样式-context', context); // // // const marker = new AMap.Marker({ // // // position: [markerData.lng, markerData.lat], // // // content: // // // // // // // // // // // // ${markerData.title} // // // // // // // // // , // // // offset: new AMap.Pixel(0, 0) // // // }); // // // 创建自定义内容 // // const content = // // // // // // // // ${markerData.title} ; // // const marker = new AMap.Marker({ // // position: [markerData.lng, markerData.lat], // // content: content, // // offset: new AMap.Pixel(-2, -4) // 根据实际图片调整偏移 // // }); // // // 绑定聚合点点击事件 // // // marker.on('click', async function(e) { // // // console.log(e, 'bbbbb'); // // // sendMessageToUniApp({ // // // type: 'markerClick', // // // center: e.lnglat // // // }); // // // }); // // // // console.log('聚合点点', marker); // // // map.add(marker); // // // 绑定标记点击事件 // // marker.on('click', async function(e) { // // 发送消息给UniApp // // await sendMessageToUniApp({ // // type: 'markerClick', // // data: markerData // // }); // // }); // // return marker; // // }, // // }); // // // 绑定聚合点点击事件 // // cluster.on('click', async function(e) { // // console.log(e, 'bbbbb'); // // // 发送聚合点点击事件 // // await sendMessageToUniApp({ // // type: 'clusterClick', // // data: { // // count: e.clusterData.count, // // center: e.clusterData.center, // // markers: e.clusterData.markers // // } // // }); // // }); // // }); // }); // 2. 绘制路线 // const path = points.map(p => [p.lng, p.lat]); // new AMap.Polyline({ // path: path, // strokeColor: "#32CD32", // strokeWeight: 5, // map: map // }); // 3. 添加起点终点标记 // points.forEach((point, i) => { // new AMap.Marker({ // position: [point.lng, point.lat], // // content: ${i === 0 ? '起' : '终'}, // offset: new AMap.Pixel(-10, -10), // map: map // }); // }); // 4. 绘制驾车路径(如果需要,可以注释掉直线路径,保留驾车路径) // 注意:这里同时绘制了直线和驾车路径,可能会重叠 // if (points.length >= 2) { // AMap.plugin('AMap.Driving', () => { // const driving = new AMap.Driving({ // map: map, // 这样驾车路线会直接显示在地图上 // policy: AMap.DrivingPolicy.LEAST_TIME // }); // driving.search( // [points[0].lng, points[0].lat], // [points[points.length - 1].lng, points[points.length - 1].lat], // (status, result) => { // // 可以根据结果处理 // } // ); // }); // } // 1. 绘制路线 // if (points.length > 0) { // const path = points.map(p => [p.lng, p.lat]); // new AMap.Polyline({ // path: path, // strokeColor: "#32CD32", // strokeWeight: 5, // map: map // }); // // 添加起点终点标记 // points.forEach((point, i) => { // new AMap.Marker({ // position: [point.lng, point.lat], // offset: new AMap.Pixel(-10, -10), // map: map // }); // }); // // 绘制驾车路径(如果需要) // if (points.length >= 2) { // AMap.plugin('AMap.Driving', () => { // const driving = new AMap.Driving({ // map: map, // policy: AMap.DrivingPolicy.LEAST_TIME // }); // driving.search( // [points[0].lng, points[0].lat], // [points[points.length - 1].lng, points[points.length - 1].lat] // ); // }); // } // } </script> </body> </html>

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <title>高德地图</title> <script src="https://webapi.amap.com/loader.js"></script> <script src="https://cdn.bootcss.com/vue/2.6.11/vue.js"></script> <style> #map-container { width: 100%; height: 584px; border-radius: 22px; z-index: 4; } </style> </head> <body> <script src="https://webapi.amap.com/maps?v=2.0&key=f3323c24769b10828db07f07c0fcec0d"></script> <script> // 解析URL参数 const params = new URLSearchParams(location.search); const points = JSON.parse(params.get('points')); const markers = JSON.parse(params.get('markers')); // 初始化地图 const map = new AMap.Map('map-container', { zoom: 10, center: [points[0].lng, points[0].lat] // center: [106.235589, 38.45766] }); // 1. 添加标记点 markers.forEach(marker => { new AMap.Marker({ position: [marker.lng, marker.lat], title: marker.title, map: map }).on('click', () => { // 向UniApp发送点击事件 window.parent.postMessage({ type: 'markerClick', title: marker.title }, '*'); }); }); // 2. 绘制路线 const path = points.map(p => [p.lng, p.lat]); new AMap.Polyline({ path: path, strokeColor: "#3366FF", strokeWeight: 5, map: map }); // 3. 添加起点终点标记 points.forEach((point, i) => { new AMap.Marker({ position: [point.lng, point.lat], content: ${i === 0 ? '起' : '终'}, offset: new AMap.Pixel(-10, -10), map: map }); }); // 4. 绘制驾车路径(如果需要,可以注释掉直线路径,保留驾车路径) // 注意:这里同时绘制了直线和驾车路径,可能会重叠 if (points.length >= 2) { AMap.plugin('AMap.Driving', () => { const driving = new AMap.Driving({ map: map, // 这样驾车路线会直接显示在地图上 policy: AMap.DrivingPolicy.LEAST_TIME }); driving.search( [points[0].lng, points[0].lat], [points[points.length - 1].lng, points[points.length - 1].lat], (status, result) => { // 可以根据结果处理 } ); }); } </script> </body> </html>markers: JSON.stringify([ { lng: 106.28776197503275, lat: 38.47105952636814, title: '鼓楼' }, { lng: 106.01313200136352, lat: 38.434956774850576, title: '西夏陵' }, { lng: 106.235068, lat: 38.484468, title: '宁夏博物馆' }, { lng: 106.123554, lat: 38.231086, title: '宁夏工委纪念馆' }, { lng: 105.975629, lat: 38.244193, title: '闽宁新貌展示中心' }, { lng: 106.381172, lat: 38.566882, title: '灵武市国家生态文明教育基地' }, { lng: 106.584342, lat: 38.159979, title: '国家能源集团宁夏煤业有限公司实践教育基地' } ]) };添加标记点,展示标题和自定义图片,怎么修改这个样式

function updateMarkerContent(markerData) { // 1. 先关闭已打开的详情(关键步骤) resetActiveMarker(); // 2. 处理当前标记 const markerKey = ${markerData.lng},${markerData.lat}; const marker = markerInstances[markerKey]; if (!marker) { console.error('未找到标记点实例:', markerKey); return; } // 3. 存储原始数据到marker(用于后续重置时获取) marker.originalData = markerData; // 4. 生成详情样式content const content = ${markerData.title} ${markerData.desc || '暂无描述'} ; // 5. 更新为详情样式 marker.setContent(content); marker.setOffset(new AMap.Pixel(-110, -200)); // 6. 记录当前活跃标记 currentActiveMarkerKey = markerKey; }给这个盒子最外层的右边上面添加两个图片。竖向排列,一个是map_back一个是map_detail,点击第一个图片,返回聚合点样式,关闭当前详情,点击第二个是跳转到详情页面(pages/achievements/index),以下是map.html所有的代码 <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <title>高德地图</title> <script src="https://cdn.bootcss.com/vue/2.6.11/vue.js"></script> <style> #map-container { width: 100%; height: 584px; border-radius: 22px; z-index: 4; } /* 新增:隐藏高德默认标记图标 */ #map-container .amap-marker img[src*="mark_bs.png"] { display: none !important; } </style> </head> <body> <script type="text/javascript" src="./js/uni.webview.0.1.52.js"> </script> <script> window._AMapSecurityConfig = { securityJsCode: 'cf331d54f67e306ca5c0b17f7122d559' }; </script> <script src="https://webapi.amap.com/maps?v=2.0&key=f3323c24769b10828db07f07c0fcec0d&plugin=AMap.MarkerCluster"> </script> <script src="https://webapi.amap.com/ui/1.1/main.js"></script> <script> // 确保UniApp通信桥接准备就绪 function setupUniBridge() { return new Promise(resolve => { // H5环境直接就绪(无需等待UniApp桥接) if (isH5Environment()) { return resolve(); } // App环境逻辑不变 if (typeof uni.postMessage === 'function') { return resolve(); } document.addEventListener('UniAppJSBridgeReady', resolve); }); } // 向UniApp发送消息(兼容H5和App环境) async function sendMessageToUniApp(data) { // console.log('当前环境是否为H5--11:', this.isH5Environment()); // 应打印true(H5环境) // 1. 确保桥接就绪(仅对App环境有效,H5环境会直接resolve) await setupUniBridge(); // 2. 区分环境发送消息 if (isH5Environment()) { // H5环境:使用浏览器原生postMessage // console.log('H5环境,使用window.parent.postMessage发送消息'); window.parent.postMessage({ type: 'webviewMessage', // 自定义类型,方便UniApp识别 data: JSON.stringify(data) }, '*'); // 第三个参数为目标域名,*表示允许所有(开发环境可用,生产环境需指定具体域名) } else if (window.uni && typeof uni.postMessage === 'function') { // App环境:使用uni.postMessage // console.log('App环境,使用uni.postMessage发送消息'); uni.postMessage({ data: JSON.stringify(data) }); } else { console.error('未支持的环境,无法发送消息'); } } // 辅助函数:判断是否为H5环境(浏览器环境) function isH5Environment() { const protocol = window.location.protocol; // H5环境的协议通常是http:或https: return protocol === 'http:' || protocol === 'https:'; } // 解析URL参数 const params = new URLSearchParams(location.search); const points = JSON.parse(params.get('points') || '[]'); const markers = JSON.parse(params.get('markers') || '[]'); const center = JSON.parse(params.get('center') || '[]'); console.log(center, '解析URL参数'); // 初始化地图 const map = new AMap.Map('map-container', { zoom: 10, center: center || [106.283333, 38.466667], // [markers[0].lng, markers[0].lat] showBuildingBlock: true }); // 新增:存储所有标记点实例(key为"lng,lat",值为标记点对象) const markerInstances = {}; // 自定义单个标记内容(保持原有样式,修复图片路径) function createMarkerContent(markerData) { // 优先使用本地图片,失败则用备用图(确保是自定义图标) const imageUrl = './img/flag.png'; return ${markerData.title} ${markerData.title} ; } // 聚合点渲染函数 function _renderClusterMarker(context) { const count = context.count; console.log('渲染聚合点,数量:', count); // 通过JS直接设置样式(优先级更高) const div = document.createElement('div'); div.innerHTML = count; // 强制设置关键样式(覆盖默认) div.style.width = '40px'; div.style.height = '40px'; div.style.backgroundColor = '#E3383B'; div.style.borderRadius = '50%'; div.style.display = 'flex'; div.style.alignItems = 'center'; div.style.justifyContent = 'center'; div.style.color = 'white'; div.style.fontWeight = 'bold'; // 设置聚合点偏移(确保居中) context.marker.setOffset(new AMap.Pixel(-20, -20)); context.marker.setContent(div); // 通过context.marker设置内容 } // 单个标记渲染函数 function _renderMarker(context) { const markerData = context.data[0].originalData; // 生成唯一key(lng+lat) const markerKey = ${markerData.lng},${markerData.lat}; // 1. 生成标记内容 const content = createMarkerContent(markerData); // 2. 设置标记内容 context.marker.setContent(content); // 3. 校准偏移量(根据内容尺寸计算,确保标记中心与经纬度对齐) // 内容总高度:图片30px + 标题区域20px = 50px,向上偏移50px使底部对齐定位点 context.marker.setOffset(new AMap.Pixel(-15, -50)); // 水平偏移:30px宽/2=15px // 初始化时就设置originalData context.marker.originalData = markerData; // 4. 存储标记点实例到全局对象 markerInstances[markerKey] = context.marker; // 5. 绑定点击事件 context.marker.on('click', async () => { // 若点击的是已打开的标记,则关闭它 if (currentActiveMarkerKey === markerKey) { resetActiveMarker(); return; } const message = { type: 'markerClick', data: markerData }; // console.log('H5发送消息:', message); // 优先使用uni.postMessag await sendMessageToUniApp({ type: 'markerClick', data: markerData }); }); } // 记录当前活跃的标记点key(lng,lat) let currentActiveMarkerKey = null; // 新增:重置活跃标记为默认样式 function resetActiveMarker() { if (!currentActiveMarkerKey) return; const marker = markerInstances[currentActiveMarkerKey]; if (!marker) { currentActiveMarkerKey = null; return; } // 1. 尝试获取marker.originalData let markerData = marker.originalData; // 2. 若不存在,则从原始markers数组中查找 if (!markerData) { const [lng, lat] = currentActiveMarkerKey.split(','); markerData = markers.find(m => m.lng.toString() === lng && m.lat.toString() === lat ); } // 3. 双重校验,确保markerData存在 if (!markerData) { console.warn('未找到标记点原始数据,无法重置:', currentActiveMarkerKey); currentActiveMarkerKey = null; return; } // 4. 安全恢复默认样式 marker.setContent(createMarkerContent(markerData)); marker.setOffset(new AMap.Pixel(-15, -50)); currentActiveMarkerKey = null; } // H5 接收 UniApp 指令,更新标记点内容的函数 function updateMarkerContent(markerData) { // 1. 先关闭已打开的详情(关键步骤) resetActiveMarker(); // 2. 处理当前标记 const markerKey = ${markerData.lng},${markerData.lat}; const marker = markerInstances[markerKey]; if (!marker) { console.error('未找到标记点实例:', markerKey); return; } // 3. 存储原始数据到marker(用于后续重置时获取) marker.originalData = markerData; // 4. 生成详情样式content const content = ${markerData.title} ${markerData.desc || '暂无描述'} ; // 5. 更新为详情样式 marker.setContent(content); marker.setOffset(new AMap.Pixel(-110, -200)); // 6. 记录当前活跃标记 currentActiveMarkerKey = markerKey; } // 绘制路线(保持原有功能) function drawRouteAndPoints() { if (points.length === 0) return; const path = points.map(p => [p.lng, p.lat]); new AMap.Polyline({ path: path, strokeColor: "#32CD32", strokeWeight: 5, zIndex: 500, map: map }); if (points.length >= 2) { AMap.plugin('AMap.Driving', () => { const driving = new AMap.Driving({ map: map, policy: AMap.DrivingPolicy.LEAST_TIME, zIndex: 550 }); driving.search( [points[0].lng, points[0].lat], [points[points.length - 1].lng, points[points.length - 1].lat] ); }); } } // 初始化聚合标记 function initClusterMarkers() { if (!markers.length) return; // 转换数据格式为官方示例要求的结构(包含lnglat字段) const clusterPoints = markers.map(marker => ({ lnglat: new AMap.LngLat(marker.lng, marker.lat), // 官方要求的经纬度格式 originalData: marker // 保留原始数据供渲染使用 })); // 参考官方示例:插件加载后初始化聚合 AMap.plugin('AMap.MarkerCluster', function() { if (typeof AMap.MarkerCluster === 'undefined') { console.error('聚合插件加载失败'); return; } // 配置聚合参数 const cluster = new AMap.MarkerCluster(map, clusterPoints, { gridSize: 80, // 网格大小(官方示例默认60,可调整) maxZoom: 15, // 最大聚合级别 minClusterSize: 2, // 最小聚合数量(测试时可改为1) zoomOnClick: true, averageCenter: true, // zIndex: 2000, // 聚合点参数名 renderClusterMarker: _renderClusterMarker, // 单个标记参数名 renderMarker: _renderMarker }); // 调整视野 // const bounds = new AMap.Bounds(); // clusterPoints.forEach(point => bounds.extend(point.lnglat)); // map.setBounds(bounds, [50, 50, 50, 50]); }); } // 地图加载完成后初始化 map.on('complete', function() { console.log('地图加载完成,初始化组件'); initClusterMarkers(); if (points.length > 0) drawRouteAndPoints(); }); map.on('error', (err) => console.error('地图错误:', err)); // 构建带标题的HTML内容 // markers.forEach(marker => { // // 构建HTML内容(包含图片和标题) // const content = // // 闽宁新貌展示中心 // // 闽宁新貌展示中心,生动呈现闽宁协作丰硕成果,见证昔日“干沙滩”蜕变为今日“金沙滩”的壮丽历程。 // // // ; // const markerObj = new AMap.Marker({ // position: [marker.lng, marker.lat], // content: content, // 使用HTML内容 // anchor: 'bottom-center', // 锚点在底部中心 // offset: new AMap.Pixel(0, -25) // 向上偏移 // }); // markerObj.on('click', () => { // console.log('window--uni', uni, window); // // console.log(marker, '000'); // // 将数据转为字符串 // const message = JSON.stringify({ // type: 'markerClick', // title: marker.title // }); // window.parent.postMessage(message, '*'); // // 通过uni.postMessage发送数据 // // uni.postMessage({ // // data: JSON.stringify(message) // 发送字符串 // // }); // }); // map.add(markerObj); // }); // 2. 在map加载完成后初始化聚合点 // map.on('complete', function() { // // 创建聚合实例 // const cluster = new AMap.MarkerClusterer(map, markers.map(m => { // return new AMap.Marker({ // position: [m.lng, m.lat], // content: createMarkerContent(m) // 自定义标记 // }); // }), { // gridSize: 80, // maxZoom: 18, // renderClusterMarker: renderCluster // }); // // // 转换数据格式 // // const clusterPoints = markers.map(marker => ({ // // lnglat: [marker.lng, marker.lat], // // title: marker.title, // // originalData: marker // 保留原始数据 // // })); // // console.log('在map加载完成后初始化聚合点', clusterPoints); // // // 确保有聚合点数据 // // if (clusterPoints.length === 0) return; // // 使用MarkerCluster插件 // // AMap.plugin('AMap.MarkerCluster', function() { // // console.log("聚合插件加载完成"); // // // 创建聚合点 // // const cluster = new AMap.MarkerCluster(map, clusterPoints, { // // gridSize: 80, // 聚合网格像素大小 // // maxZoom: 18, // 最大聚合级别 // // minClusterSize: 2, // 最小聚合数量 // // zoomOnClick: true, // 点击聚合点是否放大地图 // // averageCenter: true, // 聚合点是否使用平均值中心 // // // 定义聚合点样式 // // renderClusterMarker: function(context) { // // console.log('定义聚合点样式-context', context); // // // const count = context.count; // // // const div = document.createElement('div'); // // // div.innerHTML = // // // // // // ${count} // // // // // // ; // // const count = context.count; // // const div = document.createElement('div'); // // div.style.background = '#FF9A44'; // // div.style.borderRadius = '50%'; // // div.style.width = '40px'; // // div.style.height = '40px'; // // div.style.display = 'flex'; // // div.style.alignItems = 'center'; // // div.style.justifyContent = 'center'; // // div.style.color = 'white'; // // div.style.fontWeight = 'bold'; // // div.style.zIndex = '99'; // // div.innerHTML = count; // // // context.marker.setContent(div); // // return div; // // }, // // // 自定义标记点样式 // // renderMarker: function(context) { // // const markerData = context.data[0].originalData; // // // console.log('自定义标记点样式-context', context); // // // const marker = new AMap.Marker({ // // // position: [markerData.lng, markerData.lat], // // // content: // // // // // // // // // // // // ${markerData.title} // // // // // // // // // , // // // offset: new AMap.Pixel(0, 0) // // // }); // // // 创建自定义内容 // // const content = // // // // // // // // ${markerData.title} ; // // const marker = new AMap.Marker({ // // position: [markerData.lng, markerData.lat], // // content: content, // // offset: new AMap.Pixel(-2, -4) // 根据实际图片调整偏移 // // }); // // // 绑定聚合点点击事件 // // // marker.on('click', async function(e) { // // // console.log(e, 'bbbbb'); // // // sendMessageToUniApp({ // // // type: 'markerClick', // // // center: e.lnglat // // // }); // // // }); // // // // console.log('聚合点点', marker); // // // map.add(marker); // // // 绑定标记点击事件 // // marker.on('click', async function(e) { // // 发送消息给UniApp // // await sendMessageToUniApp({ // // type: 'markerClick', // // data: markerData // // }); // // }); // // return marker; // // }, // // }); // // // 绑定聚合点点击事件 // // cluster.on('click', async function(e) { // // console.log(e, 'bbbbb'); // // // 发送聚合点点击事件 // // await sendMessageToUniApp({ // // type: 'clusterClick', // // data: { // // count: e.clusterData.count, // // center: e.clusterData.center, // // markers: e.clusterData.markers // // } // // }); // // }); // // }); // }); // 2. 绘制路线 // const path = points.map(p => [p.lng, p.lat]); // new AMap.Polyline({ // path: path, // strokeColor: "#32CD32", // strokeWeight: 5, // map: map // }); // 3. 添加起点终点标记 // points.forEach((point, i) => { // new AMap.Marker({ // position: [point.lng, point.lat], // // content: ${i === 0 ? '起' : '终'}, // offset: new AMap.Pixel(-10, -10), // map: map // }); // }); // 4. 绘制驾车路径(如果需要,可以注释掉直线路径,保留驾车路径) // 注意:这里同时绘制了直线和驾车路径,可能会重叠 // if (points.length >= 2) { // AMap.plugin('AMap.Driving', () => { // const driving = new AMap.Driving({ // map: map, // 这样驾车路线会直接显示在地图上 // policy: AMap.DrivingPolicy.LEAST_TIME // }); // driving.search( // [points[0].lng, points[0].lat], // [points[points.length - 1].lng, points[points.length - 1].lat], // (status, result) => { // // 可以根据结果处理 // } // ); // }); // } // 1. 绘制路线 // if (points.length > 0) { // const path = points.map(p => [p.lng, p.lat]); // new AMap.Polyline({ // path: path, // strokeColor: "#32CD32", // strokeWeight: 5, // map: map // }); // // 添加起点终点标记 // points.forEach((point, i) => { // new AMap.Marker({ // position: [point.lng, point.lat], // offset: new AMap.Pixel(-10, -10), // map: map // }); // }); // // 绘制驾车路径(如果需要) // if (points.length >= 2) { // AMap.plugin('AMap.Driving', () => { // const driving = new AMap.Driving({ // map: map, // policy: AMap.DrivingPolicy.LEAST_TIME // }); // driving.search( // [points[0].lng, points[0].lat], // [points[points.length - 1].lng, points[points.length - 1].lat] // ); // }); // } // } </script> </body> </html>

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width,initial-scale=1.0"> <title>高德地图</title> <script src="https://webapi.amap.com/loader.js"></script> <script src="https://cdn.bootcss.com/vue/2.6.11/vue.js"></script> <style> #map-container { width: 100%; height: 584px; border-radius: 22px; z-index: 4; } </style> </head> <body> <script type="text/javascript"> window._AMapSecurityConfig = { securityJsCode: 'cf331d54f67e306ca5c0b17f7122d559' }; </script> <script src="https://webapi.amap.com/maps?v=2.0&key=f3323c24769b10828db07f07c0fcec0d&plugin=AMap.MarkerCluster"> </script> <script> // 解析URL参数 const params = new URLSearchParams(location.search); const points = JSON.parse(params.get('points')); const markers = JSON.parse(params.get('markers')); // 初始化地图 const map = new AMap.Map('map-container', { zoom: 10, center: [points[0].lng, points[0].lat], pitch: 45, showBuildingBlock: true }); markers.forEach(marker => { new AMap.Marker({ position: [marker.lng, marker.lat], title: marker.title, map: map }).on('click', () => { // 向UniApp发送点击事件 window.parent.postMessage({ type: 'markerClick', title: marker.title }, '*'); }); }); //构建带标题的HTML内容 markers.forEach(marker => { // 构建HTML内容(包含图片和标题) const content = ${marker.title} ; const markerObj = new AMap.Marker({ position: [marker.lng, marker.lat], content: content, // 使用HTML内容 anchor: 'bottom-center', // 锚点在底部中心 offset: new AMap.Pixel(0, -25) // 向上偏移 }); markerObj.on('click', () => { console.log(marker, '111'); // 将数据转为字符串 const message = JSON.stringify({ type: 'markerClick', title: marker.title }); window.parent.postMessage(message, '*'); }); map.add(markerObj); }); // 准备聚合数据 const clusterPoints = markers.map(marker => ({ lnglat: [marker.lng, marker.lat], title: marker.title, originalData: marker // 保留原始数据 })); // 点聚合初始化函数 function initCluster() { const cluster = new AMap.MarkerCluster(map, clusterPoints, { gridSize: 80, maxZoom: 15, minClusterSize: 2, renderClusterMarker: function(context) { const count = context.count; // 根据聚合点数量计算大小 const div = document.createElement('div'); div.className = 'cluster-marker'; div.style.backgroundColor = 'rgba(255,153,0,0.6)'; div.style.border = '1px solid rgba(255,153,0,1)'; div.style.borderRadius = '50%'; div.style.color = '#fff'; div.style.textAlign = 'center'; div.style.lineHeight = '40px'; div.style.width = '40px'; div.style.height = '40px'; div.innerHTML = count; return div; }, // 1. 添加标记点 renderMarker: function(context) { const markerData = context.data[0].originalData; // console.log('markerData.title', markerData.title); const content = ${markerData.title} ; // console.log('markerDatat', markerData); const markerObj = new AMap.Marker({ position: [markerData.lng, markerData.lat], content: content, anchor: 'bottom-center', offset: new AMap.Pixel(0, -25) }); markerObj.on('click', function() { window.parent.postMessage({ type: 'markerClick', title: markerData.title }, '*'); }); return markerObj; } }); } // 加载点聚合插件 AMap.plugin('AMap.MarkerCluster', function() { initCluster(); // 插件加载完成后执行初始化 }); // 2. 绘制路线 const path = points.map(p => [p.lng, p.lat]); new AMap.Polyline({ path: path, strokeColor: "#3366FF", strokeWeight: 5, map: map }); // 3. 添加起点终点标记 points.forEach((point, i) => { new AMap.Marker({ position: [point.lng, point.lat], content: ${i === 0 ? '起' : '终'}, offset: new AMap.Pixel(-10, -10), map: map }); }); // 4. 绘制驾车路径(如果需要,可以注释掉直线路径,保留驾车路径) // 注意:这里同时绘制了直线和驾车路径,可能会重叠 if (points.length >= 2) { AMap.plugin('AMap.Driving', () => { const driving = new AMap.Driving({ map: map, // 这样驾车路线会直接显示在地图上 policy: AMap.DrivingPolicy.LEAST_TIME }); driving.search( [points[0].lng, points[0].lat], [points[points.length - 1].lng, points[points.length - 1].lat], (status, result) => { // 可以根据结果处理 } ); }); } </script> </body> </html>uniapp的vue2项目使用webview嵌套高德地图,地图聚合点无法正常显示,都是只有默认图标,还有地图点击事件,无法正常回传给 <view class="description-card"> <web-view :src="mapUrl" @message="handleMessage"></web-view> </view>// 接收WebView消息 handleMessage(e) { console.log(e, '点击了标记点'); // e.detail.data 是一个数组,数组的每个元素是H5发送的消息(字符串) const [message] = e.detail.data; try { const data = JSON.parse(message); if (data.type === 'markerClick') { // 处理标记点击 } } catch (e) { console.error('解析消息失败', e); } },

最新推荐

recommend-type

三菱FX3U三轴伺服电机与威纶通触摸屏组合程序详解:轴点动、回零与定位控制及全流程解析

三菱FX3U三轴伺服电机与威纶通触摸屏的程序编写方法及其应用。主要内容涵盖伺服电机主控程序、触摸屏程序、轴点动、回零及定位程序、通讯模块程序以及威纶显示器程序的分析。通过对各个模块的深入探讨,帮助读者理解每个部分的功能和实现方式,确保机械运动控制的准确性、高效性和稳定性。此外,文章还提供了关于程序编写过程中可能遇到的问题及解决方案。 适合人群:从事自动化控制领域的工程师和技术人员,尤其是对三菱FX3U三轴伺服电机和威纶通触摸屏有实际操作需求的专业人士。 使用场景及目标:适用于工业自动化项目中,旨在提高对三菱FX3U三轴伺服电机和威纶通触摸屏的理解和应用能力,掌握模块化编程技巧,解决实际工程中的编程难题。 其他说明:文中不仅讲解了各模块的具体实现细节,还强调了程序的安全性和可靠性,为项目的成功实施提供了有力的支持。
recommend-type

职业介绍与人才招聘综合管理系统-基于宏达数据库信息管理开发平台的专业人力资源服务软件-包含基本信息设置-用人单位管理-求职人员登记-数据查询-统计分析-报表生成-打印输出-权限控制.zip

cursor免费次数用完职业介绍与人才招聘综合管理系统_基于宏达数据库信息管理开发平台的专业人力资源服务软件_包含基本信息设置_用人单位管理_求职人员登记_数据查询_统计分析_报表生成_打印输出_权限控制.zip
recommend-type

基于Spark2x分布式计算框架的实时新闻大数据分析可视化系统-实现用户浏览日志采集与实时处理-新闻话题热度排名统计-时段流量峰值分析-新闻曝光量监控-数据可视化展示-采用Kaf.zip

基于Spark2x分布式计算框架的实时新闻大数据分析可视化系统_实现用户浏览日志采集与实时处理_新闻话题热度排名统计_时段流量峰值分析_新闻曝光量监控_数据可视化展示_采用Kaf.zip大数据实战项目
recommend-type

基于springboot小型哺乳类宠物诊所管理系统-4339s0c8【附万字论文+PPT+包部署+录制讲解视频】.zip

基于springboot小型哺乳类宠物诊所管理系统-4339s0c8【附万字论文+PPT+包部署+录制讲解视频】.zip
recommend-type

基于Simulink的风电永磁同步电机并网系统仿真模型与SVPWM控制机制探究

基于Simulink/Matlab构建的风电永磁同步电机并网系统的仿真模型。该模型主要涵盖了SVPWM控制、MPPT风能跟踪算法以及Crowbar电路的低压穿越功能。文中首先解释了机侧变流器的工作原理及其核心——MPPT算法的具体实现方法,采用了黄金分割法进行最大功率点跟踪,并提供了相应的Matlab函数代码。接着讨论了网侧变流器的电网电压定向控制和SVPWM模块的应用,强调了载波频率设置和死区补偿的重要性。对于Crowbar电路部分,则着重讲述了其触发逻辑和保护机制,确保在电网电压骤降时能够稳定运行。此外,还分享了一些仿真设置的小技巧,如选择合适的求解器和优化参数的方法。 适合人群:从事风电系统研究的技术人员、高校相关专业师生、对电力电子控制系统感兴趣的工程技术人员。 使用场景及目标:①为风电并网仿真提供可靠的模型支持;②深入理解SVPWM控制、MPPT算法和Crowbar电路的功能;③掌握风电系统关键组件的设计与优化方法。 其他说明:本文不仅提供了详细的理论解析和技术细节,还附带了具体的代码片段,便于读者实际操作和验证。
recommend-type

Pansophica开源项目:智能Web搜索代理的探索

Pansophica开源项目是一个相对较新且具有创新性的智能Web搜索代理,它突破了传统搜索引擎的界限,提供了一种全新的交互方式。首先,我们来探讨“智能Web搜索代理”这一概念。智能Web搜索代理是一个软件程序或服务,它可以根据用户的查询自动执行Web搜索,并尝试根据用户的兴趣、历史搜索记录或其他输入来提供个性化的搜索结果。 Pansophica所代表的不仅仅是搜索结果的展示,它还强调了一个交互式的体验,在动态和交互式虚拟现实中呈现搜索结果。这种呈现方式与现有的搜索体验有着根本的不同。目前的搜索引擎,如Google、Bing和Baidu等,多以静态文本和链接列表的形式展示结果。而Pansophica通过提供一个虚拟现实环境,使得搜索者可以“扭转”视角,进行“飞行”探索,以及“弹网”来浏览不同的内容。这种多维度的交互方式使得信息的浏览变得更加快速和直观,有望改变用户与网络信息互动的方式。 接着,我们关注Pansophica的“开源”属性。所谓开源,指的是软件的源代码可以被公众获取,任何个人或组织都可以自由地使用、学习、修改和分发这些代码。开源软件通常由社区进行开发和维护,这样的模式鼓励了协作创新并减少了重复性劳动,因为全世界的开发者都可以贡献自己的力量。Pansophica项目作为开源软件,意味着其他开发者可以访问和使用其源代码,进一步改进和扩展其功能,甚至可以为Pansophica构建新的应用或服务。 最后,文件名称“Pansophica-src-1.3”表明了我们讨论的特定版本的Pansophica开源代码。数字“1.3”很可能指的是该版本号,表明这是Pansophica项目的第1.3个公开版本。这个版本号对于开发者来说很重要,因为它可以帮助他们追踪更新的进度、比较版本间的差异以及选择最适合其项目的版本。 综合以上信息,Pansophica开源项目不仅在搜索代理领域引入了创新的交互方式,而且通过开源的方式,提供了技术共享和协作的可能性。对于IT行业来说,开源项目是推动技术发展的重要力量,它激发了社区的活力,促进了知识的传播和技术的进步。通过构建一个开源社区,Pansophica可以吸引来自世界各地的开发者和研究人员,共同探索如何将搜索技术与交互式虚拟现实技术结合起来,为用户带来更加丰富和有效的信息检索体验。同时,Pansophica的开源性质也使其更容易适应未来技术的发展,随着社区的不断贡献和反馈,这一平台有望在功能和性能上得到持续的优化和增强。
recommend-type

跨平台内容提取无忧:coze工作流应对社交媒体挑战

# 1. 社交媒体内容提取的必要性与挑战 ## 1.1 内容提取的必要性 在数字时代,社交媒体内容是信息与数据流动的主干道,公司、个人和研究者都在寻求从这些信息中提取价值。无论是为了市场趋势分析、品牌监控、消费者洞察还是公共舆论监测,有效的社交媒体内容提取变得至关重要。这不仅有助于企业做出数据驱动的决策,也能使个人更好地理
recommend-type

vrrp主设备发送的免费arp

<think>好的,我现在需要回答用户关于VRRP主设备发送免费ARP的目的和工作原理的问题。首先,我得回忆一下VRRP的基本概念和工作机制。VRRP,虚拟路由冗余协议,主要用于在局域网中提供默认网关的冗余备份,确保网络的高可用性。主设备(Master)负责转发流量,而备用设备(Backup)则在主设备故障时接管工作。 用户的问题集中在主设备发送免费ARP的目的和机制上。根据引用[2],免费ARP用于通知下游设备虚拟MAC地址的变更。当主设备被选举出来后,它需要让局域网内的其他设备知道虚拟IP对应的MAC地址已经指向自己,这样流量才会被正确路由到主设备。免费ARP的作用应该就是更新这些设备的
recommend-type

为Ghost博客平台打造的Meteor流星包装使用指南

从给定文件信息中,我们可以提炼出以下IT知识点: ### 标题知识点:流星Ghost软件包 1. **流星Ghost软件包的用途**:流星Ghost软件包是专为Ghost博客平台设计的流星(Meteor)应用程序。流星是一个开源的全栈JavaScript平台,用于开发高性能和易于编写的Web应用程序。Ghost是一个开源博客平台,它提供了一个简单且专业的写作环境。 2. **软件包的作用**:流星Ghost软件包允许用户在流星平台上轻松集成Ghost博客。这样做的好处是可以利用流星的实时特性以及易于开发和部署的应用程序框架,同时还能享受到Ghost博客系统的便利和美观。 ### 描述知识点:流星Ghost软件包的使用方法 1. **软件包安装方式**:用户可以通过流星的命令行工具添加名为`mrt:ghost`的软件包。`mrt`是流星的一个命令行工具,用于添加、管理以及配置软件包。 2. **初始化Ghost服务器**:描述中提供了如何在服务器启动时运行Ghost的基本代码示例。这段代码使用了JavaScript的Promise异步操作,`ghost().then(function (ghostServer) {...})`这行代码表示当Ghost服务器初始化完成后,会在Promise的回调函数中提供一个Ghost服务器实例。 3. **配置Ghost博客**:在`then`方法中,首先会获取到Ghost服务器的配置对象`config`,用户可以在此处进行自定义设置,例如修改主题、配置等。 4. **启动Ghost服务器**:在配置完成之后,通过调用`ghostServer.start()`来启动Ghost服务,使其能够处理博客相关的请求。 5. **Web浏览器导航**:一旦流星服务器启动并运行,用户便可以通过Web浏览器访问Ghost博客平台。 ### 标签知识点:JavaScript 1. **JavaScript作为流星Ghost软件包的开发语言**:标签指出流星Ghost软件包是使用JavaScript语言开发的。JavaScript是一种在浏览器端广泛使用的脚本语言,它也是流星平台的基础编程语言。 2. **流星和Ghost共同使用的语言**:JavaScript同样也是Ghost博客平台的开发语言。这表明流星Ghost软件包可以无缝集成,因为底层技术栈相同。 ### 压缩包子文件的文件名称列表知识点:meteor-ghost-master 1. **版本控制和软件包结构**:文件名称`meteor-ghost-master`暗示了该软件包可能托管在像GitHub这样的版本控制系统上。文件名中的`master`通常指的是主分支或主版本。 2. **软件包的目录结构**:通过文件名称可以推断出该软件包可能拥有一个标准的流星软件包结构,包含了初始化、配置、运行等必要的模块和文件。 3. **软件包的维护状态**:由于文件名没有包含特定的版本号,我们无法直接得知软件包的最新更新情况。通常,软件包维护者会将最新的版本代码放在`master`分支上。 ### 总结 流星Ghost软件包提供了一个有效的解决方案,使得流星平台的开发者能够在他们的应用中添加Ghost博客功能。软件包的使用简便,通过流星的命令行工具安装,并通过JavaScript代码配置和启动Ghost服务。通过流星Ghost软件包,开发者能够享受流星的实时特性以及Ghost博客系统的便利性。此外,软件包的命名和结构也暗示了其维护和版本控制的模式,有助于开发者更好地理解如何使用和维护这一软件包。
recommend-type

抖音标题生成自动化:用coze工作流释放创意

# 1. 抖音标题生成自动化的重要性 随着社交媒体平台的崛起,内容的吸引力很大程度上取决于标题的创意与精准性。抖音作为一个日活亿级的短视频平台,高质量的标题能够有效提高视频的点击率,增加内容的传播。但是,人工撰写标题不仅耗时耗力,而且很难做到快速响应热点,自动化标题生成工具应运而生。coze工作流,作为一种实现自动化生成抖音标题的工具,其重要性不言而喻。它能够利用大数据分析和机器学习技术,提高标题的吸引