Skip to content

Commit 4894805

Browse files
author
Ryan O'Boril
committed
add circle functionality
1 parent e9ed46d commit 4894805

File tree

2 files changed

+124
-0
lines changed

2 files changed

+124
-0
lines changed

src/components/Circle.js

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
import React from 'react';
2+
import PropTypes from 'prop-types';
3+
4+
import { arePathsEqual } from '../lib/arePathsEqual';
5+
import { camelize } from '../lib/String';
6+
const evtNames = ['click', 'mouseout', 'mouseover'];
7+
8+
const wrappedPromise = function() {
9+
var wrappedPromise = {},
10+
promise = new Promise(function
11+
(resolve, reject) {
12+
wrappedPromise.resolve = resolve;
13+
wrappedPromise.reject = reject;
14+
});
15+
wrappedPromise.then = promise.then.bind(promise);
16+
wrappedPromise.catch = promise.catch.bind(promise);
17+
wrappedPromise.promise = promise;
18+
19+
return wrappedPromise;
20+
}
21+
22+
export class Circle extends React.Component {
23+
componentDidMount() {
24+
this.circlePromise = wrappedPromise();
25+
this.renderCircle();
26+
}
27+
28+
componentDidUpdate(prevProps) {
29+
if (
30+
this.props.map !== prevProps.map ||
31+
!arePathsEqual(this.props.path, prevProps.path)
32+
) {
33+
if (this.circle) {
34+
this.circle.setMap(null);
35+
}
36+
this.renderCircle();
37+
}
38+
}
39+
40+
componentWillUnmount() {
41+
if (this.circle) {
42+
this.circle.setMap(null);
43+
}
44+
}
45+
46+
renderCircle() {
47+
const {
48+
map,
49+
google,
50+
center,
51+
radius,
52+
strokeColor,
53+
strokeOpacity,
54+
strokeWeight,
55+
fillColor,
56+
fillOpacity,
57+
draggable,
58+
visible,
59+
...props
60+
} = this.props;
61+
62+
if (!google) {
63+
return null;
64+
}
65+
66+
const params = {
67+
map,
68+
center,
69+
radius,
70+
strokeColor,
71+
strokeOpacity,
72+
strokeWeight,
73+
fillColor,
74+
fillOpacity,
75+
draggable,
76+
visible,
77+
...props
78+
};
79+
80+
this.circle = new google.maps.Circle(params);
81+
82+
evtNames.forEach(e => {
83+
this.circle.addListener(e, this.handleEvent(e));
84+
});
85+
86+
this.circlePromise.resolve(this.circle);
87+
}
88+
89+
getCircle() {
90+
return this.circlePromise;
91+
}
92+
93+
handleEvent(evt) {
94+
return (e) => {
95+
const evtName = `on${camelize(evt)}`
96+
if (this.props[evtName]) {
97+
this.props[evtName](this.props, this.circle, e);
98+
}
99+
}
100+
}
101+
102+
render() {
103+
return null;
104+
}
105+
}
106+
107+
Circle.propTypes = {
108+
center: PropTypes.object,
109+
radius: PropTypes.number,
110+
strokeColor: PropTypes.string,
111+
strokeOpacity: PropTypes.number,
112+
strokeWeight: PropTypes.number,
113+
fillColor: PropTypes.string,
114+
fillOpacity: PropTypes.number,
115+
}
116+
117+
evtNames.forEach(e => Circle.propTypes[e] = PropTypes.func)
118+
119+
Circle.defaultProps = {
120+
name: 'Circle'
121+
}
122+
123+
export default Circle

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ export {InfoWindow} from './components/InfoWindow';
4848
export {HeatMap} from './components/HeatMap';
4949
export {Polygon} from './components/Polygon';
5050
export {Polyline} from './components/Polyline';
51+
export {Circle} from './components/Circle';
5152

5253
export class Map extends React.Component {
5354
constructor(props) {

0 commit comments

Comments
 (0)