react-native-maps: [iOS] Markers not showing on re-renders
Hello, I have an issue with the markers rendering. What happens is the following: 1. Initial render of the map: markers.length = 1; (The map renders one marker) 2. onRegionChangeComplete => markers.length = 2; (The marker renders only one marker in the new position) 3. Any change in the map that makes the markers state change, results in rendering one of the total markers in a new position.
This is basically how we are setting the markers on the map:
<View style={styles.container}>
<MapView
ref={(ref) => { this.map = ref; }}
style={styles.map}
provider={MapView.PROVIDER_GOOGLE}
mapType="none"
minZoomLevel={2}
maxZoomLevel={18}
rotateEnabled={false}
initialRegion={this.state.region}
onRegionChangeComplete={this.updateRegion}
onLayout={this.onLayout}
moveOnMarkerPress={false}
onPress={e => this.mapPress(e.nativeEvent.coordinate)}
>
{this.props.markers.map((marker, index) => {
const coordinates = {
latitude: marker.geometry.coordinates[1],
longitude: marker.geometry.coordinates[0]
};
return (
<MapView.Marker
key={`Alert-marker-${index}`}
coordinate={coordinates}
anchor={{ x: 0.5, y: 0.5 }}
onPress={() => this.props.selectAlert(coordinates)}
zIndex={1}
draggable={false}
>
<View
style={[
styles.markerIcon,
this.props.type === 'primary' ? styles.primary : styles.secondary
]}
/>
</MapView.Marker>
);
})}
</MapView>
</View>
I’ve debug the rendering, so I can confirm that markers is receiving more elements than the ones that the map is displaying.
Any help would be much appreciated, this issue is currently blocking me and my colleagues. Even if it isn’t a complete solution, pointing in the right direction would be awesome. Thanks 😄 !
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Comments: 17 (3 by maintainers)
I found the problem, i was rendering all markers inside the MapView with a View component, i don’t know why this was working perfectly on Android, so i tried to remove this View and finally the markers were showed on the iOS device 😄
Please close.
Thanks for the help! The solution you suggested didn’t work. I’m sharing our workaround for anyone else that might run into this issue again. In the
MapViewcomponent to forceUpdate it when the markers changed we added a propkey={markers.length}this way any time the markers changed the map re-rendered. This is not a complete solution because if the marker changed and the length if the same the map will not re-render. A solution for this is prefixing/suffixing the markers.length with some id or something else that will only change when you want the map to re-render.Heads up: Don’t prefix/suffix with a value that always changes (i.e.
Date.now()) because this will cause an infinite render loop.Hi, i’m facing this problem too…the Markers are only displayed on Android but not in iOS (the coordinates comes from redux).
I’m using the 0.22 version, any suggestion?
Hi @alvelig , I have the same problem, I already test (in #921fffb). but it still NOT work.