Eiman
Eiman

Reputation: 49

This requestPermissionsAsync() code only works in android

This code only works in Android and Android Emulator, the map shows my exact location, but not in expo iOS simulator, when I use let { status } = await Location.requestPermissionsAsync(); for iOS there is nothing coming back but when I use let { status } = await Permissions.askAsync(Permissions.LOCATION) a value is returned.

import React, { Component } from 'react';
import { Text, View } from 'react-native';
import MapView from 'react-native-maps';
import * as Location from 'expo-location';

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

  _getLocationAsync = async () => {
    // only works in android and android emulator
    let { status } = await Location.requestPermissionsAsync();
    if (status !== 'granted') {
      console.log('Location permission not granted!');
      return;
    }
    let myLocation = await Location.getCurrentPositionAsync({});
    this.setState({ location: myLocation });
  };

  componentDidMount() {
    this._getLocationAsync();
  }

  render() {
    if (!this.state.location) {
      return <View />;
    }
    return (
      <MapView
        style={{ flex: 1 }}
        initialRegion={{
          latitude: this.state.location.coords.latitude,
          longitude: this.state.location.coords.longitude,
          latitudeDelta: 0,
          longitudeDelta: 0,
        }}
      />
    );
  }
}
  1. Why with iOS this function doesn't do anything await Location.requestPermissionsAsync(), but it works perfectly with Android?
  2. Why this await Location.getCurrentPositionAsync({}); returns a Promise in Android but nothing in iOS?

Upvotes: 1

Views: 4086

Answers (1)

KBH
KBH

Reputation: 365

Be sure that you config correctly location permission on iOS: https://docs.expo.io/versions/latest/sdk/location/#configuration

Upvotes: 1

Related Questions