使用url加载kml失败,所以采用这种方法,鄙人不才,假如有大佬有更好的办法,可以留言给我
/**
*
* @param {*} kmlFile
* @param {*} context ol的环境
* @param {*} $api 地图请求API
*/
export const loadKML = function(kmlFile,context,$api){
context = context || this
var kmlVectorSource = new VectorSource({
format: new KML({
extractStyles:false
}),
loader:function(){
$api.map.getKmlFileContent(kmlFile).then(res => {
kmlVectorSource.addFeatures(kmlVectorSource.getFormat().readFeatures(res));
}).catch(e=>Error("加载所处地区错误,找不到或者本地不存在",e))
}
})
if(!context.kmlLayer){
const kmlLayer = new VectorLayer({
source: kmlVectorSource
})
context.map.addLayer(kmlLayer)
context.kmlLayer = kmlLayer
}else{
context.kmlLayer.getSource().clear()
context.kmlLayer.setSource(kmlVectorSource)
}
}