Skip to content

onPress Marker & add Callout Component #23

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Aug 1, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,4 @@ npm-debug.log
yarn-error.log

dist
/.vscode
79 changes: 65 additions & 14 deletions docs/stories/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React from 'react';
import { View, StyleSheet } from 'react-native';
import { View, StyleSheet, Text } from 'react-native';
import MapView from 'react-native-maps';

import { storiesOf } from '@storybook/react';
Expand All @@ -8,7 +8,8 @@ import { action } from '@storybook/addon-actions';
storiesOf('MapView', module)
.add('basic', () => (
<View style={styles.container}>
<MapView region={{ latitude: 48.86, longitude: 2.34 }} />
<MapView defaultZoom={15} region={{ latitude: 48.86, longitude: 2.34 }} />
<MapView defaultZoom={10} region={{ latitude: 48.86, longitude: 2.34 }} />
</View>
))
.add('onRegionChangeComplete', () => (
Expand All @@ -27,7 +28,12 @@ storiesOf('MapView', module)
.add('options', () => (
<View style={styles.container}>
<MapView
initialRegion={{ latitude: 48.86, longitude: 2.34 }}
initialRegion={{
latitude: 48.86,
longitude: 2.34,
latitudeDelta: 0.1,
longitudeDelta: 0.1,
}}
options={{
zoomControlOptions: {
position: window.google.maps.ControlPosition.RIGHT_CENTER,
Expand All @@ -40,17 +46,62 @@ storiesOf('MapView', module)
</View>
));

storiesOf('Marker', module).add('basic', () => (
<View style={styles.container}>
<MapView region={{ latitude: 48.88, longitude: 2.32 }}>
<MapView.Marker
title="BAM"
description="Shape the future of mobile with us"
coordinate={{ latitude: 48.8828463, longitude: 2.3229091 }}
/>
</MapView>
</View>
));
storiesOf('Marker', module)
.add('basic', () => (
<View style={styles.container}>
<MapView ref={map => (this.map = map)} region={{ latitude: 48.88, longitude: 2.32 }}>
<MapView.Marker
title="BAM"
description="Shape the future of mobile with us"
coordinate={{ latitude: 48.8828463, longitude: 2.3229091 }}
onPress={() => {
this.map.animateToRegion({
latitude: 48.8828463,
longitude: 2.3229091,
latitudeDelta: 0.1,
longitudeDelta: 0.1,
});
}}
/>
<MapView.Marker
title="BAM"
description="Shape the future of mobile with us"
coordinate={{ latitude: 48.8828463, longitude: 2.3 }}
onPress={() => {
console.log(this.map.getCamera());
const zoom = this.map.getCamera().zoom === 20 ? 15 : 20;
this.map.animateCamera({
zoom,
center: {
lat: 48.8828463,
lng: 2.3,
},
});
}}
/>
</MapView>
</View>
))
.add('Callout', () => (
<View style={styles.container}>
<MapView ref={map => (this.map = map)} region={{ latitude: 48.88, longitude: 2.32 }}>
<MapView.Marker
title="BAM"
ref={marker => (this.marker = marker)}
description="Shape the future of mobile with us"
coordinate={{ latitude: 48.8828463, longitude: 2.3229091 }}
onPress={() => {
this.marker1.showCallout();
}}>
<MapView.Callout onPress={action('onPress callout')}>
<View style={{ padding: 10 }}>
<Text>Paris</Text>
</View>
</MapView.Callout>
</MapView.Marker>
</MapView>
</View>
));

const styles = StyleSheet.create({
container: {
Expand Down
18 changes: 18 additions & 0 deletions src/Callout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React, { Component } from 'react';
import { TouchableOpacity } from 'react-native';
import { InfoWindow } from 'react-google-maps';

class MapViewCallout extends Component {
render() {
const { onPress, ...rest } = this.props;
return (
<TouchableOpacity onPress={onPress}>
<InfoWindow onCloseClick={this.props.hideCallout} {...rest}>
{this.props.children}
</InfoWindow>
</TouchableOpacity>
);
}
}

export default MapViewCallout;
19 changes: 17 additions & 2 deletions src/Marker.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,29 @@ import React, { Component } from 'react';
import { Marker } from 'react-google-maps';

class MapViewMarker extends Component {
state = {
isOpen: false,
};
showCallout() {
this.setState({ isOpen: true });
}
hideCallout() {
this.setState({ isOpen: false });
}
render() {
const { description, title, coordinate, ...rest } = this.props;
const { description, title, coordinate, onPress, ...rest } = this.props;

const childrenWithProps = React.Children.map(this.props.children, child => {
return React.cloneElement(child, { hideCallout: this.hideCallout.bind(this) });
});
return (
<Marker
{...rest}
title={description ? `${title}\n${description}` : title}
position={{ lat: coordinate.latitude, lng: coordinate.longitude }}
/>
onClick={onPress}>
{this.state.isOpen && childrenWithProps}
</Marker>
);
}
}
Expand Down
43 changes: 35 additions & 8 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { View, StyleSheet } from 'react-native';
import { withGoogleMap, GoogleMap } from 'react-google-maps';
import Marker from './Marker';
import Polyline from './Polyline';
import Callout from './Callout';

const GoogleMapContainer = withGoogleMap(props => (
<GoogleMap {...props} ref={props.handleMapMounted} />
Expand All @@ -18,8 +19,23 @@ class MapView extends Component {
this.props.onMapReady && this.props.onMapReady();
};

getCamera = () => {
return {
zoom: this.map.getZoom(),
center: this.map.getCenter(),
heading: this.map.getHeading(),
};
};

animateCamera(camera) {
this.setState({ zoom: camera.zoom });
this.setState({ center: camera.center });
}

animateToRegion(coordinates) {
this.setState({ center: { lat: coordinates.latitude, lng: coordinates.longitude } });
this.setState({
center: { lat: coordinates.latitude, lng: coordinates.longitude },
});
}

onDragEnd = () => {
Expand All @@ -34,36 +50,46 @@ class MapView extends Component {
};

render() {
const { region, initialRegion, onRegionChange, onPress, options } = this.props;
const { region, initialRegion, onRegionChange, onPress, options, defaultZoom } = this.props;
const { center } = this.state;
const style = this.props.style || styles.container;

const centerProps = region
const googleMapProps = center
? { center }
: region
? {
center: {
lat: region.latitude,
lng: region.longitude,
},
}
: center
? { center }
: {
defaultCenter: {
lat: initialRegion.latitude,
lng: initialRegion.longitude,
},
};

const zoom =
defaultZoom ||
(region && region.latitudeDelta
? Math.round(Math.log(360 / region.latitudeDelta) / Math.LN2)
: initialRegion && initialRegion.latitudeDelta
? Math.round(Math.log(360 / initialRegion.latitudeDelta) / Math.LN2)
: 15);
googleMapProps['zoom'] = this.state.zoom ? this.state.zoom : zoom;
return (
<View style={style}>
<GoogleMapContainer
handleMapMounted={this.handleMapMounted}
containerElement={<div style={{ height: '100%' }} />}
mapElement={<div style={{ height: '100%' }} />}
{...centerProps}
onZoomChanged={() => {
this.setState({ zoom: this.map.getZoom() });
}}
{...googleMapProps}
onDragStart={onRegionChange}
onIdle={this.onDragEnd}
defaultZoom={15}
defaultZoom={zoom}
onClick={onPress}
options={options}>
{this.props.children}
Expand All @@ -75,6 +101,7 @@ class MapView extends Component {

MapView.Marker = Marker;
MapView.Polyline = Polyline;
MapView.Callout = Callout;

const styles = StyleSheet.create({
container: {
Expand Down