1.基本格式
var geojson = {
type: "FeatureCollection",
features: [
{
type: "Feature",
geometry: {
type: "Point",
coordinates: [114.30, 30.50]
},
id:10001,
properties:{
name:"武汉"
}
}
]
}
2.将geojson数据转化为ol要素
(1)
var feature = new ol.format.GeoJSON().readFeatures(geojson);
(2)
fetch("./data/data.json").then(res=>res.json()).then(data=>{
var features = new ol.format.GeoJSON().readFeatures(data)
})
3.加载本地的geojson数据
var source = new ol.source.Vector({
format:new ol.format.GeoJSON(),
url:"./data/data.json"
})
4.加载网络的geojson数据
var source = new ol.source.Vector({
format:new ol.format.GeoJSON(),
url:"https://geo.datav.aliyun.com/areas_v3/bound/130000_full.json"
})