Skip to content

Commit 7dbd527

Browse files
committed
feat: Use functional API of @googlemaps/js-api-loader
1 parent 65d1aa7 commit 7dbd527

File tree

3 files changed

+90
-37
lines changed

3 files changed

+90
-37
lines changed

plugin/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@
9595
"access": "public"
9696
},
9797
"dependencies": {
98-
"@googlemaps/js-api-loader": "~1.16.8",
98+
"@googlemaps/js-api-loader": "~2.0.2",
9999
"@googlemaps/markerclusterer": "~2.5.3",
100100
"@types/google.maps": "~3.58.1"
101101
}

plugin/src/web.ts

Lines changed: 82 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import { WebPlugin } from '@capacitor/core';
2-
import type { Cluster, onClusterClickHandler } from '@googlemaps/markerclusterer';
3-
import { MarkerClusterer, SuperClusterAlgorithm } from '@googlemaps/markerclusterer';
2+
import type {
3+
Cluster,
4+
onClusterClickHandler,
5+
} from '@googlemaps/markerclusterer';
6+
import {
7+
MarkerClusterer,
8+
SuperClusterAlgorithm,
9+
} from '@googlemaps/markerclusterer';
410

511
import type { Marker, TileOverlay } from './definitions';
612
import { MapType, LatLngBounds } from './definitions';
@@ -30,11 +36,18 @@ import type {
3036
RemovePolylinesArgs,
3137
RemoveTileOverlayArgs,
3238
} from './implementation';
39+
import { importLibrary, setOptions } from '@googlemaps/js-api-loader';
3340

34-
export class CapacitorGoogleMapsWeb extends WebPlugin implements CapacitorGoogleMapsPlugin {
41+
export class CapacitorGoogleMapsWeb
42+
extends WebPlugin
43+
implements CapacitorGoogleMapsPlugin
44+
{
3545
private gMapsRef: google.maps.MapsLibrary | undefined = undefined;
36-
private AdvancedMarkerElement: typeof google.maps.marker.AdvancedMarkerElement | undefined = undefined;
37-
private PinElement: typeof google.maps.marker.PinElement | undefined = undefined;
46+
private AdvancedMarkerElement:
47+
| typeof google.maps.marker.AdvancedMarkerElement
48+
| undefined = undefined;
49+
private PinElement: typeof google.maps.marker.PinElement | undefined =
50+
undefined;
3851
private maps: {
3952
[id: string]: {
4053
element: HTMLElement;
@@ -109,7 +122,10 @@ export class CapacitorGoogleMapsWeb extends WebPlugin implements CapacitorGoogle
109122
return '';
110123
}
111124

112-
private getIdFromMarker(mapId: string, marker: google.maps.marker.AdvancedMarkerElement): string {
125+
private getIdFromMarker(
126+
mapId: string,
127+
marker: google.maps.marker.AdvancedMarkerElement,
128+
): string {
113129
for (const id in this.maps[mapId].markers) {
114130
if (this.maps[mapId].markers[id] == marker) {
115131
return id;
@@ -119,21 +135,24 @@ export class CapacitorGoogleMapsWeb extends WebPlugin implements CapacitorGoogle
119135
return '';
120136
}
121137

122-
private async importGoogleLib(apiKey: string, region?: string, language?: string) {
138+
private async importGoogleLib(
139+
apiKey: string,
140+
region?: string,
141+
language?: string,
142+
) {
123143
if (this.gMapsRef === undefined) {
124-
const lib = await import('@googlemaps/js-api-loader');
125-
const loader = new lib.Loader({
126-
apiKey: apiKey ?? '',
127-
version: 'weekly',
144+
setOptions({
145+
key: apiKey ?? '',
146+
v: 'weekly',
128147
language,
129148
region,
130149
});
131-
this.gMapsRef = await loader.importLibrary('maps');
150+
this.gMapsRef = await importLibrary('maps');
132151

133152
// Import marker library once
134-
const { AdvancedMarkerElement, PinElement } = (await google.maps.importLibrary(
153+
const { AdvancedMarkerElement, PinElement } = await importLibrary(
135154
'marker',
136-
)) as google.maps.MarkerLibrary;
155+
);
137156
this.AdvancedMarkerElement = AdvancedMarkerElement;
138157
this.PinElement = PinElement;
139158

@@ -183,7 +202,8 @@ export class CapacitorGoogleMapsWeb extends WebPlugin implements CapacitorGoogle
183202
}
184203

185204
async enableTrafficLayer(_args: TrafficLayerArgs): Promise<void> {
186-
const trafficLayer = this.maps[_args.id].trafficLayer ?? new google.maps.TrafficLayer();
205+
const trafficLayer =
206+
this.maps[_args.id].trafficLayer ?? new google.maps.TrafficLayer();
187207

188208
if (_args.enabled) {
189209
trafficLayer.setMap(this.maps[_args.id].map);
@@ -273,7 +293,10 @@ export class CapacitorGoogleMapsWeb extends WebPlugin implements CapacitorGoogle
273293

274294
const customMapOverlay = new google.maps.ImageMapType({
275295
getTileUrl: function (coord, zoom) {
276-
return tileOverlay.url.replace('{x}', `${coord.x}`).replace('{y}', `${coord.y}`).replace('{z}', `${zoom}`);
296+
return tileOverlay.url
297+
.replace('{x}', `${coord.x}`)
298+
.replace('{y}', `${coord.y}`)
299+
.replace('{z}', `${zoom}`);
277300
},
278301
tileSize: new google.maps.Size(256, 256),
279302
opacity: tileOverlay.opacity,
@@ -295,7 +318,10 @@ export class CapacitorGoogleMapsWeb extends WebPlugin implements CapacitorGoogle
295318
}
296319

297320
for (let i = 0; i < map.overlayMapTypes.getLength(); i++) {
298-
if (map.overlayMapTypes.getAt(i) === this.maps[_args.id].tileOverlays[_args.tileOverlayId]) {
321+
if (
322+
map.overlayMapTypes.getAt(i) ===
323+
this.maps[_args.id].tileOverlays[_args.tileOverlayId]
324+
) {
299325
map.overlayMapTypes.removeAt(i);
300326
delete this.maps[_args.id].tileOverlays[_args.tileOverlayId];
301327
break;
@@ -323,7 +349,10 @@ export class CapacitorGoogleMapsWeb extends WebPlugin implements CapacitorGoogle
323349
}
324350

325351
async addMarker(_args: AddMarkerArgs): Promise<{ id: string }> {
326-
const advancedMarker = this.buildMarkerOpts(_args.marker, this.maps[_args.id].map);
352+
const advancedMarker = this.buildMarkerOpts(
353+
_args.marker,
354+
this.maps[_args.id].map,
355+
);
327356

328357
const id = '' + this.currMarkerId;
329358

@@ -513,13 +542,17 @@ export class CapacitorGoogleMapsWeb extends WebPlugin implements CapacitorGoogle
513542
delete this.maps[_args.id];
514543
}
515544

516-
async mapBoundsContains(_args: MapBoundsContainsArgs): Promise<{ contains: boolean }> {
545+
async mapBoundsContains(
546+
_args: MapBoundsContainsArgs,
547+
): Promise<{ contains: boolean }> {
517548
const bounds = this.getLatLngBounds(_args.bounds);
518549
const point = new google.maps.LatLng(_args.point.lat, _args.point.lng);
519550
return { contains: bounds.contains(point) };
520551
}
521552

522-
async mapBoundsExtend(_args: MapBoundsExtendArgs): Promise<{ bounds: LatLngBounds }> {
553+
async mapBoundsExtend(
554+
_args: MapBoundsExtendArgs,
555+
): Promise<{ bounds: LatLngBounds }> {
523556
const bounds = this.getLatLngBounds(_args.bounds);
524557
const point = new google.maps.LatLng(_args.point.lat, _args.point.lng);
525558
bounds.extend(point);
@@ -547,7 +580,11 @@ export class CapacitorGoogleMapsWeb extends WebPlugin implements CapacitorGoogle
547580
);
548581
}
549582

550-
async setCircleListeners(mapId: string, circleId: string, circle: google.maps.Circle): Promise<void> {
583+
async setCircleListeners(
584+
mapId: string,
585+
circleId: string,
586+
circle: google.maps.Circle,
587+
): Promise<void> {
551588
circle.addListener('click', () => {
552589
this.notifyListeners('onCircleClick', {
553590
mapId: mapId,
@@ -557,7 +594,11 @@ export class CapacitorGoogleMapsWeb extends WebPlugin implements CapacitorGoogle
557594
});
558595
}
559596

560-
async setPolygonListeners(mapId: string, polygonId: string, polygon: google.maps.Polygon): Promise<void> {
597+
async setPolygonListeners(
598+
mapId: string,
599+
polygonId: string,
600+
polygon: google.maps.Polygon,
601+
): Promise<void> {
561602
polygon.addListener('click', () => {
562603
this.notifyListeners('onPolygonClick', {
563604
mapId: mapId,
@@ -567,7 +608,11 @@ export class CapacitorGoogleMapsWeb extends WebPlugin implements CapacitorGoogle
567608
});
568609
}
569610

570-
async setPolylineListeners(mapId: string, polylineId: string, polyline: google.maps.Polyline): Promise<void> {
611+
async setPolylineListeners(
612+
mapId: string,
613+
polylineId: string,
614+
polyline: google.maps.Polyline,
615+
): Promise<void> {
571616
polyline.addListener('click', () => {
572617
this.notifyListeners('onPolylineClick', {
573618
mapId: mapId,
@@ -669,20 +714,26 @@ export class CapacitorGoogleMapsWeb extends WebPlugin implements CapacitorGoogle
669714
});
670715
});
671716

672-
map.addListener('click', (e: google.maps.MapMouseEvent | google.maps.IconMouseEvent) => {
673-
this.notifyListeners('onMapClick', {
674-
mapId: mapId,
675-
latitude: e.latLng?.lat(),
676-
longitude: e.latLng?.lng(),
677-
});
678-
});
717+
map.addListener(
718+
'click',
719+
(e: google.maps.MapMouseEvent | google.maps.IconMouseEvent) => {
720+
this.notifyListeners('onMapClick', {
721+
mapId: mapId,
722+
latitude: e.latLng?.lat(),
723+
longitude: e.latLng?.lng(),
724+
});
725+
},
726+
);
679727

680728
this.notifyListeners('onMapReady', {
681729
mapId: mapId,
682730
});
683731
}
684732

685-
private buildMarkerOpts(marker: Marker, map: google.maps.Map): google.maps.marker.AdvancedMarkerElement {
733+
private buildMarkerOpts(
734+
marker: Marker,
735+
map: google.maps.Map,
736+
): google.maps.marker.AdvancedMarkerElement {
686737
if (!this.AdvancedMarkerElement || !this.PinElement) {
687738
throw new Error('Marker library not loaded');
688739
}

pnpm-lock.yaml

Lines changed: 7 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)