Skip to content

Commit b0982b4

Browse files
authored
fix(google-maps): polygon holes (#526)
1 parent a55b917 commit b0982b4

5 files changed

Lines changed: 66 additions & 45 deletions

File tree

packages/google-maps/index.android.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,26 +1356,36 @@ export class Polygon extends OverLayBase implements IPolygon {
13561356
}
13571357
}
13581358

1359-
get holes(): Coordinate[] {
1360-
const array: androidNative.Array<com.google.android.gms.maps.model.LatLng> = this.native.getHoles().toArray();
1361-
const holes: Coordinate[] = [];
1359+
get holes(): Coordinate[][] {
1360+
const array: androidNative.Array<java.util.List<com.google.android.gms.maps.model.LatLng>> = this.native.getHoles().toArray();
1361+
const holes: Coordinate[][] = [];
13621362
for (let i = 0; i < array.length; i++) {
1363-
const hole = array[i];
1364-
holes.push({
1365-
lat: hole.latitude,
1366-
lng: hole.longitude,
1367-
});
1363+
const nativeHole = array[i].toArray();
1364+
const hole: Coordinate[] = [];
1365+
for (let j = 0; j < nativeHole.length; j++) {
1366+
hole.push({
1367+
lat: nativeHole[j].latitude,
1368+
lng: nativeHole[j].longitude,
1369+
});
1370+
}
1371+
holes.push(hole);
13681372
}
13691373
return holes;
13701374
}
13711375

1372-
set holes(value) {
1376+
set holes(value: Coordinate[][]) {
13731377
if (Array.isArray(value)) {
1374-
const nativeArray = new java.util.ArrayList<com.google.android.gms.maps.model.LatLng>();
1378+
const nativeHoles = new java.util.ArrayList<java.util.ArrayList<com.google.android.gms.maps.model.LatLng>>();
13751379
value.forEach((hole) => {
1376-
nativeArray.add(new com.google.android.gms.maps.model.LatLng(hole.lat, hole.lng));
1380+
if (Array.isArray(hole) && hole.length) {
1381+
const nativeHole = new java.util.ArrayList<com.google.android.gms.maps.model.LatLng>();
1382+
hole.forEach((coordinate) => {
1383+
nativeHole.add(new com.google.android.gms.maps.model.LatLng(coordinate.lat, coordinate.lng));
1384+
});
1385+
nativeHoles.add(nativeHole);
1386+
}
13771387
});
1378-
this.native.setHoles(nativeArray);
1388+
this.native.setHoles(nativeHoles);
13791389
}
13801390
}
13811391

packages/google-maps/index.d.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -494,7 +494,7 @@ export class Circle implements ICircle {
494494

495495
export interface IPolygon {
496496
points: Coordinate[];
497-
holes: Coordinate[];
497+
holes: Coordinate[][];
498498
tappable: boolean;
499499
strokeWidth: number;
500500
strokeColor: Color | string;
@@ -512,7 +512,7 @@ export interface PolygonOptions extends Partial<IPolygon> {}
512512
export class Polygon implements IPolygon {
513513
fillColor: Color | string;
514514
geodesic: boolean;
515-
holes: Coordinate[];
515+
holes: Coordinate[][];
516516
points: Coordinate[];
517517
strokeColor: Color | string;
518518
strokeJointType: JointType;

packages/google-maps/index.ios.ts

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,31 +1336,39 @@ export class Polygon extends OverLayBase implements IPolygon {
13361336
this.native.path = points;
13371337
}
13381338

1339-
get holes(): Coordinate[] {
1339+
get holes(): Coordinate[][] {
13401340
const nativeHoles = this.native?.holes;
13411341
const count = nativeHoles?.count || 0;
1342-
const holes: Coordinate[] = [];
1342+
const holes: Coordinate[][] = [];
13431343
for (let i = 0; i < count; i++) {
1344-
const hole = nativeHoles.objectAtIndex(i);
1345-
const coord = hole.coordinateAtIndex(0);
1346-
holes.push({
1347-
lat: coord.latitude,
1348-
lng: coord.longitude,
1349-
});
1344+
const nativeHole = nativeHoles.objectAtIndex(i);
1345+
const hole: Coordinate[] = [];
1346+
for (let j = 0; j < nativeHole.count(); j++) {
1347+
const coord = nativeHole.coordinateAtIndex(j);
1348+
hole.push({
1349+
lat: coord.latitude,
1350+
lng: coord.longitude,
1351+
});
1352+
}
1353+
holes.push(hole);
13501354
}
13511355
return holes;
13521356
}
13531357

1354-
set holes(value) {
1355-
const holes = [];
1358+
set holes(value: Coordinate[][]) {
1359+
const nativeHoles = [];
13561360
if (Array.isArray(value)) {
13571361
value.forEach((hole) => {
1358-
const path = GMSMutablePath.path();
1359-
path.addCoordinate(CLLocationCoordinate2DMake(hole.lat, hole.lng));
1360-
holes.push(path);
1362+
if (Array.isArray(hole) && hole.length) {
1363+
const path = GMSMutablePath.path();
1364+
hole.forEach((coordinate) => {
1365+
path.addCoordinate(CLLocationCoordinate2DMake(coordinate.lat, coordinate.lng));
1366+
});
1367+
nativeHoles.push(path);
1368+
}
13611369
});
13621370
}
1363-
this.native.holes = holes as any;
1371+
this.native.holes = nativeHoles as any;
13641372
}
13651373

13661374
get tappable(): boolean {

packages/google-maps/utils/index.android.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ export function intoNativeMarkerOptions(options: MarkerOptions) {
7676
opts.icon(com.google.android.gms.maps.model.BitmapDescriptorFactory.defaultMarker(hueFromColor(color)));
7777
}
7878

79-
if(typeof options?.opacity === 'number') {
79+
if (typeof options?.opacity === 'number') {
8080
opts.alpha(options.opacity);
8181
}
8282

@@ -153,14 +153,15 @@ export function intoNativePolygonOptions(options: PolygonOptions) {
153153
}
154154

155155
if (Array.isArray(options?.holes)) {
156-
const holes = new java.util.ArrayList();
157156
options.holes.forEach((hole) => {
158-
holes.add(new com.google.android.gms.maps.model.LatLng(hole.lat, hole.lng));
157+
if (Array.isArray(hole) && hole.length) {
158+
const nativeHole = new java.util.ArrayList<com.google.android.gms.maps.model.LatLng>();
159+
hole.forEach((coordinate) => {
160+
nativeHole.add(new com.google.android.gms.maps.model.LatLng(coordinate.lat, coordinate.lng));
161+
});
162+
opts.addHole(nativeHole);
163+
}
159164
});
160-
161-
if (options.holes.length) {
162-
opts.addHole(holes);
163-
}
164165
}
165166

166167
if (typeof options?.tappable === 'boolean') {
@@ -275,10 +276,7 @@ export function intoNativeGroundOverlayOptions(options: GroundOverlayOptions) {
275276
}
276277

277278
if (options?.bounds) {
278-
opts.positionFromBounds(new com.google.android.gms.maps.model.LatLngBounds(
279-
new com.google.android.gms.maps.model.LatLng(options.bounds.southwest.lat, options.bounds.southwest.lng),
280-
new com.google.android.gms.maps.model.LatLng(options.bounds.northeast.lat, options.bounds.northeast.lng)
281-
));
279+
opts.positionFromBounds(new com.google.android.gms.maps.model.LatLngBounds(new com.google.android.gms.maps.model.LatLng(options.bounds.southwest.lat, options.bounds.southwest.lng), new com.google.android.gms.maps.model.LatLng(options.bounds.northeast.lat, options.bounds.northeast.lng)));
282280
}
283281

284282
if (typeof options?.transparency) {

packages/google-maps/utils/index.ios.ts

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -144,12 +144,17 @@ export function intoNativePolygonOptions(options: PolygonOptions) {
144144
const opts = path ? GMSPolygon.polygonWithPath(path) : GMSPolygon.new();
145145

146146
if (Array.isArray(options?.holes)) {
147-
if (options.holes.length) {
148-
opts.holes = options.holes.map((hole) => {
149-
const res = GMSMutablePath.path();
150-
res.addCoordinate(CLLocationCoordinate2DMake(hole.lat, hole.lng));
151-
}) as any;
152-
}
147+
const nativeHoles = NSMutableArray.new<GMSMutablePath>();
148+
options.holes.forEach((hole) => {
149+
if (Array.isArray(hole) && hole.length) {
150+
const path = GMSMutablePath.path();
151+
hole.forEach((coordinate) => {
152+
path.addCoordinate(CLLocationCoordinate2DMake(coordinate.lat, coordinate.lng));
153+
});
154+
nativeHoles.addObject(path);
155+
}
156+
});
157+
opts.holes = nativeHoles;
153158
}
154159

155160
if (typeof options?.tappable === 'boolean') {

0 commit comments

Comments
 (0)