gpt4 book ai didi

angular - 如何通过方法添加标记,openlayers 4,angular 4

转载 作者:太空狗 更新时间:2023-10-29 18:08:00 26 4
gpt4 key购买 nike

我想在我的 map 中添加实时位置标记。
作为第一步,我只想在我的 map 中显示一个静态点(纬度、经度)或标记,由 addMarker 我的代码中的方法。
这是我的代码:

import {Component, OnInit, AfterViewInit, ViewEncapsulation, Input, ElementRef, ViewChilds} from '@angular/core';
import { WmslayerService} from './wmslayer.service';
import { MapService} from './map.service';
import {AddwmslayerService} from '../../menue/addlayer/formlayer/addwmslayer.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 {


iconFeatures = [];
layername: string;
layerurl: string;

mapId2: string;
mapIndex: number;
layerName: string;

layer = [];
layers = [];


constructor(private mapService: MapService, private wmslayerService: WmslayerService,
private addLayerService: AddwmslayerService, private tempdatagiaService: TempdatagiaService,
private geomqttService: GeomqttService) {}


ngAfterViewInit() {
setTimeout (() => {

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,
})
});

//// for loop, to add some layer

for (let layer of this.wmslayerService.layer) {

console.log(layer);

let examplelayer = new ol.layer.Tile({
source: new ol.source.TileWMS({
url: layer.layerurl,
params: {
'LAYERS': layer.layername,
'TILED': true
},
projection: 'EPSG:4326',
serverType: 'geoserver'
})
});
this.layers.push(examplelayer);
map.addLayer(examplelayer);
}
this.mapIndex = this.mapService.addMap(map);
this.layername = this.layerName;
console.log(this.mapIndex);
},300)}

// -- addlayer dynamicly--

addNewLayer(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);
}



addMarker(lon: string, lat: string) {
console.log(lat);
console.log(lon);

var iconFeatures = [];

var iconFeature = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.transform([lon, lat], 'EPSG:4326',
'EPSG:3857')),
name: 'Null Island',
population: 4000,
rainfall: 500
});

iconFeatures.push(iconFeature);

var vectorSource = new ol.source.Vector({
features: iconFeatures //add an array of features
});

var iconStyle = new ol.style.Style({
image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
anchor: [0.5, 46],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
opacity: 0.75,
src: 'https://openlayers.org/en/v4.6.4/examples/data/icon.png'
}))
});


var vectorLayer = new ol.layer.Vector({
source: vectorSource,
style: iconStyle
});

map.addLayer(vectorLayer);

}
}


最后一行 map.addLayer(vectorLayer); 不起作用。
addMarker 方法将由点击事件触发。
我怎样才能使该方法起作用?
有人有更好的主意或稳定的解决方案来在 openlayers 4 map 中显示实时数据点吗?

最佳答案

您无需在每次要显示附加标记时都创建一个新层。创建单个“标记层”并向其源添加功能通常更容易。您只需要保持对图层源的引用并对其调用 addFeature

坐标不是字符串,而是小数:addMarker(lon: decimal, lat: decimal)

const markerSource = new ol.source.Vector();

var markerStyle = new ol.style.Style({
image: new ol.style.Icon(/** @type {olx.style.IconOptions} */ ({
anchor: [0.5, 46],
anchorXUnits: 'fraction',
anchorYUnits: 'pixels',
opacity: 0.75,
src: 'https://openlayers.org/en/v4.6.4/examples/data/icon.png'
}))
});

let map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM(),
}),
new ol.layer.Vector({
source: markerSource,
style: markerStyle,
}),
],
view: new ol.View({
center: ol.proj.fromLonLat([6.661594, 50.433237]),
zoom: 3,
})
});


function addMarker(lon, lat) {
console.log('lon:', lon);
console.log('lat:', lat);

var iconFeatures = [];

var iconFeature = new ol.Feature({
geometry: new ol.geom.Point(ol.proj.transform([lon, lat], 'EPSG:4326',
'EPSG:3857')),
name: 'Null Island',
population: 4000,
rainfall: 500
});

markerSource.addFeature(iconFeature);
}

map.on('singleclick',function(event){
var lonLat = ol.proj.toLonLat(event.coordinate);
addMarker(lonLat[0], lonLat[1]);
});
#map {
/* just for testing purposes */
width: 100%;
min-width: 240px;
max-width: 500px;
height: 200px;
}
<link href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@main/dist/en/v7.0.0/legacy/ol.css" rel="stylesheet"/>
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@main/dist/en/v7.0.0/legacy/ol.js"></script>

<button onclick="addMarker(8.54, 47.37)">add marker at 8.54, 47.37</button>
<div id="map"></div>

关于angular - 如何通过方法添加标记,openlayers 4,angular 4,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48170329/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com