Created
February 27, 2019 08:08
-
-
Save ecdundar/726115777c2d5bcdb06ce26baf021b60 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import {View, Text, StyleSheet, Image ,PermissionsAndroid,Platform} from 'react-native'; | |
export default class App extends React.Component { | |
state = { | |
currentLongitude: 'unknown', | |
currentLatitude: 'unknown', | |
} | |
componentDidMount = () => { | |
var that =this; | |
if(Platform.OS === 'ios'){ | |
this.callLocation(that); | |
}else{ | |
async function requestLocationPermission() { | |
try { | |
const granted = await PermissionsAndroid.request( | |
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,{ | |
'title': 'Location Access Required', | |
'message': 'This App needs to Access your location' | |
} | |
) | |
if (granted === PermissionsAndroid.RESULTS.GRANTED) { | |
that.callLocation(that); | |
} else { | |
alert("Permission Denied"); | |
} | |
} catch (err) { | |
alert("err",err); | |
console.warn(err) | |
} | |
} | |
requestLocationPermission(); | |
} | |
} | |
callLocation(that){ | |
navigator.geolocation.getCurrentPosition( | |
(position) => { | |
const currentLongitude = JSON.stringify(position.coords.longitude); | |
const currentLatitude = JSON.stringify(position.coords.latitude); | |
that.setState({ currentLongitude:currentLongitude }); | |
that.setState({ currentLatitude:currentLatitude }); | |
}, | |
(error) => alert(error.message), | |
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 } | |
); | |
that.watchID = navigator.geolocation.watchPosition((position) => { | |
console.log(position); | |
const currentLongitude = JSON.stringify(position.coords.longitude); | |
const currentLatitude = JSON.stringify(position.coords.latitude); | |
that.setState({ currentLongitude:currentLongitude }); | |
that.setState({ currentLatitude:currentLatitude }); | |
}); | |
} | |
componentWillUnmount = () => { | |
navigator.geolocation.clearWatch(this.watchID); | |
} | |
render() { | |
return ( | |
<View style = {styles.container}> | |
<Text style = {styles.boldText}> | |
Konumunuz | |
</Text> | |
<Text style={{justifyContent:'center',alignItems: 'center',marginTop:16}}> | |
Longitude: {this.state.currentLongitude} | |
</Text> | |
<Text style={{justifyContent:'center',alignItems: 'center',marginTop:16}}> | |
Latitude: {this.state.currentLatitude} | |
</Text> | |
</View> | |
) | |
} | |
} | |
const styles = StyleSheet.create ({ | |
container: { | |
flex: 1, | |
alignItems: 'center', | |
justifyContent:'center', | |
marginTop: 50, | |
padding:16, | |
backgroundColor:'white' | |
}, | |
boldText: { | |
fontSize: 30, | |
color: 'red', | |
} | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment