腾讯地图点击地图创建锚点(且只创建一个)同事创建锚点提示

本文转载于:猿2048网站https://www.mk2048.com/blog/blog.php?id=hbccb&title=%E8%85%BE%E8%AE%AF%E5%9C%B0%E5%9B%BE%E7%82%B9%E5%87%BB%E5%9C%B0%E5%9B%BE%E5%88%9B%E5%BB%BA%E9%94%9A%E7%82%B9%EF%BC%88%E4%B8%94%E5%8F%AA%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%EF%BC%89%E5%90%8C%E4%BA%8B%E5%88%9B%E5%BB%BA%E9%94%9A%E7%82%B9%E6%8F%90%E7%A4%BA

 1 <!DOCTYPE html>
 2 <html>
 3 <head>
 4     <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
 5 <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"/>
 6 <title>传递事件参数</title>
 7 <style type="text/css">
 8 *{
 9     margin:0px;
10     padding:0px;
11 }
12 body, button, input, select, textarea {
13     font: 12px/16px Verdana, Helvetica, Arial, sans-serif;
14 }
15 p{
16     width:603px;
17     padding-top:3px;
18     overflow:hidden;
19 }
20 #container{
21     min-width:600px;
22     min-height:767px;
23 }
24 </style>
25 <script charset="utf-8" src="https://map.qq.com/api/js?v=2.exp"></script>
26 <script>
27 function init() {
28     var map = new qq.maps.Map(document.getElementById("container"),{
29         center: new qq.maps.LatLng(39.916527,116.397128),
30         zoom: 13
31     });
32     //添加监听事件   获取鼠标单击事件
33     qq.maps.event.addListener(map, 'click', function(event) {
34        var marker=new qq.maps.Marker({
35          position:event.latLng, 
36          map:map
37        });          
38       //添加到提示窗
39       var info = new qq.maps.InfoWindow({
40         map: map
41       });      
42       //获取标记的点击事件
43       qq.maps.event.addListener(marker, 'click', function() {
44         info.open(); 
45         info.setContent('<div style="text-align:center;white-space:nowrap;'+
46                         'margin:10px;">单击标记</div>');
47         info.setPosition(new qq.maps.LatLng(event.latLng.getLat(),event.latLng.getLng())); 
48       });
49       qq.maps.event.addListener(map, 'click', function(event) {
50         marker.setMap(null);   
51         info.close();
52       });      
53    });
54 }
55 </script>
56 </head>
57 <body οnlοad="init()">
58 <div id="container"></div>
59 <p>给地图绑定了点击事件,当点击地图时,会在点击位置显示Marker。</p>
60 </body>
61 </html>

 

本文转载于:https://www.mk2048.com/blog/blog.php?id=hbccb&title=%E8%85%BE%E8%AE%AF%E5%9C%B0%E5%9B%BE%E7%82%B9%E5%87%BB%E5%9C%B0%E5%9B%BE%E5%88%9B%E5%BB%BA%E9%94%9A%E7%82%B9%EF%BC%88%E4%B8%94%E5%8F%AA%E5%88%9B%E5%BB%BA%E4%B8%80%E4%B8%AA%EF%BC%89%E5%90%8C%E4%BA%8B%E5%88%9B%E5%BB%BA%E9%94%9A%E7%82%B9%E6%8F%90%E7%A4%BA

转载于:https://my.oschina.net/u/4181724/blog/3085427

在 G6 中,一个节点只能创建一个锚点,但是可以通过在节点周围添加多个辅助节点来实现类似于一个节点拥有多个锚点的效果。具体实现方法如下: 1. 创建一个主节点 2. 创建多个辅助节点,位置固定在主节点周围,可以通过计算角度和距离来确定其位置 3. 将辅助节点与主节点通过连线连接起来,并设置辅助节点为不可见 4. 将主节点和辅助节点的 id 存储在一个数组中,通过数组来控制节点和锚点的位置和连接关系 示例代码如下: ```javascript const node = graph.addNode({ id: 'node', label: 'Node', x: 100, y: 100, size: 80, anchorPoints: [ [0.5, 0], // 主节点锚点 [0, 0.5], // 辅助节点1锚点 [1, 0.5], // 辅助节点2锚点 ], }); const anchorIds = ['node', 'assist1', 'assist2']; const assist1 = graph.addNode({ id: 'assist1', visible: false, x: 100 + 80 * Math.cos(Math.PI / 4), y: 100 - 80 * Math.sin(Math.PI / 4), }); const assist2 = graph.addNode({ id: 'assist2', visible: false, x: 100 + 80 * Math.cos((3 * Math.PI) / 4), y: 100 - 80 * Math.sin((3 * Math.PI) / 4), }); graph.addEdges([ { source: 'node', target: 'assist1', id: 'edge1', }, { source: 'node', target: 'assist2', id: 'edge2', }, ]); graph.on('anchorpoint:change', (e) => { const { item, anchorIndex } = e; const anchorId = anchorIds[anchorIndex]; if (anchorId === 'node') { return; } const assistNode = graph.findById(anchorId); const assistModel = assistNode.getModel(); const nodeModel = item.getModel(); const assistAnchor = assistModel.anchorPoints[0]; const nodeAnchor = nodeModel.anchorPoints[anchorIndex]; const dx = nodeModel.x - assistModel.x; const dy = nodeModel.y - assistModel.y; const angle = Math.atan2(dy, dx); const distance = Math.sqrt(dx * dx + dy * dy); const assistX = nodeModel.x - distance * assistAnchor[0] * Math.cos(angle); const assistY = nodeModel.y - distance * assistAnchor[0] * Math.sin(angle); assistNode.updatePosition(assistX, assistY); assistNode.updateAnchor(0, assistAnchor); item.updateAnchor(anchorIndex, nodeAnchor); }); ``` 在上述代码中,我们创建了一个主节点和两个辅助节点,并将辅助节点设置为不可见。然后,我们通过连线将主节点和辅助节点连接起来,并设置主节点和辅助节点的锚点。在锚点位置改变时,我们通过计算辅助节点的位置来维护节点和锚点的连接关系。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值