Leaflet中使用Leaflet.Spin插件实现地图加载等待效果
场景
Leaflet快速入门与加载OSM显示地图:
https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/122290880
在上面的基础上,怎样使用插件实现地图加载等待效果如下
3、添加地图区域GeoJson数据
var campus = { type: "Feature", properties: { popupContent: "This is the Auraria West Campus", style: { weight: 2, color: "#999", opacity: 1, fillColor: "#B0DE5C", fillOpacity: .8 } }, geometry: { type: "MultiPolygon", coordinates: [ [ [ [-105.00432014465332, 39.74732195489861], [-105.00715255737305, 39.7462000683517], [-105.00921249389647, 39.74468219277038], [-105.01067161560059, 39.74362625960105], [-105.01195907592773, 39.74290029616054], [-105.00989913940431, 39.74078835902781], [-105.00758171081543, 39.74059036160317], [-105.00346183776855, 39.74059036160317], [-105.00097274780272, 39.74059036160317], [-105.00062942504881, 39.74072235994946], [-105.00020027160645, 39.74191033368865], [-105.0007152557373, 39.74276830198601], [-105.00097274780272, 39.74369225589818], [-105.00097274780272, 39.74461619742136], [-105.00123023986816, 39.74534214278395], [-105.00183105468751, 39.74613407445653], [-105.00432014465332, 39.74732195489861] ], [ [-105.00361204147337, 39.74354376414072], [-105.00301122665405, 39.74278480127163], [-105.00221729278564, 39.74316428375108], [-105.00283956527711, 39.74390674342741], [-105.00361204147337, 39.74354376414072] ] ], [ [ [-105.00942707061768, 39.73989736613708], [-105.00942707061768, 39.73910536278566], [-105.00685214996338, 39.73923736397631], [-105.00384807586671, 39.73910536278566], [-105.00174522399902, 39.73903936209552], [-105.00041484832764, 39.73910536278566], [-105.00041484832764, 39.73979836621592], [-105.00535011291504, 39.73986436617916], [-105.00942707061768, 39.73989736613708] ] ] ] } };
4、模拟ajax请求后台数据,这里是5秒,map.spin(true)则显示加载效果,为false则不显示加载效果
var layer = L.geoJson().addTo(map); //设置为true则显示加载效果 map.spin(true); // Simulate AJAX //模拟ajax请求数据,5秒返回数据 setTimeout(function () { layer.addData(campus); //设置为false则不显示加载效果 map.spin(false); }, 5000);
5、完整示例代码
"en"> "UTF-8">leaflet实现地图加载等待效果 "stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css" />"map">