gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:13:48 25 4
gpt4 key购买 nike

目标:将数据属性添加到 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com