代码之家  ›  专栏  ›  技术社区  ›  Rexam

为什么geolocation不能在Android Emulator上使用React Native?

  •  0
  • Rexam  · 技术社区  · 7 年前

    import React, { Component } from "react";
    import {
      Platform,
      StyleSheet,
      Text,
      View,
      Alert,
      TouchableOpacity
    } 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(error.message),
          { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 }
        );
    
        alert("SKIPPED");
      };
    
      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({
      ... some style ...
    });
    

    由于我使用的是Android仿真器,所以在 AndroidManifest.xml 文件位于 android\app\src\main application 标签:

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

    getCurrentPosition 函数似乎只是被跳过了。状态未更新,未显示任何错误,并显示警报“跳过”。

    这就是我在“跳过”警报解除后在屏幕上看到的情况:

    enter image description here

    2 回复  |  直到 7 年前
        1
  •  0
  •   Community Mohan Dere    6 年前

    国际标准化组织

    安卓

        2
  •  0
  •   Rexam    7 年前

    显然,我必须重新启动模拟器,然后它才能正常工作。找到一个暗示 here .