React Native Maps显示标记标注

问题描述 投票:0回答:1

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

ScreenShot:

enter image description here

代码:

<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个回答
0
投票

将地图动画化到标记是必须完成的,因此您需要为MapView组件获取reference,例如将此道具添加到MapView:

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

在最简单的情况下,this应该引用还渲染标记的组件,以便您可以在Marker的渲染中引用this.map。如果您的MapView组件包含其他内容,则需要forward ref以获取对实际MapView组件的引用

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

<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)
    }}
© www.soinside.com 2019 - 2024. All rights reserved.