cesium如何加载geojson数据?
步骤1:创建Cesium Viewer
const viewer = new Cesium.Viewer('cesiumContainer');
步骤2:加载GeoJSON数据
// 假设geojsonData是已获取的GeoJSON对象或URL
const geojsonDataSource = new Cesium.GeoJsonDataSource();
geojsonDataSource.load(geojsonData) .then(dataSource => {
viewer.dataSources.add(dataSource);
}) .otherwise(error => { console.error('加载GeoJSON失败:', error); });
步骤3:配置样式(可选)
可以为GeoJSON中的不同图层(点、线、面)设置颜色、贴图等:在viewer.dataSources.add(dataSource)后遍历实体添加样式
dataSource.entities.values.forEach(entity => {
// 添加的实体为线条
entity.polyline = new Cesium.PolylineGraphics({
positions: entity.polyline.positions,
width: 3, // 主线条宽度
material: new Cesium.PolylineGlowMaterialProperty({
//添加发光材质
color: Cesium.Color.DODGERBLUE, // 主线条颜色:蓝紫色
outlineWidth: 1.5, // 轮廓宽度:1.5 像素
outlineColor: Cesium.Color.TRANSPARENT // 轮廓颜色:透明
})
});
//添加的实体为面
entity.polygon.material = new Cesium.ColorMaterialProperty(
Cesium.Color.fromCssColorString('#23375A').withAlpha(0.7) // 正式简洁的蓝色,带透明度
);
entity.polygon.outline = true;// 启用轮廓线
entity.polygon.outlineColor = Cesium.Color.fromCssColorString('#00ffff'); // 科技青边框
entity.polygon.outlineWidth = 2; // 轮廓线宽度
});
const classificationType = Cesium.ClassificationType.SINGLE_PROPERTY; // 按属性分类
const colorProperty = new Cesium.ColorMaterialProperty( Cesium.Color.YELLOW // 默认颜色 );
dataSource.entities.values.forEach(entity => { entity.polyline.material = colorProperty; // 例如设置线条颜色 });
GeoJSON数据格式说明
GeoJSON是一种基于JSON的地理空间数据交换格式,典型结构如下:
{ "type": "FeatureCollection", // 必须是FeatureCollection或Geometry "features": [ { "type": "Feature", // 单个地理特征 "geometry": { // 几何信息 "type": "Point/LineString/Polygon", // 几何类型 "coordinates": [lng, lat] // 经纬度坐标 }, "properties": {} // 属性(可自定义) } ] }