-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.android.js
122 lines (102 loc) · 3.03 KB
/
index.android.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
'use strict';
let React = require('react-native');
let {
Component,
DeviceEventEmitter,
Image,
PropTypes,
requireNativeComponent,
View,
} = React;
/* RNGMAPS COMP */
var gmaps = {
name: 'RNGMaps',
propTypes: {
center: PropTypes.object,
zoomLevel: PropTypes.number,
markers: PropTypes.array,
zoomOnMarkers: PropTypes.bool,
/* Hackedy hack hack hack */
scaleX: React.PropTypes.number,
scaleY: React.PropTypes.number,
translateX: React.PropTypes.number,
translateY: React.PropTypes.number,
rotation: React.PropTypes.number,
},
};
let MapView = requireNativeComponent('RNGMaps', gmaps);
class RNGMaps extends Component {
constructor (props) {
super(props);
this._listeners = {
mapError: null,
mapChange: null,
markerClick: null,
};
// Look up markers by id
this._markersLookup = {};
this.state = {
zoomOnMarkers: false,
markers: []
}
}
componentDidMount () {
this._listeners.mapError = DeviceEventEmitter.addListener('mapError', (e: Event) => {
console.log(`[GMAP_ERROR]: ${e.message}`);
this.props.onMapError && this.props.onMapError(e);
});
this._listeners.mapChange = DeviceEventEmitter.addListener('mapChange', (e: Event) => {
this.props.onMapChange && this.props.onMapChange(e);
});
this._listeners.markerClick = DeviceEventEmitter.addListener('markerClick', (e: Event) => {
let marker = this._markersLookup[e.id];
marker && this.props.onMarkerClick && this.props.onMarkerClick(marker);
});
this.updateMarkers(this.props.markers);
}
componentWillUnmount () {
this._listeners.mapError && this._listeners.mapError.remove();
this._listeners.mapChange && this._listeners.mapChange.remove();
this._listeners.markerClick && this._listeners.markerClick.remove();
}
zoomOnMarkers (bool) {
// HACK: Bleurgh, forcing the change on zoomOnMarkers.
this.setState({ zoomOnMarkers: null }, () => {
this.setState({ zoomOnMarkers: bool||true });
});
}
updateMarkers (markers) {
let newMarkers = [];
for (var i = 0; i < markers.length; i++) {
let marker = markers[i];
this._markersLookup[marker.id] = marker;
newMarkers.push(marker);
}
this.setState({ markers: newMarkers });
}
_diffMarkers (markersOne, markersTwo) {
if(markersOne.length!==markersTwo.length) return true;
for (let i = 0; i < markersOne.length; i++) {
for (let prop in markersOne[i].coordinates) {
if (markersOne[i].coordinates.hasOwnProperty(prop)) {
if(markersOne[i].coordinates[prop] !== markersTwo[i].coordinates[prop]) return true;
}
}
}
return false;
}
componentWillReceiveProps (nextProps) {
if(this._diffMarkers(nextProps.markers, this.state.markers)) {
this.updateMarkers(nextProps.markers);
}
}
render () {
return ( <MapView
{ ...this.props }
markers={ this.state.markers }
zoomOnMarkers={ this.state.zoomOnMarkers }
/>
);
}
}
module.exports = RNGMaps;