【问题标题】:React Native Maps show marker calloutReact Native Maps 显示标记标注
【发布时间】:2020-02-22 04:24:39
【问题描述】:

我正在使用react-native-maps 来渲染 GOOGLE 地图,其中包含我从同事的 API 中获取的大量标记。在地图下方,我有一个 FlatList 来自屏幕上每个标记的渲染数据。 FlatList 的renderItem 中的项目是TouchableOpacity。当我按下列表中的相应按钮时,如何专注于标记标注?

截图:

代码:

<Container>
  <StatusBar
    barStyle={theme.colors.statusDark}
    backgroundColor={theme.colors.background1}
  />
  <GoogleMaps>
    {markers.map(marker => (
      <MyMarker
        key={marker.uid}
        identifier={marker.uid}
        markerLatitude={marker.position.lat}
        markerLongitude={marker.position.lng}
        title={marker.name}
        address={marker.address}
        cashback={marker.cashback}
      />
    ))}
  </GoogleMaps>
  <DivisorSimple />
  <ListContainer>
    {fetchIsLoading ? (
      <ActivityIndicator size='large' />
    ) : (
      <FlatList
        data={markers}
        renderItem={({ item }) => (
          <ListMapItem
            handleClick={() => {}
            title={item.name}
            address={item.address}
            cashback={item.cashback}
            handleGetRoute={() => handleGetRoute(item.position.lat, item.position.lng)}
          />
        )}
        keyExtractor={item => item.uid}
      />
    )}
  </ListContainer>
</Container>

【问题讨论】:

    标签: react-native maps marker


    【解决方案1】:

    动画映射到标记是命令式完成的,因此您需要为 MapView 组件获取reference,例如将此道具添加到 MapView:

    <MapView
      ...
      ref={ref => this.map = ref}
    >
      {data.map(item => <Marker ... />)}
    </MapView>
    

    在最简单的情况下,this 应该指代也在渲染您的标记的组件,以便您可以在标记的渲染中引用this.map。如果您的 MapView 组件被包装到其他东西中,您将需要 forward ref 以获取实际 MapView 组件的引用

    然后,您需要跟踪当前显示在地图上的区域,并且当用户移动相机时它会发生变化

    <MapView
      ...
      onRegionChangeComplete={region => this.setState({ region })}
    >
    

    之后,您可以使用MapView's animateToRegion method 关注标记

    // ListMapItem's handleClick prop
    handleClick={() => {
          // construct new region from marker's lat/lng
          // and current region's deltas to keep zoom level
          const newRegion = {
            latitude: item.position.lat,
            longitude: item.position.lng,
            latitudeDelta: this.state.region.latitudeDelta,
            longitudeDelta: this.state.region.latitudeDelta,
          }
          // animate camera to that region over 500 ms
          this.map.animateToRegion(newRegion, 500)
        }}
    

    【讨论】:

    • 非常感谢!这可能会起作用,但我无法在我的功能组件中转发 refs。有什么办法可以帮助我吗?
    • 我正在关注这个stackoverflow.com/questions/57005663/… 但我得到了Warning: Function components cannot be given refs. Attempts to access this ref will fail. Did you mean to use React.forwardRef()? 它看起来像是react-redux 的东西
    • 功能组件中的转发引用与类组件中的转发不同。看看这里reactjs.org/docs/…
    • 遗憾的是我在官方文档中没有找到任何解决方案,甚至没有提到 useRef 钩子。
    • 如果你正在使用钩子,请看这里reactjs.org/docs/hooks-reference.html#useref
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 2019-09-29
    • 1970-01-01
    • 2021-11-25
    相关资源
    最近更新 更多