66<br >
77
88<h1 align =" center " >React Native Maps Draw (Polygon)</h1 >
9- <p align =" center " >Performance oriented React Native Accordion 60 FPS. A simple component of a common use case of collapsible - a visible title with a collapsible view beneath it. </p >
9+ <p align =" center " >Interactive drawing of polygons on the map. Beta version </p >
1010<h6 align =" center " >Made with ❤️ by developer for developers</h6 >
1111
1212<br >
1313<p align =" center " >
1414<img src =" http://img.shields.io/travis/badges/badgerbadgerbadger.svg?style=flat-square " alt =" build " />
15- <img src =" https://img.shields.io/github/issues/dev-event/react-native-accordion " alt =" build " />
16- <img src =" https://img.shields.io/bitbucket/pr-raw/dev-event/react-native-accordion " alt =" build " />
15+ <img src =" https://img.shields.io/github/issues/dev-event/react-native-maps-draw " alt =" build " />
16+ <img src =" https://img.shields.io/bitbucket/pr-raw/dev-event/react-native-maps-draw " alt =" build " />
1717<img src =" http://img.shields.io/:license-mit-blue.svg?style=flat-square " alt =" build " />
1818</p >
1919
@@ -51,9 +51,140 @@ Big love and gratitude to all people who are working on React Native, Expo and R
5151
5252## Usage
5353
54- For more complete example open [ App.tsx] ( https://github.com/dev-event/react-native-accordion )
54+ For more complete example open [ App.tsx] ( https://github.com/dev-event/react-native-maps-draw/blob/main/example/src/App.tsx )
5555
5656``` tsx
57+ import React , { useState , useCallback , useRef } from ' react' ;
58+ import { StyleSheet , View , TouchableOpacity , Text } from ' react-native' ;
59+ import MapView , {
60+ ILocationProps ,
61+ IDrawResult ,
62+ TouchPoint ,
63+ Marker
64+ } from ' react-native-maps-draw' ;
65+
66+
67+ const App: React .FC = () => {
68+ const mapRef = useRef <MapView >(null );
69+
70+ const initialPolygon = useRef ({
71+ polygons: [],
72+ distance: 0 ,
73+ lastLatLng: undefined ,
74+ initialLatLng: undefined ,
75+ centerLatLng: undefined ,
76+ });
77+
78+ const [modePolygon, setPolygonCreated] = useState <boolean >(false );
79+
80+ const [isActiveDraw, setDrawMode] = useState <boolean >(false );
81+ const [isReady, setIsReady] = useState <boolean >(false );
82+ const [points, setPoints] = useState <TouchPoint []>([]);
83+
84+ const [polygon, setPolygon] = useState <IDrawResult >(initialPolygon .current );
85+
86+ const handleMapReady = useCallback (() => mapRef .current && setIsReady (true ), []);
87+
88+ const handleRemovePolygon = useCallback (() => {
89+ setPolygon (initialPolygon .current );
90+ setPolygonCreated (false );
91+ }, []);
92+
93+ const handleClear = useCallback (() => {
94+ setPolygon (initialPolygon .current );
95+ setPolygonCreated (false );
96+ setPoints ([]);
97+ }, []);
98+
99+ const handleIsDraw = useCallback (() => {
100+ if (! mapRef .current ) return ;
101+ setDrawMode ((prevMode ) => ! prevMode );
102+ }, [handleClear , isActiveDraw ]);
103+
104+ const handleCanvasEndDraw = useCallback ((locations ) => {
105+ zoomCenterPolygon (locations .centerLatLng ).then (() => {
106+ setPolygon (locations );
107+ setPolygonCreated (true );
108+ });
109+ setDrawMode ((prevMode ) => ! prevMode );
110+ }, []);
111+
112+ const zoomCenterPolygon = async (center : ILocationProps ) => {
113+ if (! mapRef .current ) return ;
114+ const camera = await mapRef .current .getCamera ();
115+ if (Platform .OS === ' ios' ) {
116+ mapRef .current .animateCamera ({
117+ center: center ,
118+ });
119+ }
120+ if (Platform .OS === ' android' ) {}
121+ };
122+
123+
124+ const hasMarkerClose = polygon .centerLatLng && (
125+ <Marker onPress = { handleRemovePolygon } coordinate = { polygon .centerLatLng } >
126+ <MarkerLocation />
127+ </Marker >
128+ );
129+
130+ const handlePolygon = useCallback (
131+ (item , index ) => <AnimatedPolygon key = { index } coordinates = { polygon .polygons } />,
132+ [polygon .polygons ]
133+ );
134+
135+
136+ return (
137+ <View style = { styles .container } >
138+ <MapView
139+ ref = { mapRef }
140+ style = { { flex: 1 }}
141+ points = { points }
142+ widthLine = { 3 }
143+ onEndDraw = { handleCanvasEndDraw }
144+ isDrawMode = { isActiveDraw }
145+ onMapReady = { handleMapReady }
146+ onStartDraw = { handleClear }
147+ createdPolygon = { modePolygon }
148+ onChangePoints = { setPoints }
149+ backgroundCanvas = { ' rgba(0, 0, 0, 0.0)' }
150+ >
151+ { isReady && modePolygon && polygon .polygons && polygon .polygons .length > 0 && (
152+ <>
153+ { hasMarkerClose }
154+ { polygon .polygons .map (handlePolygon )}
155+ </>
156+ )}
157+ </MapView >
158+
159+ <TouchableOpacity style = { styles .button } onPress = { handleIsDraw } >
160+ { isActiveDraw ? (
161+ <Text style = { styles .title } >ON</Text >
162+ ) : (
163+ <Text style = { styles .title } >OFF</Text >
164+ )}
165+ </TouchableOpacity >
166+ </View >
167+ );
168+ }
169+
170+ const styles = StyleSheet .create ({
171+ container: {
172+ flex: 1 ,
173+ },
174+ button: {
175+ top: ' 10%' ,
176+ right: ' 10%' ,
177+ position: ' absolute' ,
178+ backgroundColor: ' tomato' ,
179+ padding: 16 ,
180+ zIndex: 4 ,
181+ borderRadius: 18 ,
182+ },
183+ title: {
184+ color: ' #FFFFFF' ,
185+ fontSize: 12 ,
186+ },
187+ });
57188
58189```
59190
@@ -65,7 +196,7 @@ For more complete example open [App.tsx](https://github.com/dev-event/react-nati
65196| name | description | required | type | default |
66197| -------------------- | --------------------------------------------------------------------------------------------------- | -------- | ------------------------------------------------------------------------------------------------------------| -----------------|
67198| ` points ` | An array of x, y coordinates for the drawing of the polygon. | YES | [ ` TouchPoint[] ` ] ( https://github.com/dev-event/react-native-maps-draw/blob/features/draw/src/types/index.ts ) | [ ] |
68- | ` unitDistance ` | Distance unit | NO | [ ` TouchPoint[] ` ] ( https://github.com/dev-event/react-native-maps-draw/blob/features/draw/src/maps/types.d.ts ) | 'm' |
199+ | ` unitDistance ` | Distance unit | NO | [ ` Units ` ] ( https://github.com/dev-event/react-native-maps-draw/blob/features/draw/src/maps/types.d.ts ) | 'm' |
69200| ` colorLine ` | Drawing line color | NO | string | '#791679 ' |
70201| ` widthLine ` | Drawing line width | NO | number | 3 |
71202| ` onEndDraw ` | Callback is called after drawing is complete | NO | (item: IDrawResult) => void | |
0 commit comments