Не удается получить текущее местоположение в компоненте MapView в проекте Expo - PullRequest
0 голосов
/ 06 февраля 2020

Я пытаюсь получить текущее местоположение с react-native-maps в проекте Expo React Native через компонент MapView. Я запускаю приложение на симуляторе iOS и принимаю права доступа к местоположению при появлении запроса на выбор. У меня установлена ​​expo-location, и я получаю правильное местоположение при использовании класса Location из expo-location. Не уверен, что это MapView или проблема с правами доступа.

Вот мой код:

Приложение. json

import React, { useState, useEffect } from 'react';
import { View, StyleSheet, Dimensions } from 'react-native';
import MapView, { PROVIDER_GOOGLE } from 'react-native-maps';
import * as Permissions from 'expo-permissions';

export default function App() {
  const [location, setLocation] = useState(false);

  useEffect(() => {
    getLocationAsync();
  }, []);

  async function getLocationAsync() {
    // asking for permission
    const { status, permissions } = await Permissions.askAsync(Permissions.LOCATION);
    if (status === 'granted') {
      setLocation(true);
    } else {
      throw new Error('Location permission not granted');
    }
  }

  return (
    <View style={styles.container}>
      {location && 
        <MapView
          provider={PROVIDER_GOOGLE}
          showUserLocation={true}
          followsUserLocation={true}
          showsMyLocationButton={true}
          initialRegion={{
            latitude: 47.0693453,
            longitude: 15.4390034,
            latitudeDelta: 0.0922,
            longitudeDelta: 0.0421,
          }}
          style={styles.mapStyle}
        />
      }
    </View>
  );
}

const styles = StyleSheet.create({
   container: {
    backgroundColor: '#ddeeff',
    alignItems: 'center',
    justifyContent: 'center',
    flex: 1
   },
   mapStyle: {
    width: Dimensions.get('window').width,
    height: 300
   },
 });

приложение. json

{
  "expo": {
    ...
    "ios": {
      "supportsTablet": true,
      "config": {
        "googleMapsApiKey": "MY_API_KEY"
      },
      "infoPlist": {
        "NSLocationWhenInUseUsageDescription": "This app uses your location to ...",
        "NSLocationAlwaysAndWhenInUseUsageDescription": "This app uses your location to ..."
      }
    },
    "android": {
      "config": {
        "googleMaps": {
          "apiKey": "MY_API_KEY"
        }
      }
    }
  }
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...