• <small id='0HfYF'></small><noframes id='0HfYF'>

    <i id='0HfYF'><tr id='0HfYF'><dt id='0HfYF'><q id='0HfYF'><span id='0HfYF'><b id='0HfYF'><form id='0HfYF'><ins id='0HfYF'></ins><ul id='0HfYF'></ul><sub id='0HfYF'></sub></form><legend id='0HfYF'></legend><bdo id='0HfYF'><pre id='0HfYF'><center id='0HfYF'></center></pre></bdo></b><th id='0HfYF'></th></span></q></dt></tr></i><div id='0HfYF'><tfoot id='0HfYF'></tfoot><dl id='0HfYF'><fieldset id='0HfYF'></fieldset></dl></div>
    • <bdo id='0HfYF'></bdo><ul id='0HfYF'></ul>
  • <tfoot id='0HfYF'></tfoot>

    <legend id='0HfYF'><style id='0HfYF'><dir id='0HfYF'><q id='0HfYF'></q></dir></style></legend>

      1. 如何将要在地图上显示的文本添加到传单中的

        时间:2023-08-08

          • <bdo id='Ar2Yt'></bdo><ul id='Ar2Yt'></ul>

          • <legend id='Ar2Yt'><style id='Ar2Yt'><dir id='Ar2Yt'><q id='Ar2Yt'></q></dir></style></legend>
              <i id='Ar2Yt'><tr id='Ar2Yt'><dt id='Ar2Yt'><q id='Ar2Yt'><span id='Ar2Yt'><b id='Ar2Yt'><form id='Ar2Yt'><ins id='Ar2Yt'></ins><ul id='Ar2Yt'></ul><sub id='Ar2Yt'></sub></form><legend id='Ar2Yt'></legend><bdo id='Ar2Yt'><pre id='Ar2Yt'><center id='Ar2Yt'></center></pre></bdo></b><th id='Ar2Yt'></th></span></q></dt></tr></i><div id='Ar2Yt'><tfoot id='Ar2Yt'></tfoot><dl id='Ar2Yt'><fieldset id='Ar2Yt'></fieldset></dl></div>
                <tbody id='Ar2Yt'></tbody>

                <tfoot id='Ar2Yt'></tfoot>

                <small id='Ar2Yt'></small><noframes id='Ar2Yt'>

                  本文介绍了如何将要在地图上显示的文本添加到传单中的 geojson 对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  所以我在传单中有一个 geojson 图层,我可以将 geojson 对象添加到该图层以显示在生成的地图上.

                  So I have a geojson layer in leaflet, and I can add geojson objects to this layer for display on the resulting map.

                  现在我想添加一个文本标签以显示在对象附近.

                  Now I'd like to add a text label to display near the object.

                  本示例展示了使用自定义 L.control() 对象在地图上显示附加信息.这似乎接近我想做的事情.

                  This example shows use of a custom L.control() object to display additional info on the map. Which seems close to what I want to do.

                  鉴于此示例,我想在每个状态上添加状态初始文本标签(即TX"、FL").L.control() 可以用来做这个吗,还是有别的方法?

                  Given this example, I'd like to add State initial text labels (i.e. "TX", "FL") positioned over each state. Can L.control() be used to do this, or is there another way?

                  http://leaflet.cloudmade.com/examples/choropleth.html

                  var info = L.control();
                  
                  info.onAdd = function (map) {
                      this._div = L.DomUtil.create('div', 'info'); // create a div with a class "info"
                      this.update();
                      return this._div;
                  };
                  
                  // method that we will use to update the control based on feature properties passed
                  info.update = function (props) {
                      this._div.innerHTML = '<h4>US Population Density</h4>' +  (props ?
                          '<b>' + props.name + '</b><br />' + props.density + ' people / mi<sup>2</sup>'
                          : 'Hover over a state');
                  };
                  
                  info.addTo(map);
                  

                  推荐答案

                  我最近也在找同样的问题,昨天刚刚根据google群里的帖子实现了.https://groups.google.com/forum/#!topic/leaflet-js/sA2HnU5W9Fw

                  I was looking for the same question recently and just implemented it yesterday based on a posting in the google group. https://groups.google.com/forum/#!topic/leaflet-js/sA2HnU5W9Fw

                  感谢 Adrian 提供原始代码示例.

                  Thanks to Adrian for the original code sample.

                  解决办法如下:

                  扩展如下类:

                  <script>
                  
                      L.LabelOverlay = L.Class.extend({
                          initialize: function(/*LatLng*/ latLng, /*String*/ label, options) {
                              this._latlng = latLng;
                              this._label = label;
                              L.Util.setOptions(this, options);
                          },
                          options: {
                              offset: new L.Point(0, 2)
                          },
                          onAdd: function(map) {
                              this._map = map;
                              if (!this._container) {
                                  this._initLayout();
                              }
                              map.getPanes().overlayPane.appendChild(this._container);
                              this._container.innerHTML = this._label;
                              map.on('viewreset', this._reset, this);
                              this._reset();
                          },
                          onRemove: function(map) {
                              map.getPanes().overlayPane.removeChild(this._container);
                              map.off('viewreset', this._reset, this);
                          },
                          _reset: function() {
                              var pos = this._map.latLngToLayerPoint(this._latlng);
                              var op = new L.Point(pos.x + this.options.offset.x, pos.y - this.options.offset.y);
                              L.DomUtil.setPosition(this._container, op);
                          },
                          _initLayout: function() {
                              this._container = L.DomUtil.create('div', 'leaflet-label-overlay');
                          }
                      });   
                  
                  </script>
                  

                  另外添加这个css:

                  <style>
                      .leaflet-popup-close-button {
                          display:none;
                      }
                  
                      .leaflet-label-overlay {
                          line-height:0px;
                          margin-top: 9px;
                          position:absolute;
                      }
                  </style>
                  

                  然后显示文本标签如下:

                  And then display the text labels as below:

                  <script>
                      var map = L.map('map').setView([51.898712, 6.7307100000001], 4);
                  
                      // add markers
                      // ...
                  
                      // add text labels:
                      var labelLocation = new L.LatLng(51.329219337279405, 10.454119349999928);
                      var labelTitle = new L.LabelOverlay(labelLocation, '<b>GERMANY</b>');
                      map.addLayer(labelTitle);
                  
                  
                      var labelLocation2 = new L.LatLng(47.71329162782909, 13.34573480000006);
                      var labelTitle2 = new L.LabelOverlay(labelLocation2, '<b>AUSTRIA</b>');
                      map.addLayer(labelTitle2);
                  
                      // In order to prevent the text labels to "jump" when zooming in and out,
                      // in Google Chrome, I added this event handler:
                  
                      map.on('movestart', function () {
                          map.removeLayer(labelTitle);
                          map.removeLayer(labelTitle2);
                      });
                      map.on('moveend', function () {
                          map.addLayer(labelTitle);
                          map.addLayer(labelTitle2);
                      });
                  </script>
                  

                  结果:

                  这篇关于如何将要在地图上显示的文本添加到传单中的 geojson 对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:通过 Leaflet 使用本地图块的 HTML 离线地图 下一篇:Leaflet - 可拖动的标记和坐标以字段形式显示

                  相关文章

                  最新文章

                  <legend id='qQzBp'><style id='qQzBp'><dir id='qQzBp'><q id='qQzBp'></q></dir></style></legend>

                      <bdo id='qQzBp'></bdo><ul id='qQzBp'></ul>
                  1. <i id='qQzBp'><tr id='qQzBp'><dt id='qQzBp'><q id='qQzBp'><span id='qQzBp'><b id='qQzBp'><form id='qQzBp'><ins id='qQzBp'></ins><ul id='qQzBp'></ul><sub id='qQzBp'></sub></form><legend id='qQzBp'></legend><bdo id='qQzBp'><pre id='qQzBp'><center id='qQzBp'></center></pre></bdo></b><th id='qQzBp'></th></span></q></dt></tr></i><div id='qQzBp'><tfoot id='qQzBp'></tfoot><dl id='qQzBp'><fieldset id='qQzBp'></fieldset></dl></div>

                    1. <tfoot id='qQzBp'></tfoot>

                      <small id='qQzBp'></small><noframes id='qQzBp'>