javascript - onUserLocationChange 更改 React Native map 区域

标签 javascript google-maps react-native

我正在使用“react-native-maps”中的 MapView

我无法显示“showsMyLocationButton”,所以我正在实现自己的。

我想在不使用 React Native 的地理定位的情况下执行此操作,因为我发现它比 react-native-maps prop showsUserLocation 慢。我使用返回坐标的 onUserLocationChange 事件。但是,当触发 onUserLocationChange 事件并设置包含 userLocation 的状态时,它会自动更新区域,而无需我明确要求。

这是我的代码:

display_my_location(coordinate){
  alert("region changed")
  this.setState({
    region: {
      latitude: coordinate.latitude,
      longitude: coordinate.longitude,
      latitudeDelta: 0.004,
      longitudeDelta: 0.004
    },
  });
}

setUserLocation(coordinate){
  //alert("User location changed MAP SHOULDNT MOVE")
  this.setState({
    userLocation: {
      latitude: coordinate.latitude,
      longitude: coordinate.longitude,
      latitudeDelta: 0.004,
      longitudeDelta: 0.004
    }
  })
}

<MapView
      style={styles.map}
      showsMyLocationButton={true}
      onUserLocationChange={locationChangedResult => this.setUserLocation(locationChangedResult.nativeEvent.coordinate)}
      initialRegion={this.state.region_amon}
      showsUserLocation={true}
      region={this.state.region}
      mapType={this.state.map_style}
      showsCompass = {true}
      showsMyLocationButton={true}
      chacheEnabled={false}
      zoomEnabled={true}

最佳答案

我相信您可以通过从 MapView 中完全删除 region 属性来解决这个问题。如果您需要以编程方式更改 map 的边界框,您可以将 ref 附加到 map 并使用此处适当的 MapView 方法在 componentDidUpdate 中更新:

https://github.com/react-community/react-native-maps/blob/master/docs/mapview.md#methods

关于javascript - onUserLocationChange 更改 React Native map 区域,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51976077/

相关文章:

asynchronous - React Native 计算繁重的任务

javascript - TypeError : this. props.navigation.getParam 在使用 React Native 传递参数时不是函数

javascript - Instagram embeds.js 库未加载

java - 将坐标纬度和经度更改为 andress

ios - 按下完成按钮时如何添加标记?

google-maps - 地理图表区域可点击的网址

javascript - 来自百度的ECharts

javascript - V8什么时候开始编译并执行与事件循环堆栈相关的代码?

javascript - $.parseJSON() 返回未定义

react-native - react native 导航 : Reset Stack Navigator