Files
Dan Tamas dbf746d66c Unify eslint with react-community standard (#2862)
* move eslitnt rules to @react-native-community/eslint-config

* gitignore VSCode workspace settings

* update eslint related packages and remove airbnb ones

* add .prettierrc config

* automated fix by eslint and prettier

* remove custom eslint rules

* remove never reported elint exceptions

* remove unlimited eslint-disable exceptions

* fix  formating

* fix inline styles

* fix react to 16.8.3 and remove babel-preset-react-native
2019-05-15 15:08:19 +02:00

228 lines
5.6 KiB
JavaScript

import React from 'react';
import {
StyleSheet,
View,
Text,
Dimensions,
TouchableOpacity,
Alert,
} from 'react-native';
import MapView, {
Marker,
Callout,
CalloutSubview,
ProviderPropType,
} from 'react-native-maps';
import CustomCallout from './CustomCallout';
const { width, height } = Dimensions.get('window');
const ASPECT_RATIO = width / height;
const LATITUDE = 37.78825;
const LONGITUDE = -122.4324;
const LATITUDE_DELTA = 0.0922;
const LONGITUDE_DELTA = LATITUDE_DELTA * ASPECT_RATIO;
const SPACE = 0.01;
class Callouts extends React.Component {
constructor(props) {
super(props);
this.state = {
cnt: 0,
region: {
latitude: LATITUDE,
longitude: LONGITUDE,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA,
},
markers: [
{
coordinate: {
latitude: LATITUDE + SPACE,
longitude: LONGITUDE + SPACE,
},
},
{
coordinate: {
latitude: LATITUDE + SPACE,
longitude: LONGITUDE - SPACE,
},
},
{
coordinate: {
latitude: LATITUDE,
longitude: LONGITUDE,
},
},
{
coordinate: {
latitude: LATITUDE,
longitude: LONGITUDE - SPACE / 2,
},
},
],
};
}
show() {
this.marker1.showCallout();
}
hide() {
this.marker1.hideCallout();
}
render() {
const { region, markers } = this.state;
return (
<View style={styles.container}>
<MapView
provider={this.props.provider}
style={styles.map}
initialRegion={region}
zoomTapEnabled={false}
>
<Marker
ref={ref => {
this.marker1 = ref;
}}
coordinate={markers[0].coordinate}
title="This is a native view"
description="Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation"
/>
<Marker coordinate={markers[1].coordinate}>
<Callout style={styles.plainView}>
<View>
<Text>This is a plain view</Text>
</View>
</Callout>
</Marker>
<Marker
coordinate={markers[2].coordinate}
calloutOffset={{ x: -8, y: 28 }}
calloutAnchor={{ x: 0.5, y: 0.4 }}
ref={ref => {
this.marker2 = ref;
}}
>
<Callout
alphaHitTest
tooltip
onPress={e => {
if (
e.nativeEvent.action === 'marker-inside-overlay-press' ||
e.nativeEvent.action === 'callout-inside-press'
) {
return;
}
Alert.alert('callout pressed');
}}
style={styles.customView}
>
<CustomCallout>
<Text>{`This is a custom callout bubble view ${
this.state.cnt
}`}</Text>
<CalloutSubview
onPress={() => {
this.setState({ cnt: this.state.cnt + 1 }, () => {
this.marker2.redrawCallout();
});
}}
style={[styles.calloutButton]}
>
<Text>Click me</Text>
</CalloutSubview>
</CustomCallout>
</Callout>
</Marker>
<Marker
ref={ref => {
this.marker4 = ref;
}}
coordinate={markers[3].coordinate}
title="You can also open this callout"
description="by pressing on transparent area of custom callout"
/>
</MapView>
<View style={styles.buttonContainer}>
<View style={styles.bubble}>
<Text>Tap on markers to see different callouts</Text>
</View>
</View>
<View style={styles.buttonContainer}>
<TouchableOpacity
onPress={() => this.show()}
style={[styles.bubble, styles.button]}
>
<Text>Show</Text>
</TouchableOpacity>
<TouchableOpacity
onPress={() => this.hide()}
style={[styles.bubble, styles.button]}
>
<Text>Hide</Text>
</TouchableOpacity>
</View>
</View>
);
}
}
Callouts.propTypes = {
provider: ProviderPropType,
};
const styles = StyleSheet.create({
customView: {
width: 140,
height: 140,
},
plainView: {
width: 60,
},
container: {
...StyleSheet.absoluteFillObject,
justifyContent: 'flex-end',
alignItems: 'center',
},
map: {
...StyleSheet.absoluteFillObject,
},
bubble: {
flex: 1,
backgroundColor: 'rgba(255,255,255,0.7)',
paddingHorizontal: 18,
paddingVertical: 12,
borderRadius: 20,
},
latlng: {
width: 200,
alignItems: 'stretch',
},
button: {
width: 80,
paddingHorizontal: 12,
alignItems: 'center',
marginHorizontal: 10,
},
buttonContainer: {
flexDirection: 'row',
marginVertical: 20,
backgroundColor: 'transparent',
},
calloutButton: {
width: 'auto',
backgroundColor: 'rgba(255,255,255,0.7)',
paddingHorizontal: 6,
paddingVertical: 6,
borderRadius: 12,
alignItems: 'center',
marginHorizontal: 10,
marginVertical: 10,
},
});
export default Callouts;