How to Activate GPS Icon in react-native?

最后都变了- 提交于 2020-05-24 05:45:12

问题


I want to appear GPS Icon on the status bar when I open app or screen that's contain Map, So I add a function that asks a user at the first time to allow Location permission and it's work very well and I can get Lat/Long but GPS "Location" Icon did not activate so how can I force an app to activate this icon?

here's my Ask for permission function

 async requestLocationPermission() {
    try {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
        {
          title: 'App Location Permission',
          message: 'App needs access to your Location ',
          buttonNeutral: 'Ask Me Later',
          buttonNegative: 'Cancel',
          buttonPositive: 'OK',
        },
      );
      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        this.getLocation();
      } else {
        this.setState({next: false});
      }
    } catch (err) {
      console.log('err ', err);
    }
  }




getLocation = async () => {
    const {companyLocation, Domain} = this.props.navigation.state.params;
    Geolocation.getCurrentPosition(
      position => {
        this.setState(
          {
            mapData: {
              latitude: position.coords.latitude,
              longitude: position.coords.longitude,
              latitudeDelta: 0.015,
              longitudeDelta: 0.0121,
            },
            markerData: {
              latitude: position.coords.latitude,
              longitude: position.coords.longitude,
              latitudeDelta: 0.015,
              longitudeDelta: 0.0121,
            },

            error: null,
          },
          () => {
            let {
              latitudeDelta,
              longitudeDelta,
              ...userLocation
            } = this.state.mapData;

            let howFar = Math.round(haversine(userLocation, companyLocation));
            howFar > Domain
              ? (alert('we not work in your area.'),
                this.setState({next: false}))
              : null;
          },
        );
      },
      error => this.setState({error: error.message}),
    );
    this.watchID = Geolocation.watchPosition(position => {
      const latitude = position.coords.latitude;
      const longitude = position.coords.longitude;
      this.setState({
      mapData: {
        latitude: latitude,
        longitude: longitude,
        latitudeDelta: 0.015,
        longitudeDelta: 0.0121,
      },
    });
    });
  };

来源:https://stackoverflow.com/questions/61361533/how-to-activate-gps-icon-in-react-native

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!