猿问

Leaflet.js 弹出页面在页面加载时可见

我正在将此插件用于一个项目。当我创建弹出窗口时,它们在加载时被激活。我希望它们在用户单击标记时打开。有什么帮助吗?


const mymap = L.map('mapid').setView([40.399245, 49.876386], 10);

const tile = L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}', {

  attribution: 'Map data &copy; <a href="https://www.mapbox.com/">Mapbox</a>',

  maxZoom: 18,

  id: 'mapbox.streets',

  accessToken: 'TOKEN'

}).addTo(mymap);


const mapIcon = L.icon({

  iconUrl: 'assets/image/icons/marker.png',

  iconSize: [40, 40],

  iconAnchor: [20, 20]

})



const marker = L.marker([40.399245, 49.876386], { icon: mapIcon }).addTo(mymap);

const marker = L.marker([40.399245, 49.876386], { icon: mapIcon }).addTo(mymap);

var popup = L.popup({}).setContent('content')


marker.bindPopup(popup).openPopup();

marker.setLatLng([40.399245, 49.876386]).addTo(mymap);


一只甜甜圈
浏览 193回答 1
1回答

慕斯709654

这行代码是罪魁祸首:marker.bindPopup(popup).openPopup();在这一行中,您将弹出窗口绑定到标记,然后立即打开弹出窗口。省略该openPopup()方法应该可以解决您的问题。marker.bindPopup(popup);根据传单文档:使用示例如果您只想将弹出窗口绑定到标记单击然后打开它,这真的很容易:marker.bindPopup(popupContent).openPopup();
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答