Skip to content

Commit 270a47b

Browse files
committed
Merge branch 'develop' into main
2 parents be9e927 + 6b4473e commit 270a47b

35 files changed

+947
-56
lines changed

.editorconfig

Lines changed: 0 additions & 15 deletions
This file was deleted.

.prettierrc.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"semi": true,
3+
"trailingComma": "all",
4+
"singleQuote": true,
5+
"printWidth": 90,
6+
"tabWidth": 4
7+
}

.yarnrc

Lines changed: 0 additions & 3 deletions
This file was deleted.

LICENSE

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2021 Aliaksei Petrusevich
3+
Copyright (c) 2020 Aliaksei Petrusevich
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

Lines changed: 238 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,238 @@
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+
[![License](http://img.shields.io/:license-mit-blue.svg?style=flat-square)](http://badges.mit-license.org)
237+
238+
- **[MIT license](http://opensource.org/licenses/mit-license.php)**

babel.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
module.exports = {
22
presets: ['module:metro-react-native-babel-preset'],
3+
plugins: ['react-native-reanimated/plugin'],
34
};

draw.gif

6.05 MB
Loading

example/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,11 @@
2323
"devDependencies": {
2424
"@babel/core": "~7.12.10",
2525
"@babel/runtime": "^7.9.6",
26+
"@types/react": "~16.9.35",
27+
"@types/react-native": "~0.63.2",
2628
"babel-plugin-module-resolver": "^4.0.0",
2729
"babel-preset-expo": "8.3.0",
28-
"expo-cli": "^4.0.13"
30+
"expo-cli": "^4.0.13",
31+
"typescript": "~4.0.0"
2932
}
3033
}

0 commit comments

Comments
 (0)