3

I want to display my current location but I cannot.

I add permission code to AndroidManifest.xml like below.

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

However my emulator did not ask to me 'Allow to 00 access your location while you are using the app'.

Below is my app.js code.

import React, { Component } from 'react';
import { Alert, StyleSheet, Text, View, TouchableOpacity, PermissionsAndroid } from 'react-native';

export default class App extends Component {
    state = {
        location: null
    };

    findCoordinates = () => {
        navigator.geolocation.getCurrentPosition(
            position => {
                const location = JSON.stringify(position);

                this.setState({ location });
            },
            error => Alert.alert(error.message),
            { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
        );
    };

    render() {
        return (
            <View style={styles.container}>
                <TouchableOpacity onPress={this.findCoordinates}>
                    <Text style={styles.welcome}>Find My Coords?</Text>
                    <Text>Location: {this.state.location}</Text>
                </TouchableOpacity>
            </View>
        );
    }
}

const styles = StyleSheet.create({
    container: {
        flex: 1,
        justifyContent: 'center',
        alignItems: 'center',
        backgroundColor: '#F5FCFF'
    },
    welcome: {
        fontSize: 20,
        textAlign: 'center',
        margin: 10
    }
});
Tuan Luong
  • 3,902
  • 1
  • 16
  • 18
Contractor
  • 51
  • 2
  • 7
  • Are you using which version of react native ??? where is you map code?? – Vishal Dhanotiya Dec 24 '19 at 06:11
  • This is my react-native version. "dependencies": { "react": "16.9.0", "react-native": "0.61.5", "react-native-android-location-enabler": "^1.2.0", "react-native-geolocation-service": "^3.1.0", "react-native-maps": "0.26.1" – Contractor Dec 24 '19 at 09:13
  • Google maps works well. However map on the emulator did not go to my current location. So I change my code to confirming my location. – Contractor Dec 24 '19 at 09:28
  • above 0.60.0 version of react native `navigator.geolocation.getCurrentPosition` is not working please use https://www.npmjs.com/package/@react-native-community/geolocation – Vishal Dhanotiya Dec 24 '19 at 11:43
  • 's current location is not directly working in emulator you need to send lat long form DDMS in android studio. – Vishal Dhanotiya Dec 24 '19 at 18:30
  • I also try to use @react-native-community/geolocation library. However my console indicate coordinate of google HQ still. Dear Dhanotiya, could you explain more in detial? – Contractor Dec 25 '19 at 17:13
  • Please check following url https://stackoverflow.com/a/45098850/9158543 – Vishal Dhanotiya Dec 25 '19 at 18:01

1 Answers1

2

I solved the problems using bellow code.

async function requestGeolocationPermission() {
  try {
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
      {
        'title': 'Ridesharer Geolocation Permission',
        'message': 'Ridesharer needs access to your current location so you can share or search for a ride'
      });
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      console.log("You can use the geolocation")
    } else {
      console.log("Geolocation permission denied")
    }
  } catch (err) {
    console.warn(err)
  }
}
requestGeolocationPermission();
Contractor
  • 51
  • 2
  • 7