Skip to content

Commit dcdfefa

Browse files
urikphytechtomchentw
authored andcommitted
feat(OverlayView): add props.bounds support
* Original commit: 8248e64 * Closes #205 * Closes #206
1 parent 987ed26 commit dcdfefa

File tree

1 file changed

+44
-10
lines changed

1 file changed

+44
-10
lines changed

src/creators/OverlayViewCreator.js

Lines changed: 44 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ export const overlayViewControlledPropTypes = {
2323
getPixelPositionOffset: PropTypes.func,
2424
position: PropTypes.object,
2525
children: PropTypes.node,
26+
bounds: PropTypes.object,
2627
// NOTICE!!!!!!
2728
//
2829
// Only expose those with getters & setters in the table as controlled props.
@@ -95,17 +96,33 @@ export default class OverlayViewCreator extends Component {
9596
overlayView._positionContainerElement = function _positionContainerElement() {
9697
let left;
9798
let top;
98-
const position = this._getPixelPosition();
99-
if (position) {
100-
let { x, y } = position;
101-
const offset = this._getOffset();
102-
if (offset) {
103-
x += offset.x;
104-
y += offset.y;
99+
const offset = this._getOffset();
100+
if (this.get(`bounds`)) {
101+
const bounds = this._getPixelBounds();
102+
if (bounds) {
103+
const { sw, ne } = bounds;
104+
if (offset) {
105+
sw.x += offset.x;
106+
ne.y += offset.y;
107+
}
108+
left = sw.x + `px`;
109+
top = ne.y + `px`;
110+
this._containerElement.style.width = (ne.x - sw.x) + `px`;
111+
this._containerElement.style.height = (sw.y - ne.y) + `px`;
112+
}
113+
} else {
114+
const position = this._getPixelPosition();
115+
if (position) {
116+
let { x, y } = position;
117+
if (offset) {
118+
x += offset.x;
119+
y += offset.y;
120+
}
121+
left = x + `px`;
122+
top = y + `px`;
105123
}
106-
left = x + `px`;
107-
top = y + `px`;
108124
}
125+
109126
this._containerElement.style.left = left;
110127
this._containerElement.style.top = top;
111128
};
@@ -114,7 +131,6 @@ export default class OverlayViewCreator extends Component {
114131
const projection = this.getProjection();
115132
let position = this.get(`position`);
116133
invariant(!!position, `OverlayView requires a position/defaultPosition in your props instead of %s`, position);
117-
118134
if (projection && position) {
119135
if (!(position instanceof google.maps.LatLng)) {
120136
position = new google.maps.LatLng(position.lat, position.lng);
@@ -123,6 +139,24 @@ export default class OverlayViewCreator extends Component {
123139
}
124140
};
125141

142+
overlayView._getPixelBounds = function _getPixelBounds() {
143+
const projection = this.getProjection();
144+
let bounds = this.get(`bounds`);
145+
invariant(!!bounds, `OverlayView requires a bounds in your props instead of %s`, bounds);
146+
if (projection && bounds) {
147+
if (!(bounds instanceof google.maps.LatLngBounds)) {
148+
bounds = new google.maps.LatLngBounds(
149+
new google.maps.LatLng(bounds.ne.lat, bounds.ne.lng),
150+
new google.maps.LatLng(bounds.sw.lat, bounds.sw.lng)
151+
);
152+
}
153+
return {
154+
sw: projection.fromLatLngToDivPixel(this.bounds.getSouthWest()),
155+
ne: projection.fromLatLngToDivPixel(this.bounds.getNorthEast()),
156+
};
157+
}
158+
};
159+
126160
overlayView._getOffset = function _getOffset() {
127161
// Allows the component to control the visual position of the OverlayView
128162
// relative to the LatLng pixel position.

0 commit comments

Comments
 (0)