Skip to content

Commit 22b566b

Browse files
author
Norman Torres
committed
Circle support
1 parent 71d19a5 commit 22b566b

File tree

2 files changed

+119
-0
lines changed

2 files changed

+119
-0
lines changed

src/components/Circle.js

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