import React, { useMemo, useState } from 'react'; import { Amap, Marker, InfoWindow, Polyline } from '@amap/amap-react'; import type { MapConfig, MapValue } from '../index'; export interface MapLineProps extends MapConfig { value?: Partial[]; showArrow?: boolean; strokeColor?: string; strokeWeight?: number; showPoint?: boolean; } export function Line(props: MapLineProps) { const path = useMemo(() => { if (!props.value) return []; const data: [number, number][] = []; props.value.forEach((item) => { if (item.coordinate) data.push(item.coordinate); else if (item.longitude && item.latitude) data.push([item.longitude, item.latitude]); }); return data; }, [props.value]); const first = useMemo(() => { if (!path.length) return undefined; return path[0]; }, [path]); const last = useMemo(() => { if (path.length < 2) return undefined; return path[path.length - 1]; }, [path]); const [active, setActive] = useState<[number, number] | undefined>(); return (
{props.headerTip && (
{props.headerTip}
)} {props.showPoint && first && ( { if (props.showTip) setActive(first); }} /> )} {props.showPoint && last && ( { if (props.showTip) setActive(last); }} /> )} {props.showTip && active && ( { setActive(undefined); }} content={active.join(',')} /> )}
); }