javascript - 将数据属性添加到 leaflet.js 标记元素

标签 javascript leaflet

目标:将数据属性添加到 leaflet.js 标记元素标记

我有一个带有 map 和“聚光灯”区域的项目。

map 使用 leaflet.js 填充位置

当我点击 map 上的某个图钉时,我想让它对应的图像和信息出现在聚光灯区域。

我在没有 map 的情况下进行了初步测试:http://codepen.io/sheriffderek/pen/yOmjLV我在哪里使用数据属性来连接硬币的两侧。 (一组数据由PHP吐出, map 数据为API ajax调用)

我理所当然地认为添加类或 Id 或数据或 rel 等将是一个可访问的选项。这是它的实质:

// Purveyor types - for query endpoints
var bar = 4;
var retailer = 3;

// Create the "map"
var onSiteMap = L.map('on-site-map').setView([34.0758661, -118.25430590], 13);

// Define the pin (no SVG?)
var onSiteIcon = L.divIcon({
  className: 'my-div-icon' // this gets one class name as far as I can tell
});

// Setup map "Look"
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png').addTo(onSiteMap);

// Grab the data
$.ajax( {
  url: 'http://xxxxxx.com/wp-json/wp/v2/purveyor?purveyor-type=' + bar,
  success: function ( data ) {
    var purveyorData = data;
    for (var i = 0; i < data.length; i++) {
      var ourLat = purveyorData[i].acf.purveyor_latitude;
      var ourLong = purveyorData[i].acf.purveyor_longitude;
      var ourSlug = purveyorData[i].slug;
      // create the markers
      L.marker([ ourLat , ourLong ], { 
        icon: onSiteIcon,
        slug: ourSlug // creates an extra option... but...
      }).addTo(onSiteMap);
    }
  },
  cache: false
});

我可以为对象添加一个“选项”和一个唯一值,但这并不能帮助我在标记中添加一些东西。

标记的元素最终是这样的:

<div 
    class="leaflet-marker-icon my-div-icon leaflet-zoom-animated leaflet-clickable" 
    tabindex="0" style="margin-left: -6px; margin-top: -6px; width: 12px; height: 12px; transform: translate3d(276px, 140px, 0px); z-index: 140;"></div>


试图得到更多像这样的东西:

<div 
    id='possible-id-237'
    rel='possible-rel'
    data-name='this-slug'
    class="leaflet-marker-icon my-div-icon leaflet-zoom-animated leaflet-clickable" 
    tabindex="0" style="margin-left: -6px; margin-top: -6px; width: 12px; height: 12px; transform: translate3d(276px, 140px, 0px); z-index: 140;"></div>

我做了一些研究 - 大多数问题都是 2014 年或更早的。希望新文档中缺少一些东西。

最佳答案

I can add an 'option' and a unique value for that - to the object, but that doesn't help me get something into the markup.

没错——Leaflet 不会神奇地将选项转换为 HTML data 属性。

首先:阅读 leaflet code !如果你花一点时间在里面,就很容易理解。对于标记,HTML 实际上是在 L.Icon 中构建的,而不是在 L.Marker 中构建的。

完成后,您会注意到 src/layer/marker/icon.js 中的代码执行如下操作:

_setIconStyles: function (img, name) {
    var options = this.options;

    if (options.something) {
        img.style.something = something(something);
    }
},

如果您随后阅读 Leaflet's plugin guide ,然后你会意识到你可以在以下行中制作一个插件:

L.Icon.DataMarkup = L.Icon.extend({

    _setIconStyles: function(img, name) {
        L.Icon.prototype._setIconStyles.call(this, img, name);

        if (options.slug) {
            img.dataset.slug = options.slug;
        }
    }

});

您应该能够从那里解决。

关于javascript - 将数据属性添加到 leaflet.js 标记元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37782930/

相关文章:

javascript - jQuery 点击事件目标填充

javascript - 从图 block 服务器返回的 map 不完整,缩放级别可用的图 block 数量比文档指示的要多

传单:两个 basemap 图层之间的混合

javascript - 传单不会显示我的标记, "TypeError: t is null"

javascript - 流YouTube视频WordPress小部件

javascript - Jquery VS Mootools 哪个性能更好?为什么?

javascript - UIWebview 中的自动凭据输入 - iOS

javascript - Canvas 。 AngularJS。添加文本和图像

R:带有时间 slider 的 map ?

R 传单提供商平铺 basemap - 传递 HERE 的 API 代码 token