Skip to content

Commit b61672f

Browse files
committed
add Callout component
1 parent 78df4cc commit b61672f

File tree

4 files changed

+70
-15
lines changed

4 files changed

+70
-15
lines changed

docs/stories/index.js

Lines changed: 34 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import React from 'react';
2-
import { View, StyleSheet } from 'react-native';
2+
import { View, StyleSheet, Text } from 'react-native';
33
import MapView from 'react-native-maps';
44

55
import { storiesOf } from '@storybook/react';
@@ -40,18 +40,39 @@ storiesOf('MapView', module)
4040
</View>
4141
));
4242

43-
storiesOf('Marker', module).add('basic', () => (
44-
<View style={styles.container}>
45-
<MapView region={{ latitude: 48.88, longitude: 2.32 }}>
46-
<MapView.Marker
47-
title='BAM'
48-
description='Shape the future of mobile with us'
49-
coordinate={{ latitude: 48.8828463, longitude: 2.3229091 }}
50-
onPress={action('onPresss')}
51-
/>
52-
</MapView>
53-
</View>
54-
));
43+
storiesOf('Marker', module)
44+
.add('basic', () => (
45+
<View style={styles.container}>
46+
<MapView region={{ latitude: 48.88, longitude: 2.32 }}>
47+
<MapView.Marker
48+
title="BAM"
49+
description="Shape the future of mobile with us"
50+
coordinate={{ latitude: 48.8828463, longitude: 2.3229091 }}
51+
onPress={action('onPresss')}
52+
/>
53+
</MapView>
54+
</View>
55+
))
56+
.add('Callout', () => (
57+
<View style={styles.container}>
58+
<MapView region={{ latitude: 48.88, longitude: 2.32 }}>
59+
<MapView.Marker
60+
title="BAM"
61+
ref={marker => (this.marker = marker)}
62+
description="Shape the future of mobile with us"
63+
coordinate={{ latitude: 48.8828463, longitude: 2.3229091 }}
64+
onPress={() => {
65+
this.marker.showCallout();
66+
}}>
67+
<MapView.Callout onPress={action('onPress callout')}>
68+
<View style={{ padding: 10 }}>
69+
<Text>Paris</Text>
70+
</View>
71+
</MapView.Callout>
72+
</MapView.Marker>
73+
</MapView>
74+
</View>
75+
));
5576

5677
const styles = StyleSheet.create({
5778
container: {

src/Callout.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import React, { Component } from 'react';
2+
import { TouchableOpacity } from 'react-native';
3+
import { InfoWindow } from 'react-google-maps';
4+
5+
class MapViewCallout extends Component {
6+
render() {
7+
const { onPress, ...rest } = this.props;
8+
return (
9+
<TouchableOpacity onPress={onPress}>
10+
<InfoWindow onCloseClick={this.props.hideCallout} {...rest}>
11+
{this.props.children}
12+
</InfoWindow>
13+
</TouchableOpacity>
14+
);
15+
}
16+
}
17+
18+
export default MapViewCallout;

src/Marker.js

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,29 @@ import React, { Component } from 'react';
22
import { Marker } from 'react-google-maps';
33

44
class MapViewMarker extends Component {
5+
state = {
6+
isOpen: false,
7+
};
8+
showCallout() {
9+
this.setState({ isOpen: true });
10+
}
11+
hideCallout() {
12+
this.setState({ isOpen: false });
13+
}
514
render() {
615
const { description, title, coordinate, onPress, ...rest } = this.props;
16+
17+
const childrenWithProps = React.Children.map(this.props.children, child => {
18+
return React.cloneElement(child, { hideCallout: this.hideCallout.bind(this) });
19+
});
720
return (
821
<Marker
922
{...rest}
1023
title={description ? `${title}\n${description}` : title}
1124
position={{ lat: coordinate.latitude, lng: coordinate.longitude }}
12-
onClick={onPress}
13-
/>
25+
onClick={onPress}>
26+
{this.state.isOpen && childrenWithProps}
27+
</Marker>
1428
);
1529
}
1630
}

src/index.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { View, StyleSheet } from 'react-native';
33
import { withGoogleMap, GoogleMap } from 'react-google-maps';
44
import Marker from './Marker';
55
import Polyline from './Polyline';
6+
import Callout from './Callout';
67

78
const GoogleMapContainer = withGoogleMap(props => (
89
<GoogleMap {...props} ref={props.handleMapMounted} />
@@ -75,6 +76,7 @@ class MapView extends Component {
7576

7677
MapView.Marker = Marker;
7778
MapView.Polyline = Polyline;
79+
MapView.Callout = Callout;
7880

7981
const styles = StyleSheet.create({
8082
container: {

0 commit comments

Comments
 (0)