๐Ÿ“ฆ ionic-team / capacitor-google-maps

๐Ÿ“„ Polygons.tsx ยท 216 lines
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216import { IonButton, IonTextarea } from '@ionic/react';
import { useState } from 'react';
import { GoogleMap } from '@capacitor/google-maps';
import BaseTestingPage from '../../components/BaseTestingPage';

const PolygonMapPage: React.FC = () => {
  const [map, setMap] = useState<GoogleMap | null>();
  const [commandOutput, setCommandOutput] = useState('');
  const [ids, setIds] = useState<string[]>([]);

  const apiKey = process.env.REACT_APP_GOOGLE_MAPS_API_KEY;

  const onPolygonClick = (data: any) => {
    setCommandOutput(
      `POLYGON (${data.polygonId}) WAS CLICKED ON MAP (${
        data.mapId
      }) WITH TAG (${data.tag ?? ''})`,
    );
  };

  const createMap = async () => {
    setCommandOutput('');
    setMap(null);
    try {
      const mapRef = document.getElementById('map_polygons')!;

      const newMap = await GoogleMap.create({
        element: mapRef,
        id: 'test-map-polygons',
        apiKey: apiKey!,
        config: {
          center: {
            lat: 24.886,
            lng: -70.268,
          },
          zoom: 5,
        },
        forceCreate: true,
      });

      setMap(newMap);
      setCommandOutput('Map created');
    } catch (err: any) {
      setCommandOutput(err.message);
    }
  };

  const destroyMap = async () => {
    setCommandOutput('');
    try {
      if (map) {
        await map.destroy();
        setMap(null);
        setCommandOutput('Map destroyed');
      }
    } catch (err: any) {
      setCommandOutput(err.message);
    }
  };

  const createHollowPolygon = async () => {
    setCommandOutput('');

    try {
      if (map) {
        // hollow polygon outer shape
        const outerCoords = [
          { lat: 25.774, lng: -80.19 },
          { lat: 18.466, lng: -66.118 },
          { lat: 32.321, lng: -64.757 },
        ];

        // polygon hole shape
        const innerCoords = [
          { lat: 28.745, lng: -70.579 },
          { lat: 29.57, lng: -67.514 },
          { lat: 27.339, lng: -66.668 },
        ];

        const createdIds = await map.addPolygons([
          {
            paths: [outerCoords, innerCoords],
            strokeColor: '#FFC107',
            strokeOpacity: 0.8,
            strokeWeight: 2,
            fillColor: '#FFC107',
            fillOpacity: 0.35,
            tag: 'my_test_hollow_polygon',
            clickable: true,
          },
        ]);

        const newIds = createdIds.concat(ids);

        setIds(newIds);
      }
    } catch (err: any) {
      setCommandOutput(err.message);
    }
  };

  const createPolygon = async () => {
    setCommandOutput('');
    try {
      if (map) {
        const triangleCoords = [
          { lat: 25.774, lng: -80.19 },
          { lat: 18.466, lng: -66.118 },
          { lat: 32.321, lng: -64.757 },
          { lat: 25.774, lng: -80.19 },
        ];

        const createdIds = await map.addPolygons([
          {
            paths: triangleCoords,
            strokeColor: '#FF0000',
            strokeOpacity: 0.8,
            strokeWeight: 2,
            fillColor: '#FF0000',
            fillOpacity: 0.35,
            tag: 'my_test_polygon',
            clickable: true,
          },
        ]);

        const newIds = createdIds.concat(ids);
        setIds(newIds);

        setCommandOutput('Polygons created');
      }
    } catch (err: any) {
      setCommandOutput(err.message);
    }
  };

  const deletePolygon = async () => {
    setCommandOutput('');
    try {
      if (map) {
        await map.removePolygons(ids);
        setIds([]);

        setCommandOutput('Polygons removed');
      }
    } catch (err: any) {
      setCommandOutput(err.message);
    }
  };

  const setOnPolygonClickButton = async () => {
    map?.setOnPolygonClickListener(onPolygonClick);
    setCommandOutput('Set On Polygon Click Listeners!');
  };

  const removeOnPolygonClickButton = async () => {
    map?.setOnPolygonClickListener();
    setCommandOutput('Removed On Polygon Click Listeners!');
  };

  return (
    <BaseTestingPage pageTitle="Polygons">
      <div>
        <IonButton id="createMapButton" onClick={createMap}>
          Create Map
        </IonButton>
        <IonButton id="destroyMapButton" onClick={destroyMap}>
          Destroy Map
        </IonButton>
        <IonButton
          id="setOnPolygonClickButton"
          onClick={setOnPolygonClickButton}
        >
          Set Click Listeners
        </IonButton>
        <IonButton
          id="removeOnPolygonClickButton"
          onClick={removeOnPolygonClickButton}
        >
          Remove Click Listeners
        </IonButton>

        <IonButton id="drawPolygonButton" onClick={createPolygon}>
          Draw Polygon
        </IonButton>

        <IonButton id="drawHollowPolygonButton" onClick={createHollowPolygon}>
            Draw Hollow Polygon
        </IonButton>

        <IonButton id="deletePolygonButton" onClick={deletePolygon}>
          Delete Polygons
        </IonButton>
      </div>
      <div>
        <IonTextarea
          id="commandOutput"
          value={commandOutput}
          autoGrow={true}
        ></IonTextarea>
      </div>
      <capacitor-google-map
        id="map_polygons"
        style={{
          position: 'absolute',
          bottom: 0,
          left: 0,
          width: window.outerWidth,
          height: window.outerWidth * 2 / 3,
        }}
      ></capacitor-google-map>
    </BaseTestingPage>
  );
};

export default PolygonMapPage;