open modal with marker leaflet bootstrap 3

雨燕双飞 提交于 2019-12-08 05:29:00

问题


i got the map showing in the modal working. now i have a new problem. i have different objects and i will store the coordinates for the marker in the id.

like this:

<a href="#eintrag" id="[48.20682894891699, 16.370315551757812]" role="button" class="btn btn-primary" data-toggle="modal">Open Map</a>

this also opens the modal with the map. then i have this:

    var map = L.map('map', {
        center: new L.LatLng(48.20682894891699, 16.370315551757812),
        zoom: 14,
        maxBounds: bounds
    });

    jQuery(document).on("click", "a", function (event) {
        id =jQuery(this).attr('id') || '';
        console.log(id);                    
    }); 

    L.marker(id).addTo(map);

can anyone help me out with this? - the map is blank :( help much apreciated!


回答1:


Two things for starters:

  1. You want to use id when it's undefined (you create it when you click a)
  2. You can't use string id to init marker, you can fix it using eval()

Is this what you wanted?

BTW. For storing data you can use data-id instead of id.




回答2:


You are getting the value as string, try eval() -

var map = L.map('map', {
    center: new L.LatLng(48.20682894891699, 16.370315551757812),
    zoom: 14,
    maxBounds: bounds
});

jQuery(document).on("click", "a", function (event) {
    id =jQuery(this).attr('id');
    console.log(id);                    
}); 

L.marker(eval(id)).addTo(map);


来源:https://stackoverflow.com/questions/20404174/open-modal-with-marker-leaflet-bootstrap-3

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!