React Native-ACCESS_FINE_LOCATION的位置跟踪问题

时间:2018-09-27 19:20:44

标签: android-studio react-native geolocation react-native-android

在本教程中,我尝试使用watchPosition位置跟踪:https://hackernoon.com/react-native-basics-geolocation-adf3c0d10112

出于某种原因,在Android虚拟设备上,只有getcurrentposition可以工作,并且watchposition返回了一个错误,即即使我很确定,我的android清单文件中也没有ACCESS_FINE_LOCATION我在我可以找到的每个清单文件中都包含了这一行。

import React, { Component } from 'react';

 import { View, Text } from 'react-native';



 class GeolocationExample extends Component {

 constructor(props) {

 super(props);



 this.state = {

   latitude: null,

   longitude: null,

   error: null,

 };

  }



  componentDidMount() {

    this.watchId = navigator.geolocation.watchPosition(

      (position) => {

        this.setState({

          latitude: position.coords.latitude,

          longitude: position.coords.longitude,

          error: null,

        });

      },

      (error) => this.setState({ error: error.message }),

      { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000, distanceFilter: 10 },

    );

  }



  componentWillUnmount() {

    navigator.geolocation.clearWatch(this.watchId);

  }



  render() {

    return (

      <View style={{ flexGrow: 1, alignItems: 'center', justifyContent: 'center' }}>

        <Text>Latitude: {this.state.latitude}</Text>

        <Text>Longitude: {this.state.longitude}</Text>

        {this.state.error ? <Text>Error: {this.state.error}</Text> : null}

      </View>

    );

  }

}



export default GeolocationExample;

0 个答案:

没有答案