javascript - Leaflet Polyline - 添加的 SVG 文本元素不可见

标签 javascript svg leaflet

我想为传单创建一个测量工具。因此我正在编写一个插件,扩展折线功能。在插件中,我将 SVG 文本路径元素添加到图层组中。我的问题是添加的元素在 map 上不可见。我尝试重绘图层,但这对添加元素的可见性没有影响。

Here's我的 fiddle 。

(function () {
    var __onAdd = L.Polyline.prototype.onAdd,
        __onRemove = L.Polyline.prototype.onRemove,
        __updatePath = L.Polyline.prototype._updatePath,
        __bringToFront = L.Polyline.prototype.bringToFront;

    L.Polyline.include({
      onAdd: function (map) {
          __onAdd.call(this, map);
          this._textRedraw();
      },

      onRemove: function (map) {
          __onRemove.call(this, map);
      },

      bringToFront: function () {
          __bringToFront.call(this);
          this._textRedraw();
      },

      setText: function () {
            var path = this._path,
                points = this.getLatLngs(),
                pathSeg,
                prevPathSeg,
                center,
                angle,
                rotation,
                textNode;

          /* 
           * If not in SVG mode or Polyline not added to map yet return
           * setText will be called by onAdd, using value stored in this._text
           */
          if (!L.Browser.svg || typeof this._map === 'undefined') {
              return this;
          }

          for (pathSeg = 0; pathSeg < path.pathSegList.length; pathSeg += 1) {
                if (pathSeg > 0) {
                    prevPathSeg = path.pathSegList[pathSeg - 1];
                  center = this._calcCenter(
                      prevPathSeg.x,
                      prevPathSeg.y,
                      path.pathSegList[pathSeg].x,
                      path.pathSegList[pathSeg].y
                  );                  
                  angle = this._calcAngle(
                          prevPathSeg.x,
                      prevPathSeg.y,
                      path.pathSegList[pathSeg].x,
                      path.pathSegList[pathSeg].y
                  );
                  rotation = 'rotate(' + angle + ' ' + 
                        center.x + ',' + center.y + ')';

                  textNode = document.createElement("text");
                  textNode.setAttribute('text-anchor', 'middle');
                  textNode.setAttribute('x', center.x);
                  textNode.setAttribute('y', center.y);
                  textNode.setAttribute('transform', rotation);
                  textNode.textContent = points[pathSeg - 1]
                        .distanceTo(points[pathSeg]);

                  this._path.parentElement.appendChild(textNode);
              } else {
                    continue;
              }
          }
      },

      _calcCenter: function (x1, y1, x2, y2) {
            return {
            x: (x1 + x2) / 2,
            y: (y1 + y2) / 2
          }
      },

      _calcAngle: function (x1, y1, x2, y2) {
              return Math.atan2(y2 - y1, x2 - x1) * 180 / Math.PI;
      },

      _textRedraw: function () {
            var textNodes = this._path.parentElement.getElementsByTagName('text'),
                tnIndex;

                    if (textNodes.length > 0) {
                for (tnIndex = textNodes.length - 1; tnIndex >= 0; tnIndex -= 1) {
                    textNodes[tnIndex].parentNode.removeChild(textNodes[tnIndex]);
              }
          }

          if (this.options.measurements) {
              this.setText();
          }
      },

      _updatePath: function () {
          __updatePath.call(this);
          this._textRedraw();
      }
  });
})();

最佳答案

您需要使用 createElementNS 创建一个 SVG 命名空间元素:

Creates an element with the specified namespace URI and qualified name.

https://developer.mozilla.org/en-US/docs/Web/API/Document/createElementNS

开关:

textNode = document.createElement("text");

至:

textNode = document.createElementNS("http://www.w3.org/2000/svg", "text");

示例:http://jsfiddle.net/w8yz18rf/1/

关于javascript - Leaflet Polyline - 添加的 SVG 文本元素不可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34338148/

相关文章:

Javascript:有效地将项目移入和移出固定大小的数组

javascript - Bootstrap 选择菜单 - 添加新选项

javascript - 如何使用 JSX 'collected' 将 React 元素上的属性定义为单个对象?

leaflet - wkhtmltopdf和传单等待 map

javascript - 传单 Angular Directive(指令)中的 KML

javascript - 创建新的 DOMParser 实例有什么意义?

javascript - 更新过渡中的轴样式

javascript - 长度未知的文本对齐方式

html - 如何制作在屏幕尺寸改变时完全改变的响应式 Logo ?

Leaflet.js 圆标记过渡