mapbox 添加散点图

import React, { memo, useEffect } from 'react';
import axios from 'axios';
import turfBbox from '@turf/bbox';

import config from '../../../config/api';
import { generateGeojsonData } from '../../../util/layerService';
const { geojson: { GET_URL } } = config;

const PointScatter = memo((props) => {
    const { map, layer: { layerId: id, serviceInfo, styleState }, changeServiceInfo } = props;

    // 构造id
    const layerId = `thematicLayer_geojson_pointScatter_layer_${id}`;
    const sourceId = `thematicLayer_geojson_pointScatter_source_${id}`;

    // 图层组件卸载需要移除该图层
    useEffect(() => {
        return () => {
            if (map.getLayer(layerId)) {
                map.removeLayer(layerId);
                map.removeSource(sourceId);
            }

        };
    }, [layerId, sourceId]);

    useEffect(() => {

        let { bounds, path, legend } = JSON.parse(serviceInfo);
        console.log('11111',JSON.parse(serviceInfo));
        const { radius, color, hoverColor, opacity, strokeWidth, strokeColor, strokeOpacity } = JSON.parse(styleState);
        let sactterDataSource = null; // 散点图源数据

        // 判断该图层是否已存在,已经存在,可以设置图层样式
        if (map.getLayer(layerId)) {
            map.setPaintProperty(layerId, 'circle-opacity', 1 - opacity / 100);
            map.setPaintProperty(layerId, 'circle-stroke-opacity', 1 - strokeOpacity / 100);
            map.setPaintProperty(layerId, 'circle-stroke-color', `rgb(${strokeColor.r},${strokeColor.g},${strokeColor.b})`);
            map.setPaintProperty(layerId, 'circle-stroke-width', strokeWidth);
            map.setPaintProperty(layerId, 'circle-color', ["case",
                ["boolean", ["feature-state", "hover"], false],
                `rgb(${hoverColor.r},${hoverColor.g},${hoverColor.b})`,
                `rgb(${color.r},${color.g},${color.b})`
            ]);
            map.setPaintProperty(layerId, 'circle-radius', radius);
            return;
        }

        // 获取专题数据
        function getThematicDatas() {
            return new Promise((resolve, reject) => {
                axios.get(path).then((res) => {
                    try {
                        if (res.status !== 200) {
                            return null;
                        }
                        const results = res.data.data.results;
                        resolve(results);
                    } catch (error) {
                        reject(new Error(error));
                    }
                });
            });
        }

        getThematicDatas()
            .then(results => {
                sactterDataSource = generateGeojsonData(results, 'pointScatter');
                bounds = turfBbox(sactterDataSource);
                // 修改目录树中bounds值
                changeServiceInfo({ id, bounds, legend });
                addLayer();
            })
        function addLayer(){
            map.addSource(sourceId, {
                type: 'geojson',
                data: sactterDataSource,
                generateId: true
            });

            const pointLayer = {
                'id': layerId,
                'source': sourceId,
                'type': 'circle',
                'paint': {
                    'circle-radius': radius,
                    "circle-color": ["case",
                        ["boolean", ["feature-state", "hover"], false],
                        `rgb(${hoverColor.r},${hoverColor.g},${hoverColor.b})`,
                        `rgb(${color.r},${color.g},${color.b})`
                    ],
                    'circle-opacity': 1 - opacity / 100,
                    'circle-stroke-width': strokeWidth,
                    'circle-stroke-color': `rgb(${strokeColor.r},${strokeColor.g},${strokeColor.b})`,
                    'circle-stroke-opacity': 1 - strokeOpacity / 100
                }
            };

            map.addLayer(pointLayer);
            map.fitBounds(bounds);
        }
        
    }, [layerId, sourceId, map, serviceInfo, styleState]);

    return null;
});

export default PointScatter;

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值