antv-l7/examples/point/text/demo/iconfonts.js

268 lines
5.9 KiB
JavaScript
Raw Normal View History

2021-05-27 19:53:22 +08:00
import { Scene, PointLayer } from '@antv/l7';
import { GaodeMap } from '@antv/l7-maps';
const scene = new Scene({
id: 'map',
map: new GaodeMap({
2021-05-27 19:55:06 +08:00
center: [ 120.5, 30.2 ],
2021-05-27 19:53:22 +08:00
pitch: 0,
style: 'dark',
2021-05-27 19:53:22 +08:00
zoom: 8.5,
zooms: [ 8, 10 ]
2021-05-27 19:55:06 +08:00
})
2021-05-27 19:53:22 +08:00
});
2021-05-31 20:04:34 +08:00
const dataColor = {
2021-05-31 20:39:15 +08:00
bigRainBC: '#285A8C',
middleRainBC: '#326EA0',
smallRainBC: '#4678AA',
sunBC: '#00BFFF',
cloudBC: '#1E90FF'
};
2021-05-27 19:55:06 +08:00
const originData = [
2021-05-31 10:34:15 +08:00
{
lng: 121.7,
lat: 30.6,
iconType: 'hugeRain',
iconColor: '#4678D2',
2021-05-31 20:04:34 +08:00
backgoundColor: dataColor.bigRainBC,
2021-05-31 10:34:15 +08:00
temperature: '20℃',
weather: '大雨'
},
{
lng: 119.2,
2021-05-31 10:37:14 +08:00
lat: 30.0,
2021-05-31 10:34:15 +08:00
iconType: 'smallRain',
iconColor: '#6EA0FF',
2021-05-31 20:04:34 +08:00
backgoundColor: dataColor.smallRainBC,
2021-05-31 10:34:15 +08:00
temperature: '22℃',
weather: '小雨'
},
{
lng: 119.67,
lat: 30.2,
iconType: 'sun',
iconColor: '#FFA500',
2021-05-31 20:04:34 +08:00
backgoundColor: dataColor.sunBC,
2021-05-31 10:34:15 +08:00
temperature: '28℃',
weather: '晴朗'
},
{
lng: 119.63,
lat: 30.6,
iconType: 'sun',
iconColor: '#FFA500',
2021-05-31 20:04:34 +08:00
backgoundColor: dataColor.sunBC,
2021-05-31 10:34:15 +08:00
temperature: '28℃',
weather: '晴朗'
},
2021-05-27 19:53:22 +08:00
{
lng: 120,
lat: 30,
iconType: 'sun',
2021-05-27 19:55:06 +08:00
iconColor: '#FFA500',
2021-05-31 20:04:34 +08:00
backgoundColor: dataColor.sunBC,
2021-05-27 19:53:22 +08:00
temperature: '28℃',
weather: '晴朗'
},
{
lng: 120.2,
lat: 30.5,
iconType: 'sun',
2021-05-27 19:55:06 +08:00
iconColor: '#FFA500',
2021-05-31 20:04:34 +08:00
backgoundColor: dataColor.sunBC,
2021-05-27 19:53:22 +08:00
temperature: '28℃',
weather: '晴朗'
},
{
lng: 121.5,
lat: 31.4,
iconType: 'cloud',
2021-05-27 19:55:06 +08:00
iconColor: '#F0F8FF',
2021-05-31 20:04:34 +08:00
backgoundColor: dataColor.cloudBC,
2021-05-27 19:53:22 +08:00
temperature: '22℃',
weather: '多云'
},
{
lng: 120,
lat: 31,
iconType: 'cloud',
2021-05-27 19:55:06 +08:00
iconColor: '#F0F8FF',
2021-05-31 20:04:34 +08:00
backgoundColor: dataColor.cloudBC,
2021-05-27 19:53:22 +08:00
temperature: '22℃',
weather: '多云'
},
{
lng: 120.6,
lat: 30.8,
iconType: 'cloud',
2021-05-27 19:55:06 +08:00
iconColor: '#F0F8FF',
2021-05-31 20:04:34 +08:00
backgoundColor: dataColor.cloudBC,
2021-05-27 19:53:22 +08:00
temperature: '22℃',
weather: '多云'
},
{
lng: 120.5,
lat: 31.3,
iconType: 'cloud',
2021-05-27 19:55:06 +08:00
iconColor: '#F0F8FF',
2021-05-31 20:04:34 +08:00
backgoundColor: dataColor.cloudBC,
2021-05-27 19:53:22 +08:00
temperature: '22℃',
weather: '多云'
},
{
lng: 121.3,
lat: 30.2,
iconType: 'smallRain',
2021-05-27 19:55:06 +08:00
iconColor: '#6EA0FF',
2021-05-31 20:04:34 +08:00
backgoundColor: dataColor.smallRainBC,
2021-05-27 19:53:22 +08:00
temperature: '22℃',
weather: '小雨'
},
{
lng: 121,
lat: 30.5,
iconType: 'smallRain',
2021-05-27 19:55:06 +08:00
iconColor: '#6EA0FF',
2021-05-31 20:04:34 +08:00
backgoundColor: dataColor.smallRainBC,
2021-05-27 19:53:22 +08:00
temperature: '22℃',
weather: '小雨'
},
{
lng: 120.6,
lat: 30,
iconType: 'middleRain',
2021-05-27 19:55:06 +08:00
iconColor: '#6495ED',
2021-05-31 20:04:34 +08:00
backgoundColor: dataColor.middleRainBC,
2021-05-27 19:53:22 +08:00
temperature: '24℃',
weather: '中雨'
},
{
lng: 120.2,
lat: 29.7,
iconType: 'smallRain',
2021-05-27 19:55:06 +08:00
iconColor: '#6EA0FF',
2021-05-31 20:04:34 +08:00
backgoundColor: dataColor.smallRainBC,
2021-05-27 19:53:22 +08:00
temperature: '22℃',
weather: '小雨'
},
{
lng: 121.7,
lat: 29.8,
iconType: 'middleRain',
2021-05-27 19:55:06 +08:00
iconColor: '#6495ED',
2021-05-31 20:04:34 +08:00
backgoundColor: dataColor.middleRainBC,
2021-05-27 19:53:22 +08:00
temperature: '24℃',
weather: '中雨'
},
{
lng: 121.5,
lat: 30,
iconType: 'hugeRain',
2021-05-27 19:55:06 +08:00
iconColor: '#4678D2',
2021-05-31 20:04:34 +08:00
backgoundColor: dataColor.bigRainBC,
2021-05-27 19:53:22 +08:00
temperature: '20℃',
weather: '大雨'
2021-05-27 19:55:06 +08:00
}
2021-05-27 19:53:22 +08:00
];
2021-05-27 19:55:06 +08:00
const fontFamily = 'iconfont';
const fontPath = '//at.alicdn.com/t/font_2534097_ao9soua2obv.woff2?t=1622021146076';
2021-05-27 19:53:22 +08:00
scene.addFontFace(fontFamily, fontPath);
scene.addIconFonts([
2021-05-27 19:55:06 +08:00
[ 'smallRain', '' ],
[ 'middleRain', '' ],
[ 'hugeRain', '' ],
[ 'sun', '' ],
[ 'cloud', '' ]
2021-05-27 19:53:22 +08:00
]);
scene.on('loaded', () => {
2021-05-27 19:55:06 +08:00
const layer = new PointLayer()
.source(originData, {
parser: {
type: 'json',
x: 'lng',
y: 'lat'
}
})
.shape('circle')
.color('backgoundColor')
2021-05-31 10:34:15 +08:00
.size(42);
2021-05-27 19:55:06 +08:00
scene.addLayer(layer);
2021-05-27 19:53:22 +08:00
2021-05-27 19:55:06 +08:00
const pointIconFontLayer = new PointLayer({})
.source(originData, {
parser: {
type: 'json',
x: 'lng',
y: 'lat'
}
}
)
.shape('iconType', 'text')
.size(30)
.color('iconColor')
.style({
textAnchor: 'center', // 文本相对锚点的位置 center|left|right|top|bottom|top-left
textOffset: [ 38, 10 ], // 文本相对锚点的偏移量 [水平, 垂直]
fontFamily,
iconfont: true,
textAllowOverlap: true
});
scene.addLayer(pointIconFontLayer);
2021-05-27 19:53:22 +08:00
2021-05-27 19:55:06 +08:00
const textLayer = new PointLayer({})
.source(originData,
{
parser: {
type: 'json',
x: 'lng',
y: 'lat'
}
}
)
.shape('temperature', 'text')
.size(10)
.color('#ffffff')
.style({
textAnchor: 'center', // 文本相对锚点的位置 center|left|right|top|bottom|top-left
textOffset: [ 5, -55 ], // 文本相对锚点的偏移量 [水平, 垂直]
spacing: 2, // 字符间距
padding: [ 1, 1 ], // 文本包围盒 padding [水平,垂直],影响碰撞检测结果,避免相邻文本靠的太近
stroke: '#ffffff', // 描边颜色
strokeWidth: 0.3, // 描边宽度
strokeOpacity: 1.0,
fontFamily: 'Times New Roman',
textAllowOverlap: true
});
scene.addLayer(textLayer);
const textLayer2 = new PointLayer({})
.source(originData,
{
parser: {
type: 'json',
x: 'lng',
y: 'lat'
}
}
)
.shape('weather', 'text')
.size(14)
.color('#ffffff')
.style({
textAnchor: 'center', // 文本相对锚点的位置 center|left|right|top|bottom|top-left
textOffset: [ 5, -15 ], // 文本相对锚点的偏移量 [水平, 垂直]
spacing: 2, // 字符间距
padding: [ 1, 1 ], // 文本包围盒 padding [水平,垂直],影响碰撞检测结果,避免相邻文本靠的太近
stroke: '#ffffff', // 描边颜色
strokeWidth: 0.3, // 描边宽度
strokeOpacity: 1.0,
fontFamily: 'Times New Roman',
textAllowOverlap: true
});
scene.addLayer(textLayer2);
2021-05-27 19:53:22 +08:00
});