@@ -31,6 +31,7 @@ class PolygonCreator extends React.Component {
31
31
} ,
32
32
polygons : [ ] ,
33
33
editing : null ,
34
+ creatingHole : false ,
34
35
} ;
35
36
}
36
37
@@ -39,19 +40,49 @@ class PolygonCreator extends React.Component {
39
40
this . setState ( {
40
41
polygons : [ ...polygons , editing ] ,
41
42
editing : null ,
43
+ creatingHole : false ,
42
44
} ) ;
43
45
}
44
46
47
+ createHole ( ) {
48
+ const { editing, creatingHole } = this . state ;
49
+ if ( ! creatingHole ) {
50
+ this . setState ( {
51
+ creatingHole : true ,
52
+ editing : {
53
+ ...editing ,
54
+ holes : [
55
+ ...editing . holes ,
56
+ [ ] ,
57
+ ] ,
58
+ } ,
59
+ } ) ;
60
+ } else {
61
+ const holes = [ ...editing . holes ] ;
62
+ if ( holes [ holes . length - 1 ] . length === 0 ) {
63
+ holes . pop ( ) ;
64
+ this . setState ( {
65
+ editing : {
66
+ ...editing ,
67
+ holes,
68
+ } ,
69
+ } ) ;
70
+ }
71
+ this . setState ( { creatingHole : false } ) ;
72
+ }
73
+ }
74
+
45
75
onPress ( e ) {
46
- const { editing } = this . state ;
76
+ const { editing, creatingHole } = this . state ;
47
77
if ( ! editing ) {
48
78
this . setState ( {
49
79
editing : {
50
80
id : id ++ ,
51
81
coordinates : [ e . nativeEvent . coordinate ] ,
82
+ holes : [ ] ,
52
83
} ,
53
84
} ) ;
54
- } else {
85
+ } else if ( ! creatingHole ) {
55
86
this . setState ( {
56
87
editing : {
57
88
...editing ,
@@ -61,6 +92,22 @@ class PolygonCreator extends React.Component {
61
92
] ,
62
93
} ,
63
94
} ) ;
95
+ } else {
96
+ const holes = [ ...editing . holes ] ;
97
+ holes [ holes . length - 1 ] = [
98
+ ...holes [ holes . length - 1 ] ,
99
+ e . nativeEvent . coordinate ,
100
+ ] ;
101
+ this . setState ( {
102
+ editing : {
103
+ ...editing ,
104
+ id : id ++ , // keep incrementing id to trigger display refresh
105
+ coordinates : [
106
+ ...editing . coordinates ,
107
+ ] ,
108
+ holes,
109
+ } ,
110
+ } ) ;
64
111
}
65
112
}
66
113
@@ -88,21 +135,32 @@ class PolygonCreator extends React.Component {
88
135
< MapView . Polygon
89
136
key = { polygon . id }
90
137
coordinates = { polygon . coordinates }
138
+ holes = { polygon . holes }
91
139
strokeColor = "#F00"
92
140
fillColor = "rgba(255,0,0,0.5)"
93
141
strokeWidth = { 1 }
94
142
/>
95
143
) ) }
96
144
{ this . state . editing && (
97
145
< MapView . Polygon
146
+ key = { this . state . editing . id }
98
147
coordinates = { this . state . editing . coordinates }
148
+ holes = { this . state . editing . holes }
99
149
strokeColor = "#000"
100
150
fillColor = "rgba(255,0,0,0.5)"
101
151
strokeWidth = { 1 }
102
152
/>
103
153
) }
104
154
</ MapView >
105
155
< View style = { styles . buttonContainer } >
156
+ { this . state . editing && (
157
+ < TouchableOpacity
158
+ onPress = { ( ) => this . createHole ( ) }
159
+ style = { [ styles . bubble , styles . button ] }
160
+ >
161
+ < Text > { this . state . creatingHole ? 'Finish Hole' : 'Create Hole' } </ Text >
162
+ </ TouchableOpacity >
163
+ ) }
106
164
{ this . state . editing && (
107
165
< TouchableOpacity
108
166
onPress = { ( ) => this . finish ( ) }
0 commit comments