【问题标题】:Rotate Leaflet markers tooltip text旋转传单标记工具提示文本
【发布时间】:2018-06-04 22:58:39
【问题描述】:

我有一些 JSON 数据,应该在地图上显示。

var shapesCoordinates = '[{"shapeId": 1,"coordinates": [-1500,500],"text": "bla bla", "rotation": 20},{"shapeId": 2,"coordinates": [-1800,800],"text": "idemooooo", "rotation": 60}]';
var shapess = JSON.parse(shapesCoordinates);

var markers = [];
for (var i = 0; i < shapess.length; i++) {
    var marker = L.marker(shapess[i]['coordinates'], {
        opacity: 0.01
    }).bindTooltip(shapess[i]['text'],
        {
            permanent: true,
            className: "shapesText",
            offset: [0, 0],
            direction: "center"
        }
    ).openTooltip().addTo(mymap);

    markers[shapess[i]['shapeId']] = marker;
}

我尝试使用 CSS 旋转文本,但出现了一些问题。

     .leaflet-tooltip {
            box-shadow: none;
            /** 
              This rotates text but break marker position
               set all markers on same (default) position
            */
            transform: rotate(45deg) !important;
        }

    .shapesText {
        position: absolute;
        /**
          This has no impact on text
        */
        transform: rotate(45deg) !important;
        font-size:14px;
        background: none;
        border: none
    }

我知道为什么会出现这个问题。

生成的 HTML 代码具有 transform: translate3d(),当我使用 transform: rotate()` 时,它会覆盖元素重新定位。如何同时使用两个属性值?换句话说,如何在不覆盖 translate3d 的情况下添加旋转

如果需要,是否有一些 Leaflet 的方法可以为每个标记设置不同的旋转?

我只是使用 Leaflet 来显示自定义的非地理地图,有时需要显示会遵循某些形状边界的文本。

这是生成的 HTML 代码

<div class="leaflet-tooltip shapesText leaflet-zoom-animated leaflet-tooltip-center" style="opacity: 0.9; transform: translate3d(385px, -32px, 0px);">bla bla</div>

【问题讨论】:

  • 我们可以看到其中一个工具提示的 HTML 吗?
  • 没有HTML!标记工具提示是使用 bindTooltip() 方法动态生成的。
  • 任何bindTooltip() 都不会为 DOM 生成任何 HTML,不是吗?
  • 我更新了问题,生成了 HTML 内容。
  • 所以,我猜,那个变换:rotate(...),覆盖变换:translate3d(...)

标签: javascript css rotation leaflet


【解决方案1】:

我需要玩一个包含当前工具提示内容的游戏。

var shapess = JSON.parse(shapesCoordinates);

var markers = [];
for (var i = 0; i < shapess.length; i++) {
    var marker = L.marker(shapess[i]['coordinates'], {
        opacity: 0.01
    }).bindTooltip(shapess[i]['text'],
        {
            permanent: true,
            className: "shapesText" + i,
            offset: [0, 0],
            direction: "center"
        }
    ).openTooltip().addTo(mymap);

我需要访问标记工具提示并更改其 HTML 代码,以便用当前 translate3d 值替换旧的 transform 属性并添加虚拟 @ 987654325@ 值。 虚拟的原因是添加的旋转不起作用。

    if (shapess[i]["rotation"]) {
        var content = marker['_tooltip']['_container'];
        var style = $(content).attr('style');
        var transformProperty = style.substring(style.indexOf('transform'));
        var transformRotationProperty = transformProperty.replace(';', ' rotation(' + shapess[i]["rotation"] + 'deg);');
        var changedContent = content.outerHTML.replace(transformProperty, transformRotationProperty);

        marker['_tooltip'].setContent(changedContent);
        $('.shapesText' + i).css({'transform': 'rotate(' + shapess[i]["rotation"] + 'deg)'});
    }
    markers[shapess[i]['shapeId']] = marker;

}

将新内容添加到工具提示后,我可以访问工具提示类并通过仅添加 rotate 值来更改 transform 属性。 它将保留旧的 translate 值而不被覆盖。

我不确定这是一个错误还是什么问题,但如果我只添加 rotate 而没有 translate3d 旧值将被覆盖.

但是,如果旧的 transform 同时具有 translate3drotate 使用仅旋转的 CSS 变换不会覆盖旧的 translate3d

【讨论】:

  • 对不起,今天早上我来早了!这不是 CSS3 中的错误。我已经意识到变换是一种速记,必须组合值(旋转、翻译等)。您在正确的路线上,基本上是复制当前位置,然后将您的旋转注入内联变换。 codepen.io/ahdigital/pen/OzRBeW?editors=0011
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-25
  • 1970-01-01
  • 1970-01-01
  • 2014-03-18
  • 1970-01-01
  • 2020-03-29
相关资源
最近更新 更多