Last active
March 24, 2025 23:27
-
-
Save forrestwilkins/60d6a76d393d44343693a7ac20545ee8 to your computer and use it in GitHub Desktop.
Geolocation
This file contains hidden or 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
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | |
package="com.covidmap"> | |
<uses-permission android:name="android.permission.INTERNET" /> | |
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> | |
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> | |
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> | |
<application | |
android:name=".MainApplication" | |
android:label="@string/app_name" | |
android:icon="@mipmap/ic_launcher" | |
android:roundIcon="@mipmap/ic_launcher_round" | |
android:allowBackup="false" | |
android:theme="@style/AppTheme"> | |
<activity | |
android:name=".MainActivity" | |
android:label="@string/app_name" | |
android:configChanges="keyboard|keyboardHidden|orientation|screenSize|uiMode" | |
android:launchMode="singleTask" | |
android:windowSoftInputMode="adjustResize"> | |
<intent-filter> | |
<action android:name="android.intent.action.MAIN" /> | |
<category android:name="android.intent.category.LAUNCHER" /> | |
</intent-filter> | |
</activity> | |
<activity android:name="com.facebook.react.devsupport.DevSettingsActivity" /> | |
</application> | |
</manifest> |
This file contains hidden or 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
/** | |
* Sample React Native App | |
* https://github.com/facebook/react-native | |
* | |
* @format | |
* @flow strict-local | |
*/ | |
import React, { useState } from 'react'; | |
import { | |
SafeAreaView, | |
StyleSheet, | |
ScrollView, | |
View, | |
Text, | |
StatusBar, | |
TouchableOpacity, | |
Alert, | |
Platform, | |
PermissionsAndroid | |
} from 'react-native'; | |
// To have geolocation API aligned with the browser (cross-platform apps), or to support backward compatibility | |
navigator.geolocation = require('@react-native-community/geolocation'); | |
const App: () => React$Node = () => { | |
const [location, setLocation] = useState(null); | |
const requestLocationPermission = async () => { | |
try { | |
const granted = await PermissionsAndroid.request( | |
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION, | |
{ | |
title: 'Location Permission', | |
message:'Get your location to post request', | |
buttonNeutral: "Ask Me Later", | |
buttonNegative: "Cancel", | |
buttonPositive: "OK" | |
} | |
); | |
if (granted === PermissionsAndroid.RESULTS.GRANTED) { | |
console.log("You can use geolocation"); | |
} else { | |
console.log(JSON.stringify(granted)); | |
console.log(PermissionsAndroid.RESULTS.GRANTED); | |
} | |
} catch (err) { | |
console.warn(err); | |
} | |
}; | |
const findCoordinates = () => { | |
//requestPermissions(); | |
navigator.geolocation.getCurrentPosition( | |
position => { | |
const location = JSON.stringify(position); | |
setLocation(location); | |
}, | |
error => Alert.alert(error.message), | |
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 } | |
); | |
}; | |
return ( | |
<> | |
<SafeAreaView> | |
<ScrollView | |
contentInsetAdjustmentBehavior="automatic"> | |
<View> | |
<Text> | |
Overthrow capitalism! | |
</Text> | |
<TouchableOpacity onPress={requestLocationPermission}> | |
<Text>Request permissions for location</Text> | |
</TouchableOpacity> | |
<TouchableOpacity onPress={findCoordinates}> | |
<Text>Find My Coords?</Text> | |
<Text>Location: {location}</Text> | |
</TouchableOpacity> | |
</View> | |
</ScrollView> | |
</SafeAreaView> | |
</> | |
); | |
}; | |
export default App; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment