在变量中构建函数时,如何构建可以运行该函数的链接?当我单击图层并加载弹出窗口时,我试图在地图下的另一个div中填充一些geoJSON数据。到目前为止,我可以使弹出窗口正常工作,但是如何构建onclick事件以将完整说明加载到地图下方的另一个div中?

"headline": "Flash Flood Warning issued July 28 at 7:10PM MST expiring July 28 at 10:00PM MST by NWS Flagstaff AZ",
"description": "The National Weather Service in Flagstaff has issued a\n\n* Flash Flood Warning for...\nYavapai County in west central Arizona...\n\n* Until 1000 PM MST\n\n* At 704 PM MST, Doppler radar indicated thunderstorms producing\nheavy rain across the warned area. Portions of the warned area\nhave received one to two inches of rain in a 30 minute period. The\nheaviest rain has been just north of Crown King, along Turkey\nCreek west of Cordes, and the area just northeast of Black Canyon\nCity.\n\nFlash flooding is expected to begin shortly.\n\n* Some locations that will experience flooding include...\nCordes Junction, Bumble Bee, Cordes Lakes, Crown King, Black Canyon\nCity, Rock Springs, Castle Hot Springs, Wagoner, Cordes, Turney\nGulch Group Campground and Hazlett Hollow Campground.\n\nThis includes the following streams and drainages...Poland Creek...\nSycamore Creek...Bitter Creek...Castle Creek...Squaw Creek...Turkey\nCreek...Tuscumbia Creek...Black Canyon Creek...Big Bug\nCreek...Blind Indian Creek...Agua Fria River...Wolf Creek.\n\nThis includes the following highways...\nState Route 69 between mile markers 263 and 264.",

jQuery.ajax({
    type: "GET",
    dataType: "json",
    url: "https://api.weather.gov/alerts?active=1&event=Flood%20Warning,Flash%20Flood%20Warning,Flood%20Watch,Flash%20Flood%20Watch",
    error: function (err) { console.log(err)},
    success: function (data, status, xhr) {
        jQuery(data.features).each(function(index, i) {
            console.log(i.properties.headline);
        });
        console.log(data.features.length);
        L.geoJson(data, {
            onEachFeature: function (feature, layer) {
                layer.bindPopup(feature.properties.event+"<br>"+feature.properties.headline+"<br>");
            }
        }).addTo(map);
    }
})


我不知道如何在地图下方的div中填充标题和说明。 l.GeoJson没有点击事件吗?我尝试用onclick ='function('“ + myFunction +”')'创建一个变量,但是'“出现问题,并且一直在中断。有什么想法吗?

最佳答案

如果我正确理解您的问题,那么这是一种简单的方法。像这样在您的图层上绑定一个click事件:

L.geoJson(data, {
    onEachFeature: function (feature, layer) {
        layer.bindPopup(feature.properties.event+"<br>"+feature.properties.headline+"<br>");
        layer.on({
            click: function (e) {
                //Do what you need in your div
                // You have full access to your feature properties
            }
        });
    }
}).addTo(map);


当您单击任何这些功能时,将触发该事件。

关于javascript - Mapbox-bindPopup,在div中加载内容,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51577063/

10-10 09:10