我想使用 Set Type,因为我认为它不能两次具有相同的值。但是我的“坐标”集不止一次获得相同的值。我能做些什么来获得唯一的价值?
ngOnInit()
ngOnInit(): void {
this.coords = new Set<Coordinate>();
this.coordinatesService.coordinates.subscribe((coordinates: Coordinate[]) => {
coordinates.forEach((coordinate) => {
this.addMarker(coordinate);
});
});
}
添加标记()
addMarker(coordinate: Coordinate): void {
if (!this.coords.has(coordinate)) {
this.coords.add(coordinate);
L.marker([coordinate.latitude, coordinate.longitude], this.icon)
.bindPopup('<b>Found location</b><br>' + coordinate.label)
.addTo(this.citiesLayerGroup);
}
}
坐标服务
在服务中,我添加了一个接口。
export interface Coordinate {
latitude: number;
longitude: number;
label: string;
}