|
1 | | -# react-native-maps-draw |
| 1 | +<div align="center"> |
| 2 | + <img src="./draw.gif" height="500" title="React Native Maps Draw Polygon" alt="Accordion Animated" style="box-shadow: 0 20px 30px 3px rgba(9, 9, 16, 0.2);"> |
| 3 | +</div> |
| 4 | + |
| 5 | +<br> |
| 6 | +<br> |
| 7 | + |
| 8 | +<h1 align="center">React Native Maps Draw (Polygon)</h1> |
| 9 | +<p align="center">Interactive drawing of polygons on the map. Beta version</p> |
| 10 | +<h6 align="center">Made with ❤️ by developer for developers</h6> |
| 11 | + |
| 12 | +<br> |
| 13 | +<p align="center"> |
| 14 | +<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-maps-draw" alt="build"/> |
| 16 | +<img src="https://img.shields.io/bitbucket/pr-raw/dev-event/react-native-maps-draw" alt="build"/> |
| 17 | +<img src="http://img.shields.io/:license-mit-blue.svg?style=flat-square" alt="build"/> |
| 18 | +</p> |
| 19 | + |
| 20 | + |
| 21 | + |
| 22 | +## Thanks |
| 23 | +<p>Please, click on ⭐ button.</p> |
| 24 | + |
| 25 | +# Documentation & Examples |
| 26 | + |
| 27 | +- [Installation](#installation) |
| 28 | +- [Motivation](#motivation) |
| 29 | +- [Usage](#usage) |
| 30 | +- [Props](#props) |
| 31 | +- [Example](#example) |
| 32 | +- [What's inside](#whats-inside) |
| 33 | +- [Contributing](#contributing) |
| 34 | +- [Author](#author) |
| 35 | +- [License](#license) |
| 36 | + |
| 37 | +## Installation |
| 38 | + |
| 39 | +```bash |
| 40 | +yarn add react-native-maps-draw |
| 41 | +# or |
| 42 | +npm install react-native-maps-draw |
| 43 | +``` |
| 44 | +> Also, you need to install [react-native-gesture-handler](https://github.com/software-mansion/react-native-gesture-handler) & [react-native-svg](https://github.com/react-native-community/react-native-svg), and follow theirs installation instructions. |
| 45 | +
|
| 46 | +## 🦥 Motivation |
| 47 | +- 💚 I love [React Native](https://reactnative.dev/) |
| 48 | + |
| 49 | + |
| 50 | +Big love and gratitude to all people who are working on React Native, Expo and React Native Navigation! |
| 51 | + |
| 52 | +## Usage |
| 53 | + |
| 54 | +For more complete example open [App.tsx](https://github.com/dev-event/react-native-maps-draw/blob/main/example/src/App.tsx) |
| 55 | + |
| 56 | +```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 | +}); |
| 188 | + |
| 189 | +``` |
| 190 | + |
| 191 | + |
| 192 | + |
| 193 | + |
| 194 | +## Props |
| 195 | + |
| 196 | +| name | description | required | type | default | |
| 197 | +| -------------------- | --------------------------------------------------------------------------------------------------- | -------- | ------------------------------------------------------------------------------------------------------------| -----------------| |
| 198 | +| `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) | [] | |
| 199 | +| `unitDistance` | Distance unit | NO | [`Units`](https://github.com/dev-event/react-native-maps-draw/blob/features/draw/src/maps/types.d.ts)| 'm' | |
| 200 | +| `colorLine` | Drawing line color | NO | string | '#791679' | |
| 201 | +| `widthLine` | Drawing line width | NO | number | 3 | |
| 202 | +| `onEndDraw` | Callback is called after drawing is complete | NO | (item: IDrawResult) => void | | |
| 203 | +| `isDrawMode` | Draw mode enabled / disabled | NO | YES | boolean | false | |
| 204 | +| `renderPath` | Custom canvas for drawing | NO | (path: string) => JSX.Element | | |
| 205 | +| `onStartDraw` | Callback is called when drawing starts | NO | () => void | | |
| 206 | +| `createdPolygon` | Polygon rendering modifier | YES | boolean | true | |
| 207 | +| `onChangePoints` | Callback returns x, y touches | YES | (points: TouchPoint[]) => void | | |
| 208 | +| `fillColorCanvas` | Canvas background | NO | string | 'rgba(0,0,0,0.0)'| |
| 209 | +| `backgroundCanvas` | The background of the View element overlapping the map (zIndex: 1) | NO | string | rgba(0,0,0,0.10) | |
| 210 | + |
| 211 | +## 🎉 Example |
| 212 | + |
| 213 | +Checkout the example [here](https://github.com/dev-event/react-native-accordion). |
| 214 | + |
| 215 | + |
| 216 | +## 📖 What's inside |
| 217 | +- [React Native Maps](https://github.com/wix/react-native-navigation) - is a component system for maps that ships with platform-native code that needs to be compiled together with React Native. |
| 218 | +- [React Native Svg](https://github.com/react-native-svg/react-native-svg) - provides SVG support to React Native on iOS and Android, and a compatibility layer for the web. |
| 219 | + |
| 220 | + |
| 221 | +## ✌️ Contributing |
| 222 | + |
| 223 | +Pull requests are always welcome! Feel free to open a new GitHub issue for any changes that can be made. |
| 224 | + |
| 225 | +## Author |
| 226 | + |
| 227 | +Reach out to me at one of the following places! |
| 228 | + |
| 229 | +- E-mail < a href= "#" target= "_blank"> [email protected]</ a> |
| 230 | +- Medium at <a href="https://medium.com/@effectwaaters" target="_blank">https://medium.com/@effectwaaters </a> |
| 231 | +- Instagram at <a href="https://www.instagram.com/dev_event/" target="_blank">https://www.instagram.com/dev_event/ </a> |
| 232 | + |
| 233 | + |
| 234 | +## License |
| 235 | + |
| 236 | +[](http://badges.mit-license.org) |
| 237 | + |
| 238 | +- **[MIT license](http://opensource.org/licenses/mit-license.php)** |
0 commit comments