import { useMapStore } from '@/store/map';
import { Handle } from 'esri';
import { loadModules } from 'esri-loader';
import React, { useEffect, useMemo, useRef } from 'react';
import './index.less';
interface Props {
children: React.ReactNode;
location: { x: number; y: number };
position: 'left' | 'center';
}
type MapModules = [typeof import('esri/geometry/Point')];
type PointModules = import('esri/geometry/Point');
const renderDom = (map: Maps, dom: HTMLDivElement, point: PointModules, position: 'left' | 'center') => {
const screen = point ? map.toScreen(point) : { x: -200, y: -200 };
let childWidth = dom.clientWidth;
let childHeight = dom.clientHeight;
if (childWidth && childHeight) {
if (position === 'center') {
dom.style.left = screen.x - childWidth / 2 + 'px';
dom.style.top = screen.y - childHeight / 2 + 'px';
}
if (position === 'left') {
dom.style.left = screen.x + 'px';
dom.style.top = screen.y - childHeight / 2 + 'px';
}
}
};
export default function Marker(props: Props) {
const { state } = useMapStore();
const { children, location, position = 'center' } = props;
const domRef = useRef(null);
const handleRef = useRef<Handle>();
useEffect(() => {
if (state && state.map && location && domRef.current) {
let map = state.map;
const dom = domRef.current;
const { x, y } = location;
loadModules(['esri/geometry/Point']).then(res => {
const [Point] = res as MapModules;
const point = new Point(x, y, map.spatialReference);
renderDom(map, dom, point, position);
map.on('extent-change', e => {
renderDom(map, dom, point, position);
});
});
}
}, [state, location]);
return (
<div ref={domRef} className="Marker">
{children && children}
</div>
);
}
css
.Marker {
position: fixed;
z-index: 200;
}
使用方法
townsTotal.map((item: any, index: number) => {
return (
<Marker location={{ x: item.x, y: item.y }} key={index} position={'center'}>
<TownsCircle
options={{
number: item.value,
unit: '',
name: item.name_new,
}}
/>
</Marker>
);
})}
文章版权声明:除非注明,否则均为
譬如朝露_策温技术开发工作室博客原创文章,转载或复制请以超链接形式并注明出处。
发表评论
侧栏公告
寄语
譬如朝露博客是一个分享前端知识的网站,联系方式11523518。
热评文章
标签列表
热门文章
友情链接