Leaflet 使用全局变量的小叶iconurl

Leaflet 使用全局变量的小叶iconurl,leaflet,Leaflet,我试图将我的iconurl设置为全局变量(或任何称为var1的变量),但它似乎不起作用。代码如下。请注意,当iconurl设置为“”时,代码可以工作,但如果我们设置var1='',然后执行iconurl:var1(如下所述),则不会工作。我在控制台中进行了检查,var1是正确的(我们在其他地方也成功地使用了这个链接/变量) 我们如何称呼标记器(其工作原理): 感谢您的帮助 你的问题一定在别处 您可以使用一个变量:请在此处自行查看 我也尝试过使用http表单(但我不建议使用) 出现问题时是否检查了

我试图将我的iconurl设置为全局变量(或任何称为var1的变量),但它似乎不起作用。代码如下。请注意,当iconurl设置为“”时,代码可以工作,但如果我们设置var1='',然后执行iconurl:var1(如下所述),则不会工作。我在控制台中进行了检查,var1是正确的(我们在其他地方也成功地使用了这个链接/变量)

我们如何称呼标记器(其工作原理):


感谢您的帮助

你的问题一定在别处

您可以使用一个变量:请在此处自行查看

我也尝试过使用http表单(但我不建议使用)


出现问题时是否检查了javascript控制台?

我尝试了以下操作:{iconUrl:'www.ourserver.com/picture/'+username}。这最终会在“www.ourserver.com/picture/”上显示图片,这意味着username为null,但当我打印它时它不是null(打印到控制台时它是正确的),因为我确保(与控制台一起)函数无法运行,因为它在if语句中,如:if(username!=null){function with iconull…}一位朋友查看了我的代码,我们发现了问题。基本上,我没有意识到myiconda也是一个全局变量(我使用代码的方式),var1也是。因此,在生成myiconda时,var1为null(切换它们的顺序并不重要,因为var1是在另一个函数中生成的)。每次我(在L.marker中)执行以下命令:“icon”:new myiconda(),我只是在创建myiconda的一个新副本,此时var1为null。这也是为什么设置var1='some string'时会起作用的原因。如果我输入更多的信息,你(弗朗西梅奇)可能已经知道了。谢谢
var myIconData = L.Icon.Label.extend({
options: {

iconUrl: var1,
shadowUrl: 'images/marker2.png',

iconSize:     [40, 40], // size of the icon
shadowSize:   [50, 55], // size of the shadow
iconAnchor:   [20, 50], // point of the icon which will correspond to marker's location
shadowAnchor: [25, 55],  // the same for the shadow
popupAnchor:  [0, -55], // point from which the popup should open relative to the iconAnchor

labelAnchor: [0, 0],
wrapperAnchor: [20, 50],
labelClassName: 'sweet-deal-label'
}
});
mymarker=new L.Marker([position.coords.latitude, position.coords.longitude],{ 'icon': new myIconData(), 'title': username });
// custom icon
var image = 'leaf-green.png'; 
var greenIcon = L.icon({
    iconUrl: image,
    shadowUrl: 'leaf-shadow.png',

    iconSize:     [38, 95], // size of the icon
    shadowSize:   [50, 64], // size of the shadow
    iconAnchor:   [22, 94], // point of the icon which will correspond to marker's location
    shadowAnchor: [4, 62],  // the same for the shadow
    popupAnchor:  [-3, -76] // point from which the popup should open relative to the iconAnchor
});