我使用Serice从客户端获取一些实时数据。实时数据将保存在我的服务中:
@Injectable()
export class GeomqttService {
client: any;
liveData = [
{
topic: '',
timestamp: '',
payloadstring: '',
geometry: ''
}
];
constructor () {};
...
CODE to connect to the client and get the data
...
this.client.onMessageArrived = (message: Paho.MQTT.Message) => {
console.log('Message arrived: ' + message.payloadString);
this.geohistory.push({
'topic': 'temp',
'timestamp': message.timestamp,
'payloadstring': message.payloadString,
'geometry': message.geometry
});
几何值包含一个像这样的wkt-string:
'POINT (30 10)'
现在我想在我的地图中通过服务注入这些点 这不起作用:
import {GeomqttService} from '../../paho/geomqtt.service';
import * as ol from 'openlayers';
@Component({
selector: 'olmap',
encapsulation: ViewEncapsulation.None,
templateUrl: './map.component.html',
styleUrls: ['./map.component.css']
})
export class MapComponent implements OnInit, AfterViewInit {
constructor(private geomqttService: GeomqttService) {}
ngAfterViewInit() {
let map = new ol.Map({
target: this.mapId2,
layers: [
new ol.layer.Tile({
source: new ol.source.OSM(),
}),
//vector
],
view: new ol.View({
center: ol.proj.fromLonLat([6.661594, 50.433237]),
zoom: 3,
})
});
const wkt = this.service.liveData.geometry;
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]
})
});
// -- addlayer --
addLayer(url: string, name: string) {
console.log(name);
let addedlayer = new ol.layer.Tile({
source: new ol.source.TileWMS({
url: url,
params: {
'LAYERS': name,
'TILED': true
},
projection: 'EPSG:4326'
})
});
this.layers.push(addedlayer);
this.mapService.getMap(this.mapIndex).addLayer(addedlayer);
}
}
我通常如何通过角度服务访问对象或数组?