Skip to content

Commit beb0297

Browse files
authored
Merge pull request #3154 from joshuapaling/OverlayViewBounds
2 parents 03c6a2c + f22ca2c commit beb0297

File tree

3 files changed

+44
-15
lines changed

3 files changed

+44
-15
lines changed
Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { getOffsetOverride } from './dom-helper'
1+
import { getOffsetOverride, getLayoutStyles } from './dom-helper'
22

33
type fnPixelPositionOffset = (
44
offsetWidth: number,
@@ -7,23 +7,27 @@ type fnPixelPositionOffset = (
77
export function createOverlay(
88
container: HTMLElement,
99
pane: keyof google.maps.MapPanes,
10-
position: google.maps.LatLng | google.maps.LatLngLiteral,
10+
position?: google.maps.LatLng | google.maps.LatLngLiteral,
11+
bounds?: google.maps.LatLngBounds | google.maps.LatLngBoundsLiteral,
1112
getPixelPositionOffset?: fnPixelPositionOffset
1213
) {
1314
class Overlay extends google.maps.OverlayView {
1415
container: HTMLElement
1516
pane: keyof google.maps.MapPanes
16-
position: google.maps.LatLng | google.maps.LatLngLiteral
17+
position: google.maps.LatLng | google.maps.LatLngLiteral | undefined
18+
bounds: google.maps.LatLngBounds | google.maps.LatLngBoundsLiteral | undefined
1719

1820
constructor(
1921
container: HTMLElement,
2022
pane: keyof google.maps.MapPanes,
21-
position: google.maps.LatLng | google.maps.LatLngLiteral
23+
position?: google.maps.LatLng | google.maps.LatLngLiteral,
24+
bounds?: google.maps.LatLngBounds | google.maps.LatLngBoundsLiteral
2225
) {
2326
super()
2427
this.container = container
2528
this.pane = pane
2629
this.position = position
30+
this.bounds = bounds
2731
}
2832

2933
onAdd(): void {
@@ -33,22 +37,26 @@ export function createOverlay(
3337

3438
draw(): void {
3539
const projection = this.getProjection()
36-
const point = projection.fromLatLngToDivPixel(this.position)
3740
const offset = {
3841
...(this.container
3942
? getOffsetOverride(this.container, getPixelPositionOffset)
4043
: {
4144
x: 0,
4245
y: 0,
4346
}),
44-
}
45-
if (point === null) {
46-
return
47+
}
48+
49+
const layoutStyles = getLayoutStyles(
50+
projection,
51+
offset,
52+
this.bounds,
53+
this.position
54+
)
55+
56+
for (const [key, value] of Object.entries(layoutStyles)) {
57+
this.container.style[key] = value
4758
}
4859

49-
this.container.style.transform = `translate(${point.x + offset.x}px, ${
50-
point.y + offset.y
51-
}px)`
5260
}
5361

5462
onRemove(): void {
@@ -57,5 +65,5 @@ export function createOverlay(
5765
}
5866
}
5967
}
60-
return new Overlay(container, pane, position)
68+
return new Overlay(container, pane, position, bounds)
6169
}

packages/react-google-maps-api/src/components/dom/OverlayView.stories.tsx

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ export default {
2121
component: OverlayViewF,
2222
} as ComponentMeta<typeof OverlayViewF>
2323

24+
2425
const getPixelPositionOffset = (width: number, height: number) => ({
2526
x: -(width / 2),
2627
y: -(height / 2),
2728
})
2829
const Template: ComponentStory<typeof OverlayViewF> = () => {
29-
return (
30+
const newZealand = new google.maps.LatLngBounds({lat: -46.641, lng: 166.509}, {lat: -34.450, lng: 178.517})
31+
return (
3032
<GoogleMap mapContainerStyle={mapContainerStyle} zoom={3} center={center}>
3133
{locations.map((location, index) => (
3234
<OverlayViewF
@@ -47,6 +49,23 @@ const Template: ComponentStory<typeof OverlayViewF> = () => {
4749
</div>
4850
</OverlayViewF>
4951
))}
52+
53+
<OverlayViewF
54+
mapPaneName={OVERLAY_LAYER}
55+
bounds={newZealand}
56+
position={{lat: -46.641, lng: 166.509}}
57+
>
58+
<div
59+
style={{
60+
width: '100%',
61+
height: '100%',
62+
backgroundColor: 'rgba(255,255,0,0.4)',
63+
fontSize: '12px',
64+
}}
65+
>
66+
Overlay with Bounds
67+
</div>
68+
</OverlayViewF>
5069
</GoogleMap>
5170
)
5271
}

packages/react-google-maps-api/src/components/dom/OverlayView.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ export interface OverlayViewProps {
7070
children?: ReactNode | undefined
7171
// required
7272
mapPaneName: PaneNames
73-
position: google.maps.LatLng | google.maps.LatLngLiteral
73+
position?: google.maps.LatLng | google.maps.LatLngLiteral | undefined
7474
getPixelPositionOffset?:
7575
| ((offsetWidth: number, offsetHeight: number) => { x: number; y: number })
7676
| undefined
@@ -90,6 +90,7 @@ export const OVERLAY_MOUSE_TARGET: PaneNames = `overlayMouseTarget`
9090

9191
function OverlayViewFunctional({
9292
position,
93+
bounds,
9394
mapPaneName,
9495
zIndex,
9596
onLoad,
@@ -109,9 +110,10 @@ function OverlayViewFunctional({
109110
container,
110111
mapPaneName,
111112
position,
113+
bounds,
112114
getPixelPositionOffset
113115
)
114-
}, [container, mapPaneName, position])
116+
}, [container, mapPaneName, position, bounds])
115117

116118
useEffect(() => {
117119
onLoad?.(overlay)

0 commit comments

Comments
 (0)