@@ -296,45 +296,53 @@ export default class extends AbstractMapController<
296
296
element,
297
297
} : {
298
298
definition : InfoWindowWithoutPositionDefinition < google . maps . InfoWindowOptions > ;
299
- element : google . maps . marker . AdvancedMarkerElement | google . maps . Polygon | google . maps . Polyline ;
299
+ element : google . maps . marker . AdvancedMarkerElement | google . maps . Polygon | google . maps . Polyline | google . maps . Circle | google . maps . Rectangle ;
300
300
} ) : google . maps . InfoWindow {
301
- const { headerContent, content, extra, rawOptions = { } , ...otherOptions } = definition ;
301
+ const { headerContent, content, opened, autoClose, rawOptions = { } } = definition ;
302
+
303
+ let position : google . maps . LatLng | null = null ;
304
+ if ( element instanceof google . maps . Circle ) {
305
+ position = element . getCenter ( ) ;
306
+ } else if ( element instanceof google . maps . Rectangle ) {
307
+ position = element . getBounds ( ) ?. getCenter ( ) || null ;
308
+ } else if ( element instanceof google . maps . Polygon || element instanceof google . maps . Polyline ) {
309
+ // Note: We do not compute the center of Polygons or Polylines here, since shapes can be complex.
310
+ // Instead, listen to the `ux:map:polygon:before-create` or `ux:map:polyline:before-create` events to set the position manually.
311
+ // ```js
312
+ // const bounds = new google.maps.LatLngBounds();
313
+ // element.getPath().forEach((latLng) => bounds.extend(latLng));
314
+ // event.definition.infoWindow.rawOptions.position = bounds.getCenter();
315
+ // ```
316
+ }
302
317
303
- const infoWindow = new _google . maps . InfoWindow ( {
318
+ const infoWindowOptions : google . maps . InfoWindowOptions = {
304
319
headerContent : this . createTextOrElement ( headerContent ) ,
305
320
content : this . createTextOrElement ( content ) ,
306
- ... otherOptions ,
321
+ position ,
307
322
...rawOptions ,
308
- } ) ;
323
+ } ;
309
324
310
- if ( element instanceof google . maps . marker . AdvancedMarkerElement ) {
311
- element . addListener ( 'click' , ( ) => {
312
- if ( definition . autoClose ) {
313
- this . closeInfoWindowsExcept ( infoWindow ) ;
314
- }
315
- infoWindow . open ( { map : this . map , anchor : element } ) ;
316
- } ) ;
325
+ const infoWindow = new _google . maps . InfoWindow ( infoWindowOptions ) ;
317
326
318
- if ( definition . opened ) {
319
- infoWindow . open ( { map : this . map , anchor : element } ) ;
327
+ element . addListener ( 'click' , ( event : google . maps . MapMouseEvent ) => {
328
+ if ( autoClose ) {
329
+ this . closeInfoWindowsExcept ( infoWindow ) ;
320
330
}
321
- } else if ( element instanceof google . maps . Polygon ) {
322
- element . addListener ( 'click' , ( event : any ) => {
323
- if ( definition . autoClose ) {
324
- this . closeInfoWindowsExcept ( infoWindow ) ;
325
- }
331
+
332
+ // Don't override the position if it was already set (e.g. through "rawOptions")
333
+ if ( infoWindowOptions . position === null ) {
326
334
infoWindow . setPosition ( event . latLng ) ;
327
- infoWindow . open ( this . map ) ;
328
- } ) ;
329
-
330
- if ( definition . opened ) {
331
- const bounds = new google . maps . LatLngBounds ( ) ;
332
- element . getPath ( ) . forEach ( ( point : google . maps . LatLng ) => {
333
- bounds . extend ( point ) ;
334
- } ) ;
335
- infoWindow . setPosition ( bounds . getCenter ( ) ) ;
336
- infoWindow . open ( { map : this . map , anchor : element } ) ;
337
335
}
336
+
337
+ infoWindow . open ( { map : this . map , anchor : element } ) ;
338
+ } ) ;
339
+
340
+ if ( opened ) {
341
+ if ( autoClose ) {
342
+ this . closeInfoWindowsExcept ( infoWindow ) ;
343
+ }
344
+
345
+ infoWindow . open ( { map : this . map , anchor : element } ) ;
338
346
}
339
347
340
348
return infoWindow ;
0 commit comments