首页 > 解决方案 > 在 react-native 中获取按 headerRight 的位置

问题描述

在 react native 中单击 headerRight 中的按钮之一时,如何获得位置?我尝试按照文档进行操作,但没有显示任何内容。这是代码:

static navigationOptions = {
    headerRight: (
      <View style={{marginLeft: 8, marginRight: 8, flexDirection: 'row', alignItems: 'center' }}>
        <TouchableOpacity style={{ marginRight: 10 }} onPress={ () =>{ }}>
            <Image style={{ width: 22, height: 22 }} source={require("../Images/search.png")} />
        </TouchableOpacity>
        <TouchableOpacity style={{marginLeft: 10, marginRight: 10 }} onPress={ () =>{ this.displayPosition()}}>
            <Image style={{ width: 22, height: 22 }} source={require("../Images/localisation.png")} />
        </TouchableOpacity>
        <TouchableOpacity style={{marginLeft: 10 }} onPress={ () =>{ }}>
            <Image style={{ width: 22, height: 22 }} source={require("../Images/refresh.png")} />
        </TouchableOpacity>
      </View>
    ),
  };

displayPosition(){
    Geolocation.getCurrentPosition(
      (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 },
    );
     Alert.alert(this.state.latitude, this.state.longitude)
  }

标签: react-native

解决方案


请澄清一下,您是想从地理位置获取(纬度,经度)位置还是只是从 headerRight 获取项目位置?


推荐阅读