[英]How to write custom coordinateFormat of ControlMousePosition in Openlayers 6?
I use Openlayers 6 in project of Angular 14.我在 Angular 14 的项目中使用 Openlayers 6。
I don't know how write to coordinateFormat.我不知道如何写入坐标格式。
The following code does not work properly以下代码无法正常工作
import { createStringXY, format } from 'ol/coordinate';
import {
Component,
OnInit,
ChangeDetectionStrategy,
Input,
ElementRef,
} from '@angular/core';
import Map from 'ol/Map';
import ControlMousePosition from 'ol/control/MousePosition';
@Component({
selector: 'app-mouse-position',
template: ``,
styles: [],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MousePositionComponent implements OnInit {
@Input()
map!: Map;
@Input()
positionTemplate!: string;
control!: ControlMousePosition;
constructor(private element: ElementRef) {
}
ngOnInit() {
this.control = new ControlMousePosition({
className: 'mouseposition-control',
coordinateFormat: (coordinate: number[]) => { return format(coordinate, this.positionTemplate);},
target: this.element.nativeElement,
undefinedHTML: undefined,
});
this.map.addControl(this.control);
}
}
the error message is错误信息是
Type '(coordinate: number[]) => string' is not assignable to type 'CoordinateFormat'.
类型 '(coordinate: number[]) => string' 不可分配给类型 'CoordinateFormat'。 The types of arguments 'coordinate' and 'arg0' are incompatible.
参数 'coordinate' 和 'arg0' 的类型不兼容。 Type 'number[] |
输入“号码[] | undefined' is not assignable to type 'number[]'.
undefined' 不能分配给类型 'number[]'。 Type 'undefined' is not assignable to type 'number[]'.
类型 'undefined' 不能分配给类型 'number[]'。 ts(2322)
ts(2322)
I find problem, and resolve it我发现问题并解决它
import { createStringXY, format, CoordinateFormat } from 'ol/coordinate';
import {
Component,
OnInit,
ChangeDetectionStrategy,
Input,
ElementRef,
} from '@angular/core';
import Map from 'ol/Map';
import ControlMousePosition from 'ol/control/MousePosition';
@Component({
selector: 'app-mouse-position',
template: ``,
styles: [`::ng-deep .ol-scale-line {
position: relative;
}
::ng-deep .ol-scale-line, ::ng-deep .ol-scale-line-inner {
background-color: transparent;
border-color: var(--text-color);
color: var(--text-color);
font-size: inherit;
bottom: auto;
}
`],
changeDetection: ChangeDetectionStrategy.OnPush,
})
export class MousePositionComponent implements OnInit {
@Input()
map!: Map;
@Input()
positionTemplate!: string;
control!: ControlMousePosition;
constructor(private element: ElementRef) {
}
ngOnInit() {
let customFormat = function(template: string): CoordinateFormat {
return (coordinate: number[] | undefined) => format(coordinate || [0, 0], template);
}
this.control = new ControlMousePosition({
className: 'mouseposition-control',
coordinateFormat: customFormat(this.positionTemplate),
target: this.element.nativeElement,
undefinedHTML: undefined,
});
this.map.addControl(this.control);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.