简体   繁体   English

react-native-maps:如何在不重新渲染整个地图的情况下添加标记?

[英]react-native-maps: How do I add markers without re-rendering the entire map?

I'm building a map that displays markers to show the location of nearby merchants.我正在构建一个显示标记的地图,以显示附近商家的位置。 I'm passing a 'data' array as props to the map.我将一个“数据”数组作为道具传递给地图。 I've set an interval such that every two seconds, I fetch more merchants from my API, and the 'data' array grows.我设置了一个间隔,每两秒,我从我的 API 中获取更多商家,并且“数据”数组增长。

I'm fetching the data inside componentWillReceiveProps.我在 componentWillReceiveProps 中获取数据。 fetchData() appends more merchants to the data array. fetchData() 将更多商家追加到数据数组中。

componentWillReceiveProps(nextProps) {
    if(nextProps.loading == false) {
        setTimeout(nextProps.fetchData,2000);
    }

And inside my MapView component -在我的 MapView 组件内部 -

{
    this.props.data.length > 0 && this.props.data.map(merchant => (
        <MapView.Marker
            coordinate={{latitude: Number(merchant.latitude), longitude: Number(merchant.longitude)}}
            title={merchant.name}
            description={merchant.address}
            identifier={"" + merchant.id}
            key={merchant.id}
        />
    ))
}

My problem: Whenever I call fetchData(), the new markers are rendered.我的问题:每当我调用 fetchData() 时,都会呈现新的标记。 However, the whole map is rendered again.然而,整个地图再次被渲染。 I do not want this kind of blinking behaviour.我不想要这种眨眼的行为。

I'd be very grateful for any kind of help/suggestions.我将非常感谢任何形式的帮助/建议。 Thanks in advance!提前致谢!

PS You can find the visual here PS你可以在这里找到视觉效果

您可以使用自定义类组件代替MapView.marker ,然后使用componentShouldUpdate()函数来指定标记是否需要在某些条件下更新。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM