@@ -5,140 +5,140 @@ import { Canvas } from '../canvas';
55import { GestureHandler } from '../gesture' ;
66import type { IMapProps } from './types' ;
77import {
8- DEFAULT_ACTIVE_COLOR_LINE_WIDTH ,
9- DEFAULT_FILL_BACKGROUND_CANVAS ,
10- DEFAULT_BACKGROUND_VIEW_CANVAS ,
11- DEFAULT_INDEX_INITIAL_LAT_LNG ,
12- DEFAULT_CREATED_NEW_POLYGON ,
13- DEFAULT_ACTIVE_COLOR_LINE ,
14- DEFAULT_UNIT_DISTANCE ,
15- DEFAULT_DRAW_MODE ,
8+ DEFAULT_ACTIVE_COLOR_LINE_WIDTH ,
9+ DEFAULT_FILL_BACKGROUND_CANVAS ,
10+ DEFAULT_BACKGROUND_VIEW_CANVAS ,
11+ DEFAULT_INDEX_INITIAL_LAT_LNG ,
12+ DEFAULT_CREATED_NEW_POLYGON ,
13+ DEFAULT_ACTIVE_COLOR_LINE ,
14+ DEFAULT_UNIT_DISTANCE ,
15+ DEFAULT_DRAW_MODE ,
1616} from './contstant' ;
1717import * as _GEO from 'geolib' ;
1818import type { ILocationProps } from './types' ;
1919import useValidator from '../hooks/use-validator' ;
2020
2121export default forwardRef < MapView , IMapProps > ( ( props , ref ) => {
22- useValidator ( props ) ;
23- const {
24- points,
25- children,
26- colorLine = DEFAULT_ACTIVE_COLOR_LINE ,
27- widthLine = DEFAULT_ACTIVE_COLOR_LINE_WIDTH ,
28- onEndDraw,
29- isDrawMode = DEFAULT_DRAW_MODE ,
30- renderPath,
31- onStartDraw,
32- unitDistance = DEFAULT_UNIT_DISTANCE ,
33- onChangePoints,
34- createdPolygon = DEFAULT_CREATED_NEW_POLYGON ,
35- fillColorCanvas = DEFAULT_FILL_BACKGROUND_CANVAS ,
36- styleViewGesture,
37- backgroundCanvas = DEFAULT_BACKGROUND_VIEW_CANVAS ,
38- ...rest
39- } = props ;
40- const internalRef = useRef < MapView > ( null ) ;
41- const mapRef = ( ref as RefObject < MapView > ) || internalRef ;
22+ useValidator ( props ) ;
23+ const {
24+ points,
25+ children,
26+ colorLine = DEFAULT_ACTIVE_COLOR_LINE ,
27+ widthLine = DEFAULT_ACTIVE_COLOR_LINE_WIDTH ,
28+ onEndDraw,
29+ isDrawMode = DEFAULT_DRAW_MODE ,
30+ renderPath,
31+ onStartDraw,
32+ unitDistance = DEFAULT_UNIT_DISTANCE ,
33+ onChangePoints,
34+ createdPolygon = DEFAULT_CREATED_NEW_POLYGON ,
35+ fillColorCanvas = DEFAULT_FILL_BACKGROUND_CANVAS ,
36+ styleViewGesture,
37+ backgroundCanvas = DEFAULT_BACKGROUND_VIEW_CANVAS ,
38+ ...rest
39+ } = props ;
40+ const internalRef = useRef < MapView > ( null ) ;
41+ const mapRef = ( ref as RefObject < MapView > ) || internalRef ;
4242
43- const containerStyle = useMemo (
44- ( ) => [
45- { zIndex : 1 , backgroundColor : backgroundCanvas } ,
46- StyleSheet . absoluteFill ,
47- styleViewGesture ,
48- ] ,
49- [ backgroundCanvas , styleViewGesture ]
50- ) ;
43+ const containerStyle = useMemo (
44+ ( ) => [
45+ { zIndex : 1 , backgroundColor : backgroundCanvas } ,
46+ StyleSheet . absoluteFill ,
47+ styleViewGesture ,
48+ ] ,
49+ [ backgroundCanvas , styleViewGesture ] ,
50+ ) ;
51+
52+ const path = useMemo (
53+ ( ) => points . map ( ( item ) => `${ item . x } ,${ item . y } ` ) . join ( ' ' ) ,
54+ [ points ] ,
55+ ) ;
56+
57+ const calculatedCenterPolygon = ( coordinates : ILocationProps [ ] ) =>
58+ Promise . resolve ( _GEO . getCenter ( coordinates ) ) ;
59+
60+ const convertPointToCoordinates = useCallback (
61+ ( polygons ) => {
62+ if ( polygons && polygons . length > 0 ) {
63+ calculatedCenterPolygon ( polygons ) . then ( ( centerLatLng ) => {
64+ if ( onEndDraw && centerLatLng ) {
65+ const distance = _GEO . convertDistance (
66+ _GEO . getPathLength ( polygons ) ,
67+ unitDistance ,
68+ ) ;
69+ const initialLatLng = polygons [ DEFAULT_INDEX_INITIAL_LAT_LNG ] ;
70+ const lastLatLng = polygons [ polygons . length - 1 ] ;
71+ onEndDraw ( {
72+ polygons,
73+ distance,
74+ centerLatLng,
75+ initialLatLng,
76+ lastLatLng,
77+ } ) ;
78+ }
79+ } ) ;
80+ }
81+ } ,
82+ [ onEndDraw , unitDistance ] ,
83+ ) ;
5184
52- const path = useMemo (
53- ( ) => points . map ( ( item ) => ` ${ item . x } , ${ item . y } ` ) . join ( ' ' ) ,
54- [ points ]
55- ) ;
85+ const convertByPoint = useCallback (
86+ async ( item ) => await mapRef . current ?. coordinateForPoint ( item ) ,
87+ [ mapRef ] ,
88+ ) ;
5689
57- const calculatedCenterPolygon = ( coordinates : ILocationProps [ ] ) =>
58- Promise . resolve ( _GEO . getCenter ( coordinates ) ) ;
90+ const handleEndDraw = useCallback (
91+ async ( data ) => {
92+ await Promise . all ( data . map ( convertByPoint ) ) . then ( convertPointToCoordinates ) ;
93+ } ,
94+ [ convertByPoint , convertPointToCoordinates ] ,
95+ ) ;
5996
60- const convertPointToCoordinates = useCallback (
61- ( polygons ) => {
62- if ( polygons && polygons . length > 0 ) {
63- calculatedCenterPolygon ( polygons ) . then ( ( centerLatLng ) => {
64- if ( onEndDraw && centerLatLng ) {
65- const distance = _GEO . convertDistance (
66- _GEO . getPathLength ( polygons ) ,
67- unitDistance
68- ) ;
69- const initialLatLng = polygons [ DEFAULT_INDEX_INITIAL_LAT_LNG ] ;
70- const lastLatLng = polygons [ polygons . length - 1 ] ;
71- onEndDraw ( {
72- polygons,
73- distance,
74- centerLatLng,
75- initialLatLng,
76- lastLatLng,
77- } ) ;
78- }
79- } ) ;
80- }
81- } ,
82- [ onEndDraw , unitDistance ]
83- ) ;
97+ const hasCanvas = useMemo ( ( ) => {
98+ return (
99+ < View style = { containerStyle } >
100+ < >
101+ { renderPath ? (
102+ renderPath ( path )
103+ ) : (
104+ < Canvas
105+ path = { path }
106+ widthLine = { widthLine }
107+ colorLine = { colorLine }
108+ fillColorCanvas = { fillColorCanvas }
109+ />
110+ ) }
84111
85- const convertByPoint = useCallback (
86- async ( item ) => await mapRef . current ?. coordinateForPoint ( item ) ,
87- [ mapRef ]
88- ) ;
112+ < GestureHandler
113+ onEndTouchEvents = { handleEndDraw }
114+ onStartTouchEvents = { onStartDraw }
115+ onChangeTouchEvents = { onChangePoints }
116+ />
117+ </ >
118+ </ View >
119+ ) ;
120+ } , [
121+ path ,
122+ widthLine ,
123+ colorLine ,
124+ renderPath ,
125+ onStartDraw ,
126+ handleEndDraw ,
127+ containerStyle ,
128+ onChangePoints ,
129+ fillColorCanvas ,
130+ ] ) ;
89131
90- const handleEndDraw = useCallback (
91- async ( data ) => {
92- await Promise . all ( data . map ( convertByPoint ) ) . then ( convertPointToCoordinates ) ;
93- } ,
94- [ convertByPoint , convertPointToCoordinates ]
95- ) ;
132+ const hasMap = (
133+ < MapView scrollEnabled = { ! isDrawMode } ref = { mapRef } { ...rest } >
134+ { children }
135+ </ MapView >
136+ ) ;
96137
97- const hasCanvas = useMemo ( ( ) => {
98138 return (
99- < View style = { containerStyle } >
100139 < >
101- { renderPath ? (
102- renderPath ( path )
103- ) : (
104- < Canvas
105- path = { path }
106- widthLine = { widthLine }
107- colorLine = { colorLine }
108- fillColorCanvas = { fillColorCanvas }
109- />
110- ) }
111-
112- < GestureHandler
113- onEndTouchEvents = { handleEndDraw }
114- onStartTouchEvents = { onStartDraw }
115- onChangeTouchEvents = { onChangePoints }
116- />
140+ { hasMap }
141+ { ! createdPolygon && hasCanvas }
117142 </ >
118- </ View >
119143 ) ;
120- } , [
121- path ,
122- widthLine ,
123- colorLine ,
124- renderPath ,
125- onStartDraw ,
126- handleEndDraw ,
127- containerStyle ,
128- onChangePoints ,
129- fillColorCanvas ,
130- ] ) ;
131-
132- const hasMap = (
133- < MapView scrollEnabled = { ! isDrawMode } ref = { mapRef } { ...rest } >
134- { children }
135- </ MapView >
136- ) ;
137-
138- return (
139- < >
140- { ! createdPolygon && hasCanvas }
141- { hasMap }
142- </ >
143- ) ;
144144} ) ;
0 commit comments