mogzzer

Untitled

Feb 16th, 2020
123
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.52 KB | None | 0 0
  1. import React from 'react';
  2. import {
  3. View,
  4. StyleSheet,
  5. Text,
  6. PermissionsAndroid,
  7. Platform
  8. } from 'react-native';
  9. import MapView from 'react-native-maps';
  10. import Geolocation from 'react-native-geolocation-service';
  11. import PlaceInput from '../components/PlaceInput';
  12.  
  13.  
  14. //const API_KEY = 'AIzaSyDwVK7iTyCi1liykASBPsaalapSSClq5a8';
  15.  
  16. class MapScreen extends React.Component {
  17.  
  18. constructor(props){
  19. super(props);
  20.  
  21. this.state = {
  22. curLoc: null
  23. }
  24. this._requestUserLocation();
  25. }
  26.  
  27.  
  28. if (hasMapPermission) {
  29. Geolocation.getCurrentPosition(
  30. (position) => {
  31. console.log(position);
  32. },
  33. (error) => {
  34. // See error code charts below.
  35. console.log(error.code, error.message);
  36. },
  37. { enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 }
  38.  
  39. );
  40. let curLoc = {
  41. userLatitude: position.coords.latitude,
  42. userLongitude: position.coords.longitude,
  43. latitudeDelta: 0.045,
  44. longitudeDelta: 0.045,
  45. }
  46. this.setState({curLoc : curLoc})
  47. }
  48.  
  49. async _requestUserLocation(){
  50. try{
  51. if(Platform.OS === 'android'){
  52. const granted = await PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION);
  53. if(granted === PermissionsAndroid.RESULTS.GRANTED){
  54. this.setState({hasMapPermission: true})
  55. }else{
  56. this.setState({hasMapPermission: false})
  57. }
  58. }
  59. } catch(err){
  60. console.warn(err);
  61. }
  62. }
  63.  
  64. render(){
  65. return(
  66. <View style={styles.container}>
  67. <PlaceInput />
  68. <MapView
  69. style={styles.map}
  70. showsUserLocation
  71. followsUserLocation
  72. region={this.state.curLoc}
  73.  
  74. />
  75. </View>
  76. );
  77. }
  78. }
  79.  
  80. export default MapScreen;
  81.  
  82. const styles = StyleSheet.create({
  83. container:{
  84. position: 'absolute',
  85. top: 0,
  86. left: 0,
  87. right: 0,
  88. bottom: 0,
  89. justifyContent: 'flex-end',
  90. alignItems: 'center',
  91. },
  92. map: {
  93. flex: 1,
  94. position: 'absolute',
  95. top: 0,
  96. left: 0,
  97. right: 0,
  98. bottom: 0,
  99. },
  100. })
Add Comment
Please, Sign In to add comment