mirror of
https://github.com/expo/react-native-maps.git
synced 2026-11-15 09:18:08 -10:00
* 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
137 lines
3.6 KiB
JavaScript
137 lines
3.6 KiB
JavaScript
import React, { Component } from 'react';
|
|
|
|
import { View, StyleSheet, TouchableOpacity, Text } from 'react-native';
|
|
|
|
import MapView from 'react-native-maps';
|
|
import carImage from './assets/car.png';
|
|
|
|
export default class NavigationMap extends Component {
|
|
constructor(props) {
|
|
super(props);
|
|
this.state = {
|
|
prevPos: null,
|
|
curPos: { latitude: 37.420814, longitude: -122.081949 },
|
|
curAng: 45,
|
|
latitudeDelta: 0.0922,
|
|
longitudeDelta: 0.0421,
|
|
};
|
|
this.changePosition = this.changePosition.bind(this);
|
|
this.getRotation = this.getRotation.bind(this);
|
|
this.updateMap = this.updateMap.bind(this);
|
|
}
|
|
|
|
changePosition(latOffset, lonOffset) {
|
|
const latitude = this.state.curPos.latitude + latOffset;
|
|
const longitude = this.state.curPos.longitude + lonOffset;
|
|
this.setState({
|
|
prevPos: this.state.curPos,
|
|
curPos: { latitude, longitude },
|
|
});
|
|
this.updateMap();
|
|
}
|
|
|
|
getRotation(prevPos, curPos) {
|
|
if (!prevPos) {
|
|
return 0;
|
|
}
|
|
const xDiff = curPos.latitude - prevPos.latitude;
|
|
const yDiff = curPos.longitude - prevPos.longitude;
|
|
return (Math.atan2(yDiff, xDiff) * 180.0) / Math.PI;
|
|
}
|
|
|
|
updateMap() {
|
|
const { curPos, prevPos, curAng } = this.state;
|
|
const curRot = this.getRotation(prevPos, curPos);
|
|
this.map.animateCamera({ heading: curRot, center: curPos, pitch: curAng });
|
|
}
|
|
|
|
render() {
|
|
return (
|
|
<View style={styles.flex}>
|
|
<MapView
|
|
ref={el => (this.map = el)}
|
|
style={styles.flex}
|
|
minZoomLevel={15}
|
|
initialRegion={{
|
|
...this.state.curPos,
|
|
latitudeDelta: this.state.latitudeDelta,
|
|
longitudeDelta: this.state.longitudeDelta,
|
|
}}
|
|
>
|
|
<MapView.Marker
|
|
coordinate={this.state.curPos}
|
|
anchor={{ x: 0.5, y: 0.5 }}
|
|
image={carImage}
|
|
/>
|
|
</MapView>
|
|
<View style={styles.buttonContainerUpDown}>
|
|
<TouchableOpacity
|
|
style={[styles.button, styles.up]}
|
|
onPress={() => this.changePosition(0.0001, 0)}
|
|
>
|
|
<Text>+ Lat</Text>
|
|
</TouchableOpacity>
|
|
<TouchableOpacity
|
|
style={[styles.button, styles.down]}
|
|
onPress={() => this.changePosition(-0.0001, 0)}
|
|
>
|
|
<Text>- Lat</Text>
|
|
</TouchableOpacity>
|
|
</View>
|
|
<View style={styles.buttonContainerLeftRight}>
|
|
<TouchableOpacity
|
|
style={[styles.button, styles.left]}
|
|
onPress={() => this.changePosition(0, -0.0001)}
|
|
>
|
|
<Text>- Lon</Text>
|
|
</TouchableOpacity>
|
|
<TouchableOpacity
|
|
style={[styles.button, styles.right]}
|
|
onPress={() => this.changePosition(0, 0.0001)}
|
|
>
|
|
<Text>+ Lon</Text>
|
|
</TouchableOpacity>
|
|
</View>
|
|
</View>
|
|
);
|
|
}
|
|
}
|
|
|
|
const styles = StyleSheet.create({
|
|
flex: {
|
|
flex: 1,
|
|
width: '100%',
|
|
},
|
|
buttonContainerUpDown: {
|
|
...StyleSheet.absoluteFillObject,
|
|
flexDirection: 'row',
|
|
justifyContent: 'center',
|
|
},
|
|
buttonContainerLeftRight: {
|
|
...StyleSheet.absoluteFillObject,
|
|
flexDirection: 'column',
|
|
justifyContent: 'center',
|
|
},
|
|
button: {
|
|
backgroundColor: 'rgba(100,100,100,0.2)',
|
|
position: 'absolute',
|
|
alignItems: 'center',
|
|
justifyContent: 'center',
|
|
borderRadius: 20,
|
|
height: 50,
|
|
width: 50,
|
|
},
|
|
up: {
|
|
alignSelf: 'flex-start',
|
|
},
|
|
down: {
|
|
alignSelf: 'flex-end',
|
|
},
|
|
left: {
|
|
alignSelf: 'flex-start',
|
|
},
|
|
right: {
|
|
alignSelf: 'flex-end',
|
|
},
|
|
});
|