简体   繁体   English

在传单地图中单击时,首先加载标记,然后加载每个弹出窗口的数据

[英]Load markers first and then data for each popup when clicked in Leaflet Map

I am trying to load 300 markers at a time from an API and since I'm trying to load the data also for pop-up which contains image, it takes too much time to load the markers as well.我正在尝试从 API 一次加载 300 个标记,并且由于我还尝试为包含图像的弹出窗口加载数据,因此加载标记也需要太多时间。 I want to know if I can load the markers as soon the map opens and then load the image for each marker pop-up when clicked on that particular marker.我想知道我是否可以在地图打开后立即加载标记,然后在单击该特定标记时为每个标记弹出窗口加载图像。

This is my piece of code:这是我的一段代码:

fetch('http://www.example.com/data.php?qty=250')
        .then((response) => response.json())
        .then((response) => {
            let datapoint = response.datapoint;
            for (let i=0; i<datapoint.length; i++) {

                let lat = parseFloat(datapoint[i]["lat"]);
                let lon = parseFloat(datapoint[i]["long"]);
                let popup = L.popup().setContent("<img src=\""+datapoint[i].img+"\" width='32%' height='135px'/>" + '<h3>');
                let markerLocation = new L.LatLng(lat, lon);
                let marker = new L.marker(markerLocation,{icon: greenIcon});
                marker.addTo(map).bindPopup(popup,customOptions);

                marker.setOpacity(1.0);
            }

            document.getElementById('loader').style.display='none';
        })

I tried to add this code but not sure what to do:我尝试添加此代码,但不知道该怎么做:

marker.bindPopup(function() {
    var el = $('<div/>');

    $.get("DYNAMIC_CONTENT_URL").done(function(data) {
        el.setContent(data);
        popup.update();
    });

    return el;
});

Here is my sample data:这是我的示例数据:

{"datapoint":[{"img":"abc.jpeg","latitud":"18.52","longitud":"82.4767"},{"img":"bbc.jpeg","latitud":"17.7375","longitud":"82.8347"}]}

Any help is appreciated!任何帮助表示赞赏! Thanks.谢谢。

This is how you could do it.这就是你可以做到的。 Bind an onclick event to the marker as it is created, then have a function that creates an independant popup on click.在创建标记时将 onclick 事件绑定到标记,然后使用一个函数在单击时创建一个独立的弹出窗口。

Example:例子:

var data = {
    "datapoint":[
            {"img":"abc.jpeg","latitud":"18.52","longitud":"82.4767"},
            {"img":"bbc.jpeg","latitud":"17.7375","longitud":"82.8347"}
        ]
}

function markerOnClick(e){
    L.popup()
    .setLatLng(e.latlng)
    .setContent('<img src="'+e.target.img+'">')
    .openOn(map);
}

for(i in data.datapoint){
    var marker = L.marker([data.datapoint[i].latitud, data.datapoint[i].longitud]);
    marker.img = data.datapoint[i].img;
    marker.on('click', markerOnClick);
    map.addLayer(marker);
}

You probably want to look into layerGroups, so you can group all these markers and do things like turn them on and off or update all of them as markers are added and remove, but this is a bare bones example.您可能想要查看 layerGroups,以便您可以对所有这些标记进行分组并执行诸如打开和关闭它们或在添加和删除标记时更新所有这些标记的操作,但这是一个简单的示例。

There is no need for ajax in this example, as the image will not be downloaded until the popup is created.在这个例子中不需要 ajax,因为在创建弹出窗口之前不会下载图像。 You would need to use ajax if you wanted to add other content.如果您想添加其他内容,则需要使用 ajax。

I have added a JSFiddle with the leaflet example to show how it works.我添加了一个带有传单示例的JSFiddle来展示它是如何工作的。 you will want to play around with the size of the popup to suit your needs您将想要调整弹出窗口的大小以满足您的需求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM