Skip to content

Commit

Permalink
feat(SebmGoogleMap): triggering resize events
Browse files Browse the repository at this point in the history
SebmGoogleMap now supports triggering resize events
on the google map instance. This is usefull when you intially
hide the map (for example in a tab) and then want to show
the map:

```
<div [hidden]="!showMap">
	<sebm-google-map #myMap></sebm-google-map>
</div>
<button (click)="showMap = true; myMap.triggerResize()">resize</button>
```

Closes #166

Closes #188
  • Loading branch information
sebholstein authored and a2gm-bot committed Mar 5, 2016
1 parent f2d9563 commit b27ae46
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/directives/google-map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,20 @@ export class SebmGoogleMap implements OnChanges,
}
}

/**
* Triggers a resize event on the google map instance.
* Returns a promise that gets resolved after the event was triggered.
*/
triggerResize(): Promise<void> {
// Note: When we would trigger the resize event and show the map in the same turn (which is a
// common case for triggering a resize event), then the resize event would not
// work (to show the map), so we trigger the event in a timeout.
return new Promise<void>((resolve) => {
setTimeout(
() => { return this._mapsWrapper.triggerMapEvent('resize').then(() => resolve()); });
});
}

/**
* Sets the zoom level of the map. The default value is `8`.
*/
Expand Down
7 changes: 7 additions & 0 deletions src/services/google-maps-api-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,11 @@ export class GoogleMapsAPIWrapper {
}

getMap(): Promise<mapTypes.GoogleMap> { return this._map; }

/**
* Triggers the given event name on the map instance.
*/
triggerMapEvent(eventName: string): Promise<void> {
return this._map.then((m) => google.maps.event.trigger(m, eventName));
}
}

0 comments on commit b27ae46

Please sign in to comment.