antv-l7/stories/Layers/components/Text.tsx

105 lines
2.5 KiB
TypeScript
Raw Normal View History

import { PointLayer, Scene } from '@antv/l7';
2019-12-26 22:36:50 +08:00
import { GaodeMap, Mapbox } from '@antv/l7-maps';
2019-11-01 15:36:20 +08:00
import * as React from 'react';
2020-01-08 17:54:23 +08:00
import * as dat from 'dat.gui';
2019-12-26 22:36:50 +08:00
// @ts-ignore
2019-11-01 15:36:20 +08:00
import data from '../data/data.json';
2019-12-26 22:36:50 +08:00
export default class TextLayerDemo extends React.Component {
// @ts-ignore
2019-11-01 15:36:20 +08:00
private scene: Scene;
2020-01-08 17:54:23 +08:00
private gui: dat.GUI;
2019-11-01 15:36:20 +08:00
public componentWillUnmount() {
this.scene.destroy();
2020-01-08 17:54:23 +08:00
if (this.gui) {
this.gui.destroy();
}
2019-11-01 15:36:20 +08:00
}
2019-12-26 22:36:50 +08:00
public async componentDidMount() {
const response = await fetch(
'https://gw.alipayobjects.com/os/rmsportal/oVTMqfzuuRFKiDwhPSFL.json',
);
const pointsData = await response.json();
const scene = new Scene({
id: 'map',
2019-12-30 15:35:47 +08:00
map: new Mapbox({
2019-12-26 22:36:50 +08:00
center: [120.19382669582967, 30.258134],
pitch: 0,
style: 'dark',
zoom: 3,
}),
});
// scene.on('loaded', () => {
const pointLayer = new PointLayer({})
.source(pointsData.list, {
parser: {
type: 'json',
x: 'j',
y: 'w',
},
})
.shape('m', 'text')
2019-12-30 15:35:47 +08:00
.size(12)
2019-12-26 22:36:50 +08:00
.color('#fff')
.style({
2019-12-30 18:10:21 +08:00
// fontWeight: 200,
// textAnchor: 'center', // 文本相对锚点的位置 center|left|right|top|bottom|top-left
// textOffset: [0, 0], // 文本相对锚点的偏移量 [水平, 垂直]
// spacing: 2, // 字符间距
// padding: [1, 1], // 文本包围盒 padding [水平,垂直],影响碰撞检测结果,避免相邻文本靠的太近
// stroke: 'red', // 描边颜色
// strokeWidth: 2, // 描边宽度
// strokeOpacity: 1.0,
2019-12-26 22:36:50 +08:00
});
2019-11-01 15:36:20 +08:00
scene.addLayer(pointLayer);
pointLayer.on('click', (e) => {
console.log(e);
});
2019-12-26 22:36:50 +08:00
2019-11-01 15:36:20 +08:00
this.scene = scene;
2020-01-08 17:54:23 +08:00
const gui = new dat.GUI();
this.gui = gui;
const styleOptions = {
textAnchor: 'center',
strokeWidth: 1,
};
const rasterFolder = gui.addFolder('栅格可视化');
rasterFolder
.add(styleOptions, 'textAnchor', [
'center',
'left',
'right',
'top',
'bottom',
'top-left',
'bottom-right',
'bottom-left',
'top-right',
])
.onChange((anchor: string) => {
pointLayer.style({
textAnchor: anchor,
});
scene.render();
});
2019-12-26 22:36:50 +08:00
// });
2019-11-01 15:36:20 +08:00
}
public render() {
return (
<div
id="map"
style={{
position: 'absolute',
top: 0,
left: 0,
right: 0,
bottom: 0,
}}
/>
);
}
}