简体   繁体   English

如何在openlayers 4中显示实时数据点,角度5

[英]How to show live data points in openlayers 4, angular 5

I want to update live data points in my map. 我想更新地图中的实时数据点。 I get every second a new wkt-point through a Service. 我通过服务每秒钟获得一个新的wkt点。 But every point should stay on the map, so that i can follow the wkt-points. 但是每个点都应该留在地图上,以便我可以跟随wkt点。
At the moment, i can only show one static point in my map: 目前,我只能在地图上显示一个静态点:

import {Component, OnInit, AfterViewInit, ViewEncapsulation} from '@angular/core';
import {PointService} from "../../pointservice.service";

import * as ol from 'openlayers';


@Component({
  selector: 'olmap',
  templateUrl: './map.component.html',
  styleUrls: ['./map.component.css']
})
export class MapComponent implements OnInit, AfterViewInit {

  layername: string;
  layerurl: string;

constructor(private mapService: PointService){}

ngAfterViewInit() {
    setTimeout (() => {

      const raster = new ol.layer.Tile({
        source: new ol.source.OSM()
      });

      const wkt = 'POINT(20, 30)';

      const format = new ol.format.WKT();

      const feature = format.readFeature(wkt, {
        dataProjection: 'EPSG:4326',
        featureProjection: 'EPSG:3857'
      });

      const vector = new ol.layer.Vector({
        source: new ol.source.Vector({
          features: [feature]
        })
      });


      let map = new ol.Map({
        target: this.mapId2,
        layers: [raster, vector,
          new ol.layer.Tile({
            source: new ol.source.OSM(),
          })
        ],
        view: new ol.View({
          center: ol.proj.fromLonLat([6.661594, 50.433237]),
          zoom: 2,
        })
      }); },500)}


I have to use the AfterViewInit lifecycle hook with the timeout of 500ms, for some other reasons. 由于某些其他原因,我必须使用AfterViewInit生命周期挂钩(超时为500ms)。

Perhaps the best way would be to define a own method to show the route of the wkt-points? 也许最好的方法是定义一个自己的方法来显示wkt点的路线?

Mi idea is the observe my Data-Service, so that the points are autoupdating every second, but how could i implement that the points showing up every second in openlayers??? 我的想法是观察我的数据服务,以便这些点每秒自动更新,但是我如何实现这些点在openlayers中每秒显示一次呢???

Just define an array of features as a member of your class: 只需将一系列功能定义为您的类的成员即可:

private arrayOfFeatures: ol.Feature[] = [];

The modify ngAfterViewInit like this: 修改ngAfterViewInit像这样:

ngAfterViewInit() {
setTimeout (() => {

  const raster = new ol.layer.Tile({
    source: new ol.source.OSM()
  });


  const vector = new ol.layer.Vector({
    source: new ol.source.Vector({
      features: this.arrayOfFeatures
    })
  });


  let map = new ol.Map({
    target: this.mapId2,
    layers: [raster, vector,
      new ol.layer.Tile({
        source: new ol.source.OSM(),
      })
    ],
    view: new ol.View({
      center: ol.proj.fromLonLat([6.661594, 50.433237]),
      zoom: 2,
    })
  }); },500)}

and finally create a function to add a single feature: 最后创建一个添加单个功能的函数:

readSingleFeature( wkt: string) {
  const format = new ol.format.WKT();

  const feature = format.readFeature(wkt, {
    dataProjection: 'EPSG:4326',
    featureProjection: 'EPSG:3857'
  });

this.arrayOfFeatures.push( feature );

}

Then you just have to observe your data service and when you get a string, just call readSingleFeature . 然后,您只需要观察数据服务即可,当您获得字符串时,只需调用readSingleFeature

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

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