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
173import { IonButton, IonTextarea } from '@ionic/react';
import { useState } from 'react';
import { GoogleMap } from '@capacitor/google-maps';
import BaseTestingPage from '../../components/BaseTestingPage';
const PolylineMapPage: 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 onPolylineClick = (data: any) => {
setCommandOutput(
`POLYLINE (${data.polylineId}) WAS CLICKED ON MAP (${
data.mapId
}) WITH TAG (${data.tag ?? ''})`,
);
};
const createMap = async () => {
setCommandOutput('');
setMap(null);
try {
const mapRef = document.getElementById('map_polylines')!;
const newMap = await GoogleMap.create({
element: mapRef,
id: 'test-map-polylines',
apiKey: apiKey!,
config: {
center: {
lat: 33.6,
lng: -117.9,
},
zoom: 8,
},
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 createPolyline = async () => {
setCommandOutput('');
try {
if (map) {
const sampleLines: google.maps.LatLngLiteral[] = [
{ lat: 37.772, lng: -122.214 },
{ lat: 21.291, lng: -157.821 },
{ lat: -18.142, lng: 178.431 },
{ lat: -27.467, lng: 153.027 },
];
const createdIds = await map.addPolylines([
{
path: sampleLines,
strokeColor: '#ffdd00',
strokeOpacity: 1.0,
strokeWeight: 2,
geodesic: true,
tag: 'my_polyline',
clickable: true,
styleSpans: [
{ color: '#85892D' },
{ color: '#0000FF' },
{ color: '#FFF700' },
{ color: '#FF99CC' },
],
},
]);
setIds(createdIds);
setCommandOutput('Polyline created');
}
} catch (err: any) {
setCommandOutput(err.message);
}
};
const deletePolyline = async () => {
setCommandOutput('');
try {
if (map) {
await map.removePolylines(ids);
setIds([]);
}
} catch (err: any) {
setCommandOutput(err.message);
}
};
const setOnPolylineClickButton = async () => {
map?.setOnPolylineClickListener(onPolylineClick);
setCommandOutput('Set On Polyline Click Listeners!');
};
const removeOnPolylineClickButton = async () => {
map?.setOnPolylineClickListener();
setCommandOutput('Removed On Polyline Click Listeners!');
};
return (
<BaseTestingPage pageTitle="Polylines">
<div>
<IonButton id="createMapButton" onClick={createMap}>
Create Map
</IonButton>
<IonButton id="destroyMapButton" onClick={destroyMap}>
Destroy Map
</IonButton>
<IonButton
id="setOnPolylineClickButton"
onClick={setOnPolylineClickButton}
>
Set Click Listeners
</IonButton>
<IonButton
id="removeOnPolylineClickButton"
onClick={removeOnPolylineClickButton}
>
Remove Click Listeners
</IonButton>
<IonButton id="drawPolylineButton" onClick={createPolyline}>
Draw Polyline
</IonButton>
<IonButton id="deletePolylineButton" onClick={deletePolyline}>
Delete Polyline
</IonButton>
</div>
<div>
<IonTextarea
id="commandOutput"
value={commandOutput}
autoGrow={true}
></IonTextarea>
</div>
<capacitor-google-map
id="map_polylines"
style={{
position: 'absolute',
bottom: 0,
left: 0,
width: window.outerWidth,
height: window.outerWidth * 2 / 3,
}}
></capacitor-google-map>
</BaseTestingPage>
);
};
export default PolylineMapPage;