Skip to content

Commit

Permalink
fix: fix LabelMarker/polyline/polygon bugs (#282)
Browse files Browse the repository at this point in the history
* feat:修复 LabelMarker,以便兼容高德地图JS API v1.4.xxx。

* fix:修复细节

* feat:添加Polyline-Editor Poly-Editor组件项目,修改PolyLine、Polygon、PolygonEditor组件内容

* feat:添加MouseTool组件项目,以便支持 MouseTool 插件功能。

* fix:修复一些问题,如 polyline、polygon 的受控 path 属性,还有些 api 的兼容处理

* fix:修复自测过程中遇到的一些问题,readme 中的示例或是一些控制细节
  • Loading branch information
peidongGuo authored Jan 5, 2023
1 parent b998aa9 commit aec8380
Show file tree
Hide file tree
Showing 17 changed files with 71 additions and 28 deletions.
4 changes: 3 additions & 1 deletion packages/bezier-curve/src/useBezierCurve.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ export const useBezierCurve = (props = {} as UseBezierCurve) => {
setBezierCurve(instance);
return () => {
if (instance) {
map && map.removeLayer(instance);
// 暂不使用这个 API,这个不兼容 v1.4.xx,改用 map.remove API
// map && map.removeLayer(instance);
map && map.remove(instance);
setBezierCurve(undefined);
}
};
Expand Down
4 changes: 3 additions & 1 deletion packages/circle-marker/src/useCircleMarker.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export const useCircleMarker = (props = {} as UseCircleMarker) => {
setCircleMarker(instance);
return () => {
if (instance) {
map && map.removeLayer(instance);
// 暂不使用这个 API,这个不兼容 v1.4.xx,改用 map.remove API
// map && map.removeLayer(instance);
map && map.remove(instance);
setCircleMarker(undefined);
}
};
Expand Down
2 changes: 1 addition & 1 deletion packages/context-menu/src/useContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ export const useContextMenu = (props = {} as UseContextMenu) => {
map.on('rightclick', rightclick);
return () => {
if (instance) {
instance.close();
map.off('rightclick', rightclick);
map && map.removeLayer(instance);
setContextMenu(undefined);
}
};
Expand Down
4 changes: 3 additions & 1 deletion packages/ellipse/src/useEllipse.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export const useEllipse = (props = {} as UseEllipse) => {
setEllipse(instance);
return () => {
if (instance) {
map && map.removeLayer(instance);
// 暂不使用这个 API,这个不兼容 v1.4.xx,改用 map.remove API
// map && map.removeLayer(instance);
map && map.remove(instance);
setEllipse(undefined);
}
};
Expand Down
2 changes: 1 addition & 1 deletion packages/hawk-eye-control/src/useHawkEyeControl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function useHawkEyeControl(props = {} as UseHawkEyeControl) {

return () => {
if (instance && map) {
map.removeLayer(instance);
map && map.removeControl(instance);
setHawkEyeControl(undefined);
}
};
Expand Down
14 changes: 9 additions & 5 deletions packages/poly-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ const Example = () => {
[116.402292, 39.892353],
[116.389846, 39.891365],
];
const [polygonPath,setPolygonPath]=useState(path);
return (
<>
<button onClick={() => setShow(!show)}>
Expand All @@ -49,7 +50,7 @@ const Example = () => {
<Map zoom={14} center={[116.400274, 39.905812]}>
<Polygon
visiable={show}
path={path}
path={polygonPath}
strokeColor="#FF33FF"
strokeWeight={6}
strokeOpacity={0.2}
Expand All @@ -60,7 +61,8 @@ const Example = () => {
<PolyEditor
active={active}
onEnd={(e) => {
console.log('onEnd:>>',e)
console.log('onEnd:>>',e.target.getPath());
setPolygonPath(e.target.getPath())
}}
onAdjust={() => {
console.log('onAdjust:>>')
Expand Down Expand Up @@ -106,6 +108,7 @@ const Example = () => {
[116.402292, 39.892353],
[116.389846, 39.891365],
];
const [polylinePath,setPolylinePath]=useState(path);
return (
<>
<button onClick={() => setShow(!show)}>
Expand All @@ -118,7 +121,7 @@ const Example = () => {
<Map zoom={14} center={[116.400274, 39.905812]}>
<Polyline
visiable={show}
path={path}
path={polylinePath}
strokeColor="#FF33FF"
strokeWeight={6}
strokeOpacity={0.2}
Expand All @@ -128,8 +131,9 @@ const Example = () => {
>
<PolyEditor
active={active}
onEnd={(e) => {
console.log('onEnd:>>',e)
onEnd={(e) => {
console.log('onEnd:>>',e.target.getPath());
setPolylinePath(e.target.getPath())
}}
onAdjust={() => {
console.log('onAdjust:>>')
Expand Down
2 changes: 2 additions & 0 deletions packages/poly-editor/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@ export const PolyEditor = forwardRef<PolyEditorProps, PolyEditorProps>((props, r
}
if (visiable && !active) {
polyEditor.close();
props.onEnd && props.onEnd({ type: 'end', target: props.polyElement as any });
} else if (visiable && active) {
polyEditor.open();
} else if (!visiable && active) {
polyEditor.close();
props.onEnd && props.onEnd({ type: 'end', target: props.polyElement as any });
}
}, [active, visiable]);

Expand Down
9 changes: 6 additions & 3 deletions packages/polygon-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@ const Example = () => {
[116.402292, 39.892353],
[116.389846, 39.891365],
];
const [polygonPath,setPolygonPath]=useState(path);

return (
<>
<button onClick={() => setShow(!show)}>
Expand All @@ -46,7 +48,7 @@ const Example = () => {
<Map zoom={14} center={[116.400274, 39.905812]}>
<Polygon
visiable={show}
path={path}
path={polygonPath}
strokeColor="#FF33FF"
strokeWeight={6}
strokeOpacity={0.2}
Expand All @@ -56,8 +58,9 @@ const Example = () => {
>
<PolygonEditor
active={active}
onEnd={() => {
console.log('onEnd:>>')
onEnd={(e) => {
console.log('onEnd:>>',e.target.getPath());
setPolygonPath(e.target.getPath())
}}
onAdjust={() => {
console.log('onAdjust:>>')
Expand Down
3 changes: 3 additions & 0 deletions packages/polygon-editor/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,13 @@ export const PolygonEditor = forwardRef<PolygonEditorProps, PolygonEditorProps>(
}
if (visiable && !active) {
polyEditor.close();
props.onEnd && props.onEnd({ target: props.polygon as any });
} else if (visiable && active) {
polyEditor.open();
props.onAdd && props.onAdd({ target: props.polygon as any });
} else if (!visiable && active) {
polyEditor.close();
props.onEnd && props.onEnd({ target: props.polygon as any });
}
}, [active, visiable]);

Expand Down
2 changes: 1 addition & 1 deletion packages/polygon/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface PolygonProps extends OverlayProps, AMap.PolygonEvents, AMap.Pol
export const Polygon = forwardRef<PolygonProps, PolygonProps>((props, ref) => {
const { children } = props;
const { polygon } = usePolygon(props);
useImperativeHandle(ref, () => ({ ...props, polygon }));
useImperativeHandle(ref, () => ({ ...props, polygon }), [polygon]);
if (children && isValidElement(children) && polygon) {
const oProps = { polygon, polyElement: polygon };
return cloneElement(children, { ...props, ...oProps });
Expand Down
10 changes: 9 additions & 1 deletion packages/polygon/src/usePolygon.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,19 @@ export const usePolygon = (props = {} as UsePolygon) => {
let instance: AMap.Polygon = new AMap.Polygon({ ...other });
map.add(instance);
setPolygon(instance);
return () => {
if (instance) {
// 暂不使用这个 API,这个不兼容 v1.4.xx,改用 map.remove API
// map && map.removeLayer(instance);
map && map.remove(instance);
setPolygon(undefined);
}
};
}
}, [map]);

useVisiable(polygon!, visiable);
useSettingProperties<AMap.Polygon, UsePolygon>(polygon!, props, ['ExtData', 'ExtData']);
useSettingProperties<AMap.Polygon, UsePolygon>(polygon!, props, ['Path', 'Options', 'Map', 'ExtData']);
useEventProperties<AMap.Polygon, UsePolygon>(polygon!, props, [
'onClick',
'onDblClick',
Expand Down
13 changes: 8 additions & 5 deletions packages/polyline-editor/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,17 @@ import React, { useState, useRef } from 'react';
import { Map, APILoader, Polyline, PolylineEditor } from '@uiw/react-amap';

const Example = () => {
const [show, setShow] = useState(true);
const [active, setActive] = useState(false);
// 多边形轮廓线的节点坐标数组
const path = [
[116.403322, 39.920255],
[116.410703, 39.897555],
[116.402292, 39.892353],
[116.389846, 39.891365],
];
const [show, setShow] = useState(true);
const [active, setActive] = useState(false);
const [polylinePath,setPolylinePath]=useState(path);

return (
<>
<button onClick={() => setShow(!show)}>
Expand All @@ -46,7 +48,7 @@ const Example = () => {
<Map zoom={14} center={[116.400274, 39.905812]}>
<Polyline
visiable={show}
path={path}
path={polylinePath}
strokeColor="#FF33FF"
strokeWeight={6}
strokeOpacity={0.2}
Expand All @@ -56,8 +58,9 @@ const Example = () => {
>
<PolylineEditor
active={active}
onEnd={() => {
console.log('onEnd:>>')
onEnd={(e) => {
console.log('onEnd:>>',e.target.getPath());
setPolylinePath(e.target.getPath())
}}
onAdjust={() => {
console.log('onAdjust:>>')
Expand Down
5 changes: 4 additions & 1 deletion packages/polyline-editor/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,15 @@ export const PolylineEditor = forwardRef<PolylineEditorProps, PolylineEditorProp
}
if (visiable && !active) {
polyEditor.close();
props.onEnd && props.onEnd({ target: props.polyline as any });
} else if (visiable && active) {
polyEditor.open();
props.onAdd && props.onAdd({ target: props.polyline as any });
} else if (!visiable && active) {
polyEditor.close();
props.onEnd && props.onEnd({ target: props.polyline as any });
}
}, [active, visiable]);
}, [active, visiable, polyEditor]);

useEventProperties<AMap.PolylineEditor, AMap.PolylineEditorEvents>(polyEditor!, props, [
'onEnd',
Expand Down
6 changes: 4 additions & 2 deletions packages/polyline/src/usePolyline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@ export function usePolyline(props = {} as UsePolyline) {
setPolyline(instance);
return () => {
if (instance) {
map && map.removeLayer(instance);
// 暂不使用这个 API,这个不兼容 v1.4.xx,改用 map.remove API
// map && map.removeLayer(instance);
map && map.remove(instance);
setPolyline(undefined);
}
};
}
}, [map]);

useVisiable(polyline!, visiable);
useSettingProperties<AMap.Polyline, UsePolyline>(polyline!, props, ['Options', 'Map', 'ExtData']);
useSettingProperties<AMap.Polyline, UsePolyline>(polyline!, props, ['Path', 'Options', 'Map', 'ExtData']);
useEventProperties<AMap.Polyline, UsePolyline>(polyline!, props, [
'onHide',
'onShow',
Expand Down
4 changes: 3 additions & 1 deletion packages/rectangle/src/useRectangle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ export const useRectangle = (props = {} as UseRectangle) => {
setRectangle(instance);
return () => {
if (instance) {
map && map.removeLayer(instance);
// 暂不使用这个 API,这个不兼容 v1.4.xx,改用 map.remove API
// map && map.removeLayer(instance);
map && map.remove(instance);
setRectangle(undefined);
}
};
Expand Down
5 changes: 2 additions & 3 deletions packages/tile-layer/src/useTileLayer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,8 @@ export function useTileLayer(props = {} as UseTileLayer) {
return () => {
if (instance) {
// 暂不使用这个 API,这个不兼容 v1.4.xx
// map.removeLayer(instance);

map.remove(instance);
// map && map.removeLayer(instance);
map && map.remove(instance);
setTileLayer(null as any);
props.onRemoved && props.onRemoved();
}
Expand Down
10 changes: 9 additions & 1 deletion packages/types/src/overlay.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,12 @@ declare namespace AMap {
contains(point: LngLatLike): boolean;

}
interface PolylineEvents extends EventsCommonProps { }
interface PolylineEvents extends EventsCommonProps {
onDragStart?(event:MapsEvent):void;
onDragEnd?(event:MapsEvent):void;
onDragging?(event:MapsEvent):void;
}

interface PolylineOptions {
/**
* polyline 路径,支持 lineString 和 MultiLineString
Expand Down Expand Up @@ -894,6 +899,9 @@ declare namespace AMap {
strokeDasharray?: Array<number>;
}
interface PolygonEvents extends EventsCommonProps {
onDragStart?(event:MapsEvent):void;
onDragEnd?(event:MapsEvent):void;
onDragging?(event:MapsEvent):void;
/** 属性发生变化时 */
onChange?(): void;
}
Expand Down

1 comment on commit aec8380

@vercel
Copy link

@vercel vercel bot commented on aec8380 Jan 5, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

react-amap – ./

react-amap-git-master-398188662.vercel.app
react-amap-one.vercel.app
react-amap-398188662.vercel.app

Please sign in to comment.