openlayer 将 GeoJSON 格式的 geometry 转换为 feature
geometry格式如图:
代码:
/**
* 将 GeoJSON 格式的 geometry 转换为 feature
* @param geometry
* @returns
*/
geoJsonToWkt(geometry) {
const { coordinates, type } = geometry;
let olGeometry;
switch (type) {
case 'Point':
olGeometry = new Point(coordinates);
break;
case 'MultiPoint':
olGeometry = new MultiPoint(coordinates);
break;
case 'LineString':
olGeometry = new LineString(coordinates);
break;
case 'MultiLineString':
olGeometry = new MultiLineString(coordinates);
break;
case 'Polygon':
olGeometry = new Polygon(coordinates);
break;
case 'MultiPolygon':
olGeometry = new MultiPolygon(coordinates);
break;
default:
console.error('不支持的几何类型');
break;
}
if (olGeometry) {
const featureTemp = new Feature({
geometry: olGeometry,
});
return featureTemp;
}
return null;
}
转化完了就直接直接加载到地图啦!