Javascript 向OpenLayers 6添加功能

Javascript 向OpenLayers 6添加功能,javascript,openlayers-6,Javascript,Openlayers 6,我正在尝试使用OpenLayers 6向矢量层添加几何体、线串,但失败了。谢谢你的帮助 这是我的密码 var坐标=[ [新ol.几何点(103.8797182,1.3160559)], [新ol.几何点(103.8800485,1.3161336)], [新ol.几何点(103.8800889,1.3161672)], [新ol.几何点(103.8801166,1.3162658)], [新ol.几何点(103.8798829,1.3171543)], ]; 控制台日志(坐标); 图层=新的ol

我正在尝试使用OpenLayers 6向矢量层添加几何体、线串,但失败了。谢谢你的帮助

这是我的密码

var坐标=[
[新ol.几何点(103.8797182,1.3160559)],
[新ol.几何点(103.8800485,1.3161336)],
[新ol.几何点(103.8800889,1.3161672)],
[新ol.几何点(103.8801166,1.3162658)],
[新ol.几何点(103.8798829,1.3171543)],
];
控制台日志(坐标);
图层=新的ol.layer.Vector({
来源:新ol.source.Vector({
特点:[
新ol.功能({
几何:新的ol.geom.LineString(坐标),
名称:“行”,
}),
],
}),
风格:功能(特征){
log(feature.getGeometry().getType());
返回样式[feature.getGeometry().getType()];
},
});
添加图层(图层);
代码有什么问题吗?因为图层没有显示

编辑: 我已经实施了Mark的建议,以下是解决方案:

var coordinates = [
    [ 103.7960334725309, 1.4494121393815099 ],
    [ 103.79617186914557, 1.4491070600247167 ],
    [ 103.79642909728881, 1.4489874603770377 ],
    [ 103.79664709373664, 1.4489591347536637 ],
    [ 103.79904789809408, 1.4501025693183976 ],
    [ 103.79917449669307, 1.449834325824822 ]
];

var lines = new ol.geom.LineString(coordinates).transform('EPSG:4326', powerMap.getView().getProjection());

var layer = new ol.layer.Vector({
  source: new ol.source.Vector({
    features: [
      new ol.Feature({
        geometry: lines,
        name: "Line",
      }),
    ],
  }),
  style: function (feature) {
    console.log(feature.getGeometry().getType());
    return styles[feature.getGeometry().getType()];
  },
});

powerMap.addLayer(layer);

坐标应该是坐标数组,而不是点几何图形数组

var coordinates = [
  [103.8797182, 1.3160559],
  [103.8800485, 1.3161336],
  [103.8800889, 1.3161672],
  [103.8801166, 1.3162658],
  [103.8798829, 1.3171543],
];
您可能还需要将线字符串转换为地图投影

new ol.geom.LineString(coordinates).transform('EPSG:4326', map.getView().getProjection())