Skip to content

Commit 667fb64

Browse files
authored
Merge pull request #405 from basantech89/basantech89-issue403
Complete Issue#403 - Add rectangle component
2 parents dabc6e8 + 9f012db commit 667fb64

File tree

5 files changed

+207
-0
lines changed

5 files changed

+207
-0
lines changed

examples/components/withRectangle.js

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import React from 'react';
2+
3+
import Map from '../../src/index';
4+
5+
import Rectangle from '../../src/components/Rectangle';
6+
7+
8+
const WithRectangles = props => {
9+
if (!props.loaded) return <div>Loading...</div>;
10+
11+
const bounds = {
12+
north: 37.789411,
13+
south: 37.731757,
14+
east: -122.410333,
15+
west: -122.489116,
16+
};
17+
18+
return (
19+
<Map
20+
google={props.google}
21+
className="map"
22+
style={{ height: '100%', position: 'relative', width: '100%' }}
23+
zoom={11}
24+
>
25+
<Rectangle
26+
fillColor="#0000FF"
27+
fillOpacity={0.35}
28+
bounds={bounds}
29+
strokeColor="#0000FF"
30+
strokeOpacity={0.8}
31+
strokeWeight={2}
32+
/>
33+
</Map>
34+
);
35+
};
36+
37+
export default WithRectangles;

examples/index.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import Autocomplete from './components/autocomplete';
1919
import HeatMap from './components/withHeatMap';
2020
import Polygon from './components/withPolygons';
2121
import Polyline from './components/withPolylines';
22+
import Rectangle from './components/withRectangle';
2223
import CustomEvents from './components/resizeEvent';
2324

2425
const routes = [
@@ -62,6 +63,11 @@ const routes = [
6263
name: 'Polyline',
6364
component: Polyline
6465
},
66+
{
67+
path: '/rectangle',
68+
name: 'Rectangle',
69+
component: Rectangle
70+
},
6571
{
6672
path: '/onResizeEvent',
6773
name: 'Custom events',

src/components/Rectangle.js

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

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export {HeatMap} from './components/HeatMap';
4949
export {Polygon} from './components/Polygon';
5050
export {Polyline} from './components/Polyline';
5151
export {Circle} from './components/Circle';
52+
export {Rectangle} from './components/Rectangle';
5253

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

src/lib/areBoundsEqual.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/**
2+
* Compares two bound objects.
3+
*/
4+
5+
export const areBoundsEqual = function(boundA, boundB) {
6+
if (boundA === boundB) {
7+
return true;
8+
}
9+
if (
10+
!(boundA instanceof Object) ||
11+
!(boundB instanceof Object)
12+
) {
13+
return false;
14+
}
15+
if (Object.keys(boundA).length !== Object.keys(boundB).length) {
16+
return false;
17+
}
18+
if (
19+
!areValidBounds(boundA) ||
20+
!areValidBounds(boundB)
21+
) {
22+
return false;
23+
}
24+
for (const key of Object.keys(boundA)) {
25+
if (boundA[key] !== boundB[key]) {
26+
return false;
27+
}
28+
}
29+
return true;
30+
};
31+
32+
/**
33+
* Helper that checks whether an array consists of objects
34+
* with lat and lng properties
35+
* @param {object} elem the element to check
36+
* @returns {boolean} whether or not it's valid
37+
*/
38+
const areValidBounds = function(elem) {
39+
return (
40+
elem !== null &&
41+
typeof elem === 'object' &&
42+
elem.hasOwnProperty('north') &&
43+
elem.hasOwnProperty('south') &&
44+
elem.hasOwnProperty('east') &&
45+
elem.hasOwnProperty('west')
46+
);
47+
};

0 commit comments

Comments
 (0)