Javascript 在(';单击';)上,函数…不适用于我

Javascript 在(';单击';)上,函数…不适用于我,javascript,leaflet,Javascript,Leaflet,我正在尝试记录一张传单.js地图上的点击时间。我计划将其记录到数据库中,但目前我只想查看记录的点击位置的证据。我正在尝试激活一个警报,根据传单文档打印时间 这段代码中还有一些额外的东西,只是生成自定义标记,但现在还可以 如果有人能指出我把这个简单的警报放在底部的错误,我会很感激的 window.onload = function() { var energyIcon = L.icon({ iconUrl: '../scripts/leaflet/images/marker-icon-g

我正在尝试记录一张传单.js地图上的点击时间。我计划将其记录到数据库中,但目前我只想查看记录的点击位置的证据。我正在尝试激活一个警报,根据传单文档打印时间

这段代码中还有一些额外的东西,只是生成自定义标记,但现在还可以

如果有人能指出我把这个简单的警报放在底部的错误,我会很感激的

window.onload = function() {

var energyIcon = L.icon({
    iconUrl: '../scripts/leaflet/images/marker-icon-green.png',
    iconRetinaUrl: '../scripts/leaflet/images/marker-icon-green-@2x.png',
    iconSize: [25, 41],
    iconAnchor: [25, 41],
    popupAnchor: [-12, -38],
    shadowUrl: '../scripts/leaflet/images/marker-shadow.png',
    shadowRetinaUrl: '../scripts/leaflet/images/marker-shadow-@2x.png',
    shadowSize: [41, 41],
    shadowAnchor: [25, 41]
});

var foodIcon = L.icon({
    iconUrl: '../scripts/leaflet/images/marker-icon.png',
    iconRetinaUrl: '../scripts/leaflet/images/marker-icon--@2x.png',
    iconSize: [25, 41],
    iconAnchor: [25, 41],
    popupAnchor: [-12, -38],
    shadowUrl: '../scripts/leaflet/images/marker-shadow.png',
    shadowRetinaUrl: '../scripts/leaflet/images/marker-shadow-@2x.png',
    shadowSize: [41, 41],
    shadowAnchor: [25, 41]
});

var tourismIcon = L.icon({
    iconUrl: '../scripts/leaflet/images/marker-icon-red.png',
    iconRetinaUrl: '../scripts/leaflet/images/marker-icon-red-@2x.png',
    iconSize: [25, 41],
    iconAnchor: [25, 41],
    popupAnchor: [-12, -38],
    shadowUrl: '../scripts/leaflet/images/marker-shadow.png',
    shadowRetinaUrl: '../scripts/leaflet/images/marker-shadow-@2x.png',
    shadowSize: [41, 41],
    shadowAnchor: [25, 41]
});

var map = new L.Map('map', {
    zoom: 12, 
    center: new L.latLng(data[data.length -1].loc) //set center from first location
}); 

map.addLayer(new L.TileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png')); //base layer

var markersLayer = new L.LayerGroup();  //layer contain searched elements
map.addLayer(markersLayer);

// ===== populate map with markers from sample data and give it a different icon depending on its 'type'

for(i in data) {
    var title = data[i].title,  // value searched
        loc = data[i].loc,      // position found
        type = data[i].type;    // type
        title = data[i].title   // title

    if (data[i].type == "energy") {
        var marker = new L.Marker(new L.latLng(loc), {title: title, icon: energyIcon});
    } else if (data[i].type == "food") {
        var marker = new L.Marker(new L.latLng(loc), {title: title, icon: foodIcon} );
    } else if (data[i].type == "tourism") {
        var marker = new L.Marker(new L.latLng(loc), {title: title, icon: tourismIcon} );
    }
    marker.bindPopup('Hello. I\'m ' + title + '. This is a place of type "'+ type + '" and is number ' + i + ' in this view.');
    markersLayer.addLayer(marker);
}

// ===== inizialize search control
map.addControl( new L.Control.Search({
    wrapper: 'findbox',
    layer: markersLayer,
    initial: false,
    collapsed: false
}) );

map.on('click', function(e) {
    alert(e.latlng); // e is an event object (MouseEvent in this case)
}); 

}

似乎
map.addControl()
正在制造问题


注释掉这个函数,然后运行你的代码。希望它能正常工作

当你点击map时,你在控制台中看到任何错误吗?有这样一个;'无法读取传单-search.min.js的'style'属性'null',但我不知道它是否相关或正在停止其他内容?注释
map.addControl()
函数单击
函数上方的
函数,然后再试一次。该函数已被注释掉!谢谢。知道这会导致问题的原因吗?看起来搜索控件没有正确添加,它可能需要一些参数,这些参数是用空值传递的,这会造成问题,例如检查“findbox”是否存在等