1- import * as React from 'react' ;
1+ import React , { useState , useCallback , useRef } from 'react' ;
2+ import { StyleSheet , View , TouchableOpacity , Text } from 'react-native' ;
3+ import MapView , { ILocationProps } from '../../src' ;
4+ import { MarkerLocation } from './assets' ;
5+ import { Marker , Overlay , Polygon } from 'react-native-maps' ;
26
3- import { StyleSheet , View , Text } from 'react-native' ;
4- import Map from 'react-native-maps-draw' ;
57
68export default function App ( ) {
9+ const mapRef = useRef < MapView > ( null ) ;
10+
11+ const initialPolygon = useRef ( {
12+ polygons : [ ] ,
13+ lastLatLng : undefined ,
14+ initialLatLng : undefined ,
15+ centerLatLng : undefined ,
16+ } ) ;
17+
18+ const [ isActiveDraw , setDrawMode ] = useState < boolean > ( false ) ;
19+ const [ isReady , setIsReady ] = useState < boolean > ( false ) ;
20+ const [ polygon , setPolygon ] = useState < {
21+ polygons : ILocationProps [ ] ;
22+ lastLatLng : ILocationProps ;
23+ initialLatLng : ILocationProps ;
24+ centerLatLng : ILocationProps ;
25+ } > ( initialPolygon . current ) ;
26+
27+ const handleMapReady = useCallback (
28+ ( ) => mapRef . current && setIsReady ( true ) ,
29+ [ ]
30+ ) ;
31+
32+ const handleIsDraw = useCallback ( ( ) => {
33+ if ( ! mapRef . current ) return ;
34+ setDrawMode ( ( prevMode ) => ! prevMode ) ;
35+ } , [ mapRef ] ) ;
36+
37+ const handleCanvasDraw = useCallback (
38+ ( locations ) => {
39+ setPolygon ( locations ) ;
40+ handleIsDraw ( ) ;
41+ } ,
42+ [ handleIsDraw ]
43+ ) ;
44+ const handleRemovePolygon = useCallback (
45+ ( ) => setPolygon ( initialPolygon . current ) ,
46+ [ ]
47+ ) ;
48+ // const getCameraMap = () => Promise.resolve(mapRef.current.getCamera());
49+
50+ // const zoomCenterPolygon = (center: ILocationProps): boolean => {
51+ // if (!mapRef.current) return false;
52+ // getCameraMap().then((camera) => {
53+ // if (Platform.OS === 'ios') {
54+ // mapRef.current.animateCamera({
55+ // center: center,
56+ // altitude: camera.altitude / 2,
57+ // });
58+ // }
59+ // if (Platform.OS === 'android') {
60+ // mapRef.current.animateCamera({ center, zoom: camera.zoom + 1 });
61+ // }
62+ // });
63+ // return true;
64+ // };
65+
66+ // const zoomOut = async () => {
67+ // if (!ref.current) {
68+ // return;
69+ // }
70+ // const camera = await ref.current.getCamera();
71+ // if (Platform.OS === 'ios') {
72+ // ref.current.animateCamera({altitude: camera.altitude * 2});
73+ // } else {
74+ // ref.current.animateCamera({zoom: camera.zoom - 1});
75+ // }
76+ // };
77+
78+ const hasMarkerClose = polygon . centerLatLng && (
79+ < Marker onPress = { handleRemovePolygon } coordinate = { polygon . centerLatLng } >
80+ < MarkerLocation />
81+ </ Marker >
82+ ) ;
83+
84+ const handlePolygon = useCallback (
85+ ( item , index ) => (
86+ < Polygon
87+ key = { index }
88+ coordinates = { polygon . polygons }
89+ fillColor = "rgba(0, 0, 0, 0.00)"
90+ strokeColor = "rgba(0, 0, 0, 0.00)"
91+ strokeWidth = { 1 }
92+ />
93+ ) ,
94+ [ polygon . polygons ]
95+ ) ;
96+
797 return (
898 < View style = { styles . container } >
9- < Map style = { { flex : 1 } } />
10- < Text > 3</ Text >
99+ < MapView
100+ ref = { mapRef }
101+ style = { { flex : 1 } }
102+ onStartDraw = { handleRemovePolygon }
103+ onEndDraw = { handleCanvasDraw }
104+ isDrawMode = { isActiveDraw }
105+ onMapReady = { handleMapReady }
106+ colorWidthOverlayLine = { 'red' }
107+ renderContentGesture = { ( ) => < Text > test</ Text > }
108+ >
109+ { isReady && polygon . polygons && polygon . polygons . length > 0 && (
110+ < >
111+ { hasMarkerClose }
112+ { polygon . polygons . map ( handlePolygon ) }
113+ </ >
114+ ) }
115+ </ MapView >
116+
117+ { ! isActiveDraw ? (
118+ < TouchableOpacity style = { styles . button } onPress = { handleIsDraw } >
119+ < Text > { 'start' } </ Text >
120+ </ TouchableOpacity >
121+ ) : null }
11122 </ View >
12123 ) ;
13124}
@@ -16,9 +127,13 @@ const styles = StyleSheet.create({
16127 container : {
17128 flex : 1 ,
18129 } ,
19- box : {
20- width : 60 ,
21- height : 60 ,
22- marginVertical : 20 ,
130+ button : {
131+ bottom : '10%' ,
132+ right : '10%' ,
133+ position : 'absolute' ,
134+ padding : 16 ,
135+ zIndex : 4 ,
136+ backgroundColor : 'yellow' ,
137+ borderRadius : 24
23138 } ,
24139} ) ;
0 commit comments