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

97 lines
2.3 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';
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;
public componentWillUnmount() {
this.scene.destroy();
}
2019-12-26 22:36:50 +08:00
public async componentDidMount() {
const data = {
2019-11-01 15:36:20 +08:00
type: 'FeatureCollection',
features: [
{
type: 'Feature',
2019-12-26 22:36:50 +08:00
properties: {
name: '中华人民共和国',
},
geometry: {
type: 'Point',
coordinates: [103.0078125, 36.03133177633187],
},
},
{
type: 'Feature',
properties: {
name: '中华人民共和国',
},
2019-11-01 15:36:20 +08:00
geometry: {
type: 'Point',
2019-12-26 22:36:50 +08:00
coordinates: [122.6953125, 10.833305983642491],
2019-11-01 15:36:20 +08:00
},
},
],
};
2019-12-26 22:36:50 +08:00
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 15:35:47 +08:00
fontWeight: 200,
2019-12-26 22:36:50 +08:00
textAnchor: 'center', // 文本相对锚点的位置 center|left|right|top|bottom|top-left
textOffset: [0, 0], // 文本相对锚点的偏移量 [水平, 垂直]
spacing: 2, // 字符间距
2019-12-30 15:35:47 +08:00
padding: [1, 1], // 文本包围盒 padding [水平,垂直],影响碰撞检测结果,避免相邻文本靠的太近
stroke: 'red', // 描边颜色
strokeWidth: 2, // 描边宽度
2019-12-26 22:36:50 +08:00
strokeOpacity: 1.0,
});
2019-11-01 15:36:20 +08:00
scene.addLayer(pointLayer);
2019-12-26 22:36:50 +08:00
2019-11-01 15:36:20 +08:00
this.scene = scene;
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,
}}
/>
);
}
}