react-native-maps: Custom marker with Image won't render first time on Android
When making custom MapView.Marker with children as prop to Marker like this:
<MapView.Marker coordinate={coordinate}>
<View source={styles.container}>
<Image source={require('./my-image.png')} style={style.icon}>
// other stuff here
</View>
</MapView.Marker>
It does not render image on first render time on Android.
But if i use Marker’s image prop like this:
<MapView.Marker coordinate={coordinate} image={require('./my-image.png')}>
<View source={styles.container}>
// other stuff here
</MapView.Marker>
It does render on first time on Android too.
But I cant use second option because I have to set custom size to the image via styles. So either would be nice to be:
- able to style image which is passed by
imageprop OR - fix bug where image won’t render on first try.
Also note that in order to notice the first render bug you have to actually TURN OFF the app and then back on. 😃 Tell me if you experience this too
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 72
- Comments: 47 (8 by maintainers)
Here’s a workaround that doesn’t require hiding random elements:
The idea is to force the
<Image />to re-render after it is loaded the first time.This issue has been reported a bunch of times recently. Definitely a bug being experienced by everyone.
Using @gilbox’s idea of having a hidden element that changes using the image’s
onLoad, here’s a hack that works - onLoad force a reload of the component which regenerates a random number in the hidden text element…BARF! …but works! Need a fix!I solved this issue by using the same image again outside the mapViw. but with 0 width and 0 height.
<Image style={{width:0,height:0}} source={ Constants.Images.MarkerImage } />So that Constants.Images.MarkerImage will load before mapView loads markers.Hello @AhmedAly86 and @ewein .
I use j-nolan’s solution by changing it a little. Thanks to solutions of gilbox, sators and j-nolan we have workarounds for now.
any news on this issue? workarounds didn’t work for me. I even set a this.forceUpdate on a button call and change state and props and images didn’t get rendered.
I need to display image from the internet but had the same problem with Image component I ended up using WebView instead.
I have my Custom Marker function and i want to be able to give it a style to my image because it’s original size it’s 200 x 200. I’ve been struggling with this because when i try the image props from MapView.Marker, i can’t resize my image. Can someone share me their temporal fix or their solution to this issue? I’m kind of new in RN , so i’m not able to find a way to make it work on Android. iOS it’s working properly. `
`
@henrikra you could try forcing a view hierarchy change in the
Imagecomponent’sonLoadprop. The way I forced a view hierarchy change is by having some hidden text that changes value: https://github.com/airbnb/react-native-maps/pull/814/files#diff-36881e0ead965fdaf7cdcadb269a42cfR103Update in your package.json to “react-native-maps”: “^0.22.0”
Any new updates, i’m stacked
@seppemarotta this is my code for custom image marker
but as mentioned here, you cannot resize it via style. If you want to control the marker via style you probably should take a look here and here.
https://github.com/airbnb/react-native-maps/issues/882 https://github.com/airbnb/react-native-maps/issues/907
I’m having the same issue. I have also tried to pass the image as a prop to MapView.Marker however compromised the styles.