简体   繁体   English

OpenLayers 5,Angular 7 线未显示

[英]OpenLayers 5, Angular 7 lines not showing

I am using Angular 7 , OpenLayers 5 to show map with markers.我正在使用Angular 7OpenLayers 5来显示带有标记的地图。 Now I want to draw lines between markers.现在我想在标记之间画线。 I followed some examples.我遵循了一些例子。 Like this .这样

But the lines don't show up.但是线条不显示。 I tried to pass array of coordinates to LineString instead of Point .我试图将坐标数组传递给LineString而不是Point That didn't work either.那也没有用。 I'd appreciate some help.我很感激一些帮助。

Here's the code:这是代码:


   this.source = new XYZ({
     url: 'http://tile.osm.org/{z}/{x}/{y}.png'
   });

   this.layer = new TileLayer({
     source: this.source
   });

   this.view = new View({
     center: fromLonLat([lon, lat]),
     zoom: 5
   });

   this.map = new Map({
     target: 'map',
     layers: [this.layer],
     view: this.view,
     controls: defaultControls().extend([
       new OverviewMap()
     ]),
   });

   let vectorSource = new VectorSource({});

    // create features for markers
    for (let i = 0; i < nodes.length; i++) {
      let nodeFeature = new Feature({
        geometry: new Point(fromLonLat([nodes[i].lon, nodes[i].lat])),
        name: nodes[i].name
      });
      vectorSource.addFeature(nodeFeature);
    }
    let vectorLayer = new VectorLayer({
      source: vectorSource
    });
    this.map.addLayer(vectorLayer);

   const fromProjection = new Projection("EPSG:4326");   // Transform from WGS 1984
   const toProjection = new Projection("EPSG:900913");

   let lon = 24.9342;
   let lat = 60.2017;

   // Start and end point
   let start_point1 = new Point(lon, lat).transform(fromProjection, toProjection);
   let end_point1 = new Point(30.9342, 62.2017).transform(fromProjection, toProjection);

   let start_point2 = new Point(lon, lat).transform(fromProjection, toProjection);
   let end_point2 = new Point(20.9342, 55.2017).transform(fromProjection, toProjection);

   // new vector graphic layer
   var layerLines = new LayerVector({
     source: new VectorSource({
         features: [new Feature({
             geometry: new LineString([start_point1, end_point1]),
             name: 'Line 1'
         }), 
         new Feature({
           geometry: new LineString([start_point2, end_point2]),
           name: 'Line 2'
       })]
     })
 });
   this.map.addLayer(layerLines);

I got it to work with markers and lines.我让它与标记和线条一起工作。 I'm new to OpenLayers and still learning.我是OpenLayers新手,仍在学习。 There are probably better examples but these two that I found, make better sense to me:可能有更好的例子,但我发现这两个对我来说更有意义:

  1. how to add lines between point in openlayers 如何在openlayers中的点之间添加线

  2. Marker Animation 标记动画

The first example shows how to transform projections for LineString then add to Feature .第一个示例显示如何转换LineString投影,然后添加到Feature The second one shows how to use different styles.第二个展示了如何使用不同的样式。 In my case, I have different styles for the lines and markers.就我而言,我有不同的线条和标记样式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM