Skip to main content

InfoWindow

The InfoWindow object displays an InfoWindow on the map.

InfoWindow extends Layer.

Example usage

The easiest way to display an InfoWindow is to use the attachInfoWindow() method.

const map = G.map('map1', { apiKey: 'my-api-key', center: { latitude: 48.864716, longitude: 2.3522 } });
map.load().then(() => {
const marker = G.marker({
latitude: 48.9,
longitude: 2.4,
map: map,
title: 'My Marker',
});

marker.attachInfoWindow('Testing');
});

Alternately, you can display the InfoWindow when the marker is clicked.

const map = G.map('map1', { apiKey: 'my-api-key', center: { latitude: 48.864716, longitude: 2.3522 } });
map.load();
const marker = G.marker({
latitude: 48.9,
longitude: 2.4,
map: map,
title: 'My Marker',
});
const infoWindow = G.infoWindow({
content: 'This is a test',
});
marker.on('click', () => {
infoWindow.show(marker);
});

Creating the InfoWindow object

G.infoWindow(options?: InfoWindowOptions): InfoWindow

There are a few ways that you can setup the InfoWindow object.

No parameters.

G.infoWindow(): InfoWindow

const infoWindow = G.infoWindow();

Pass the options.

G.infoWindow(options: InfoWindowOptions): InfoWindow

ParameterTypeRequiredDescription
optionsInfoWindowOptionsYesThe options.
const infoWindow = G.infoWindow({content: 'Hello!'});

Pass an existing InfoWindow object.

G.infoWindow(object: LatLngBounds): InfoWindow

In this case the InfoWindow object is simply returned.

ParameterTypeRequiredDescription
objectInfoWindowYesA InfoWindow object.
const infoWindow = G.infoWindow(infoWindowObject);

Pass the InfoWindow content.

G.infoWindow(content: string|HTMLElement): InfoWindow

ParameterTypeRequiredDescription
contentstring or HTMLElementYesThe InfoWindow content.
const infoWindow = G.infoWindow('My InfoWindow content');
const content = document.createElement('div');
content.classList.add('myInfoWindowClass');
content.innerHTML = 'This is my InfoWindow';
const infoWindow = G.infoWindow(content);

InfoWindow type

The attachInfoWindow() function and other methods that accept an InfoWindow value accept InfoWindowValue as the value type.

The InfoWindowValue can be one of the following values:

  • InfoWindow object
  • InfoWindowOptions object
  • A string containing the content for the InfoWindow
  • An HTMLElement that will be the content for the InfoWindow.

InfoWindow options

Type InfoWindowOptions

OptionTypeDefaultDescription
ariaLabelstringThe aria label for the InfoWindow.
autoClosebooleantrueWhether to automatically close other open InfoWindows when opening another one.
contentstring or HTMLElement or TextThe content for the InfoWindow.
disableAutoPanbooleanWhether to disable panning the map to make the InfoWindow fully visible when it shows.
eventstring'click'The event to trigger the display of the InfoWindow. Allowed values are click, clickon, and hover. This is an alternate way of setting the trigger event than passing the event to attachTo or attachInfoWindow.
maxWidthnumberThe maximum width of the InfoWindow, regardless of content's width
minWidthnumberThe minimum width of the InfoWindow, regardless of content's width
pixelOffsetSize[0, -4]The offset, in pixels, of the tip of the info window from the point on the map at whose geographical coordinates the info window is anchored. If an InfoWindow is opened from an anchor, the pixelOffset will be calculated from the anchor's anchorPoint property.
positionLatLngThe InfoWindow position
zIndexnumberThe zIndex of the InfoWindow

Properties

  • Properties inherited from Layer.
OptionTypeDescription
ariaLabelstringThe aria label for the InfoWindow.
contentstring or HTMLElement or TextThe content for the InfoWindow.
disableAutoPanbooleanWhether to disable panning the map to make the InfoWindow fully visible when it shows.
eventstringThe event to trigger the display of the InfoWindow. Allowed values are click, clickon, and hover. This is an alternate way of setting the trigger event than passing the event to attachTo or attachInfoWindow.
maxWidthnumberThe maximum width of the InfoWindow, regardless of content's width
minWidthnumberThe minimum width of the InfoWindow, regardless of content's width
pixelOffsetSizeThe offset, in pixels, of the tip of the info window from the point on the map at whose geographical coordinates the info window is anchored. If an InfoWindow is opened from an anchor, the pixelOffset will be calculated from the anchor's anchorPoint property.
positionLatLngThe InfoWindow position
zIndexnumberThe zIndex of the InfoWindow

Methods

  • Methods inherited from Layer.
  • Methods inherited from Evented.
  • Methods inherited from Base

attachTo

attachTo(element: Map | Layer, event?: string): Promise<InfoWindow>

Attach the InfoWindow to a map or an element that extends the Layer object and show it when hovering the mouse over the element.

Elements that extend Layer include Marker, InfoWindow, and Popup.

ParameterTypeDefaultRequiredDescription
elementMap or LayerYesThe element to attach the InfoWindow to.
eventstring'hover'The event to trigger the InfoWindow.

Allowed event values include:

  • click - Toggle the display of the InfoWindow when clicking on the element.
  • clickon - Show the InfoWindow when clicking on the element. It will always be shown and can't be hidden once the element is clicked. This may be useful if you want to show a InfoWindow each time you click on the map.
  • hover - Show the InfoWindow when hovering over the element. Hide the InfoWindow when the element is no longer hovered.
const infoWindow = G.infoWindow({
className: 'my-infoWindow',
content: 'This is a info window on a marker',
});
infoWindow.attachTo(marker);
const infoWindow = G.infoWindow({
className: 'my-infoWindow',
content: 'This is a info window on a map',
});
infoWindow.attachTo(map, 'clickon');

close

close(): InfoWindow

Hides the InfoWindow. Alias to hide().

infoWindow.close();

hasContent

hasContent(): boolean

Returns whether the InfoWindow has any content set.

if (infoWindow.hasContent()) {
// Do something
}

hide

hide(): InfoWindow

Hides the InfoWindow.

infoWindow.hide();

isOpen

isOpen(): boolean

Returns whether the InfoWindow is open.

if (infoWindow.isOpen()) {
// Do something
}

open

open(element: Map | Layer): Promise<InfoWindow>

Show the InfoWindow for the element. Alias to show().

ParameterTypeRequiredDescription
elementMap or LayerYesThe element to show the InfoWindow on.
infoWindow.open(markerObj);
infoWindow.open(mapObj);

setContent

setContent(content: string | Element | Text): InfoWindow

Set the InfoWindow content.

ParameterTypeRequiredDescription
contentstring or HTMLElement or TextYesThe InfoWindow content.
infoWindow.setContent('This is the content');

setOptions

setOptions(options: InfoWindowOptions): InfoWindow

ParameterTypeRequiredDescription
optionsInfoWindowOptionsYesThe InfoWindow options.
infoWindow.setOptions({content: 'Hi!'});

setPosition

setPosition(position: LatLngValue): InfoWindow

Set the position of the InfoWindow.

ParameterTypeRequiredDescription
positionLatLngValueYesThe InfoWindow position.
infoWindow.setPosition([49.864716, 2.6522]);

setZIndex

setZIndex(zIndex: number): InfoWindow

Set the zIndex for the InfoWindow.

ParameterTypeRequiredDescription
zIndexInfoWindowOptionsYesThe InfoWindow zIndex.
infoWindow.setZIndex(3);

show

show(element: Map | Layer): Promise<InfoWindow>

Shows the InfoWindow for the element.

ParameterTypeRequiredDescription
elementMap or LayerYesThe element to show the InfoWindow on.
infoWindow.show(markerObj);
infoWindow.show(mapObj);

If a Marker is passed as the element then the marker's position is automatically used to position the InfoWindow about the marker.

marker.on('click', () => {
infoWindow.show(marker);
});

If a Map is passed as the element then you also need to call setPosition() to set the latitude/longitude position of the InfoWindow.

map.on('click', (e) => {
infoWindow.setPosition(e.latLng);
infoWindow.show(map);
});

By default if an InfoWindow is already shown then it'll be hidden automatically. If you want the InfoWindow to show on the map where you click then you'll need to hide it first.

map.on('click', (e) => {
infoWindow.setPosition(e.latLng);
infoWindow.hide();
infoWindow.show(map);
});

If you attach the InfoWindow to the Map or Marker object then the positioning and showing/hiding is handled automatically.

map.attachInfoWindow(infoWindow);
marker.attachInfoWindow(infoWindow);

toggle

toggle(map: Map): void

Toggle the display of the InfoWindow on the map.

ParameterTypeRequiredDescription
mapMapYesThe map object
infoWindow.toggle(map);

toGoogle

toGoogle(): google.maps.InfoWindow

Returns the Google Maps InfoWindow object.

const googleObject = infoWindow.toGoogle();