【问题标题】:React-Native-Maps don't seem to rerender once the geo location is acquired获取地理位置后,React-Native-Maps 似乎不会重新渲染
【发布时间】:2017-07-29 21:42:54
【问题描述】:

我正在尝试创建一个简单的应用程序来加载谷歌地图(使用 airbnb 的 react-native-maps 库)并显示用户的当前位置。我看到的是地图总是显示默认的初始位置,而不是在获取用户位置后重新渲​​染。

我正在使用 React 0.42 并且仅在 iOS 上进行测试。这里有一些代码需要澄清:

1.) 我设置了一个初始状态

state = {
      region: {
        latitude: 52,
        longitude: 5,
        latitudeDelta: 0.0922,
        longitudeDelta: 0.0421
      }
  }

2.) 我在 componentDidMount 中获取用户的位置

componentDidMount() {
    navigator.geolocation.getCurrentPosition(
      (position) => {
        this.setState({
          region: {
            latitude: position.coords.latitude,
            longitude: position.coords.longitude,
            latitudeDelta: 0.01,
            longitudeDelta: 0.0011
          }
        });
      },
      (error) => alert(JSON.stringify(error)),
      {enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
    );
  }

3.) 使用渲染时,我会显示带有初始区域的地图,并希望在获取用户位置后该区域会发生变化

render() {
    return (
     <View style={{ flex: 1 }}>
       <View style={{backgroundColor: 'coral', height: 70, justifyContent: 'center', alignItems: 'center'}}>
         <Text>
            <Text>longitude: {this.state.region.longitude}</Text>
            <Text>latitude: {this.state.region.latitude}</Text>
        </Text>
       </View>
       <View style={styles.container}>
         <MapView
           provider={PROVIDER_GOOGLE}
           style={styles.map}
           initialRegion={this.state.region}
           region={this.state.region}
           onRegionChange={this.onRegionChange}
           onRegionChangeComplete={this.reloadEntities}
         />
       </View>
     </View>
   );
 }

这里是onRegionChange,只是用新区域更新状态,我相信这会导致重新渲染

onRegionChange = (region) => {
  this.setState({ region });
}

注意:经度和纬度文本值会随着地图上区域的变化而更新,并且一旦获取用户的位置,它们就会更新。

所以我有点困惑,为什么地图在获取用户位置后不会改变它所显示的内容。任何帮助将不胜感激!

更新:我查看了这个帖子:https://github.com/airbnb/react-native-maps/issues/43,它似乎主要围绕 Android 展开,但我确实尝试删除 enableHighAccuracy 选项,但没有成功。

【问题讨论】:

    标签: react-native react-native-maps


    【解决方案1】:

    将 MapView 的 region 设置为您所在区域状态的值 this.state.region

    您需要获取当前位置并将其设置为区域,然后使用watchPosition 函数在每次设备检测到位置发生变化时获取坐标,这次将新值设置为您的@987654325 @状态。

    这样就可以了

    import React, { Component } from 'react';
    import MapView from 'react-native-maps';
    import { AppRegistry, View } from 'react-native';
    
    const { width, height } = Dimensions.get('window');
    const ASPECT_RATIO    = width / height;
    const LATITUDE        = 37.78825;
    const LONGITUDE       = -122.4324;
    const LATITUDE_DELTA  = 0.0122;
    const LONGITUDE_DELTA = LATITUDE_DELTA * ASPECT_RATIO;
    const SPACE           = 0.01;
    
    class Test extends Component {
      constructor() {
        super();
        this.state = {
          region: {
            latitude: LATITUDE,
            longitude: LONGITUDE,
            latitudeDelta: LATITUDE_DELTA,
            longitudeDelta: LONGITUDE_DELTA
          }
        }
      }
    
    componentDidMount() {
        navigator.geolocation.getCurrentPosition(
          (position) => {
            this.setState({
              region: {
                latitude: position.coords.latitude,
                longitude: position.coords.longitude,
                latitudeDelta: LATITUDE_DELTA,
                longitudeDelta: LONGITUDE_DELTA,
                accuracy: position.coords.accuracy
              }
            });
          },
          (error) => alert(error.message),
          {timeout: 10000}
        );
    
        this.watchID = navigator.geolocation.watchPosition((position) => {
          const newRegion = {
            latitude: position.coords.latitude,
            longitude: position.coords.longitude,
            latitudeDelta: LATITUDE_DELTA,
            longitudeDelta: LONGITUDE_DELTA,
            accuracy: position.coords.accuracy
          }
          this.setState({newRegion});
        });
      }
    
      componentWillUnmount() {
        navigator.geolocation.clearWatch(this.watchID);
      }
    
      render() {
        return (
          <View style={styles.container}>
            <MapView
              style={styles.map}
              region={this.state.region}
              showsUserLocation={true}
              followUserLocation={true}>
            </MapView>
          </View>
        );
      }
    }
    
    const styles = StyleSheet.create({
      container: {
        ...StyleSheet.absoluteFillObject,
        justifyContent: 'flex-end',
        alignItems: 'center',
      },
      map: {
        ...StyleSheet.absoluteFillObject,
      },
    });
    
    AppRegistry.registerComponent('Test', () => Test);
    

    请务必在 MapView 中启用showsUserLocationfollowUserLocation,这样应用会询问用户当前位置。

    followUserLocation 依赖于showUserLocation

    恒定的经度和纬度仅作为示例。

    希望对你有帮助

    【讨论】:

    • 谢谢!这有助于理解 MapView 上的一些附加选项。但是,似乎@Adam-Patterson 建议删除 onRegionChangeComplete 始终显示正确的位置。话虽如此,在区域更改完成后如何调用回调函数?
    • @Rudy 很高兴为您提供帮助,正如它所说,onRegionChangeComplete 已经是一个回调,当区域更改时调用一次,例如当用户完成移动地图时,您可以覆盖它的行为在其中添加您需要的逻辑。 akshay2604 在 react-native-maps 存储库上打开了一个问题,也许可以帮助您github.com/airbnb/react-native-maps/issues/846
    • 完美,谢谢!似乎即使在 iOS 上我也遇到了同样的问题。我将在该问题线程中添加一些其他信息。再次感谢!
    • 正确的拼写是followsUserLocation not followUserLocation
    【解决方案2】:

    您不应该将initialRegion 属性与region 属性一起使用:

    仅当您不想控制 除了初始区域之外的地图视口。

    Source

    删除 initialRegion 属性后它应该可以工作。

    【讨论】:

      【解决方案3】:

      摆脱onRegionChangeComplete={this.reloadEntities} 也很重要。

      每次区域更改时,react-native 都会重新加载并默认返回到您在构造函数中声明的初始状态。

      【讨论】:

      • 谢谢,这似乎可以解决问题,正如我在另一条评论中指出的那样,在区域更改完成后,您还能如何调用回调函数?
      猜你喜欢
      • 1970-01-01
      • 2022-08-12
      • 1970-01-01
      • 1970-01-01
      • 2017-01-21
      • 1970-01-01
      • 1970-01-01
      • 2019-09-26
      • 2018-12-27
      相关资源
      最近更新 更多