Skip to content

Commit 624bebe

Browse files
committed
initial code for custom popup
1 parent 5038ac4 commit 624bebe

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import React from 'react';
2+
import Map from '../../src/index';
3+
import CustomPopup from '../../src/components/CustomPopup';
4+
5+
const WithCustomPopup = (props) => {
6+
if (!props.loaded) return <div>Loading...</div>;
7+
8+
return (
9+
<Map
10+
google={props.google}
11+
className="map"
12+
style={{ height: '100%', position: 'relative', width: '100%' }}
13+
zoom={14}
14+
>
15+
<CustomPopup position={{ lat: 37.782551, lng: -122.445368 }}>
16+
<div
17+
style={{
18+
backgroundColor: 'white',
19+
padding: '20px',
20+
borderRadius: '10px',
21+
}}
22+
>
23+
Hi there. I'm a custom popup.
24+
</div>
25+
</CustomPopup>
26+
</Map>
27+
);
28+
};
29+
30+
export default WithCustomPopup;

examples/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import Polygon from './components/withPolygons';
2121
import Polyline from './components/withPolylines';
2222
import Rectangle from './components/withRectangle';
2323
import CustomEvents from './components/resizeEvent';
24+
import CustomPopup from './components/withCustomPopup';
2425

2526
const routes = [
2627
{
@@ -72,6 +73,11 @@ const routes = [
7273
path: '/onResizeEvent',
7374
name: 'Custom events',
7475
component: CustomEvents
76+
},
77+
{
78+
path: '/onCustomPopup',
79+
name: 'Custom popup',
80+
component: CustomPopup
7581
}
7682
];
7783

src/components/CustomPopup.js

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
import React, { useRef, useEffect } from 'react';
2+
3+
function createPopupClass() {
4+
function Popup({ position, content, map }) {
5+
this.position = position;
6+
this.containerDiv = content;
7+
this.setMap(map);
8+
google.maps.OverlayView.preventMapHitsAndGesturesFrom(this.containerDiv);
9+
}
10+
11+
Popup.prototype = Object.create(google.maps.OverlayView.prototype);
12+
13+
Popup.prototype.onAdd = function () {
14+
this.getPanes().floatPane.appendChild(this.containerDiv);
15+
};
16+
17+
Popup.prototype.onRemove = function () {
18+
if (this.containerDiv.parentElement) {
19+
this.containerDiv.parentElement.removeChild(this.containerDiv);
20+
}
21+
};
22+
23+
Popup.prototype.draw = function () {
24+
var divPosition = this.getProjection().fromLatLngToDivPixel(this.position);
25+
var display =
26+
Math.abs(divPosition.x) < 4000 && Math.abs(divPosition.y) < 4000
27+
? 'block'
28+
: 'none';
29+
30+
if (display === 'block') {
31+
this.containerDiv.style.left = divPosition.x + 'px';
32+
this.containerDiv.style.top = divPosition.y + 'px';
33+
}
34+
if (this.containerDiv.style.display !== display) {
35+
this.containerDiv.style.display = display;
36+
}
37+
};
38+
39+
return Popup;
40+
}
41+
42+
const CustomPopup = ({ map, position, children }) => {
43+
const containerEl = useRef(null);
44+
useEffect(() => {
45+
const pos = new google.maps.LatLng(position.lat, position.lng);
46+
const Popup = createPopupClass();
47+
const popup = new Popup({
48+
position: pos,
49+
content: containerEl.current,
50+
map,
51+
});
52+
});
53+
return (
54+
<div
55+
style={{ position: 'absolute' }}
56+
className="custom-popup"
57+
ref={containerEl}
58+
>
59+
{children}
60+
</div>
61+
);
62+
};
63+
64+
export default CustomPopup;

0 commit comments

Comments
 (0)